jquery - Onchange 2 fields in JavaScript widget -


i need use onchange function in javascript widget

i have 2 fields in javascript widget - 1st(account refer contracts) , - 2nd (task refer customers) , want use onchange between them

i want change account field based on task field, when choose customer should load contracts of customer only.

anyone face before? give me examples?

here photo have 2 fields in line

here javascript code:

openerp.net2do_elkamel = function (instance) {      var module = instance.hr_timesheet_sheet      module.weeklytimesheet = module.weeklytimesheet.extend({         events: {             "click .oe_timesheet_weekly_account a": "go_to",             "click .oe_timesheet_weekly_task a": "go_to_partner",         },         go_to_partner: function (event) {             var id = json.parse($(event.target).data("cus-id"));             this.do_action({                 type: 'ir.actions.act_window',                 res_model: "res.partner",                 res_id: id,                 views: [[false, 'form']],                 target: 'current'             });         },         initialize_content: function () {             var self = this;             if (self.setting)                 return;             // don't render until have date_to , date_from             if (!self.get("date_to") || !self.get("date_from"))                 return;             this.destroy_content();              // it's important use vars avoid race conditions             var dates;             var accounts;             var account_names;             var task_names;             var default_get;             return this.render_drop.add(new instance.web.model("hr.analytic.timesheet").call("default_get", [                 ['account_id', 'customer', 'general_account_id', 'journal_id', 'date', 'name', 'user_id', 'product_id', 'product_uom_id', 'to_invoice', 'amount', 'unit_amount'],                 new instance.web.compoundcontext({'user_id': self.get('user_id')})]).then(function (result) {                 default_get = result;                 // calculating dates                 dates = [];                 var start = self.get("date_from");                 var end = self.get("date_to");                 while (start <= end) {                     dates.push(start);                     start = start.clone().adddays(1);                 }                  timesheet_lines = _(self.get("sheets")).chain()                     .map(function (el) {                         // simpler use id in cases                         if (typeof(el.account_id) === "object")                             el.account_id = el.account_id[0];                         if (typeof(el.customer) === "object")                             el.customer = el.customer[0];                         return el;                     }).value();                  // group account                 var timesheet_lines_by_account_id = _.groupby(timesheet_lines, function (el) {                     return el.account_id;                 });                  // group account , customer                 var timesheet_lines_by_account_id_task_id = _.groupby(timesheet_lines, function (el) {                     return [el.account_id, el.customer];                 });                  var account_ids = _.map(_.keys(timesheet_lines_by_account_id), function (el) {                     return el === "false" ? false : number(el)                 });                  return new instance.web.model("hr.analytic.timesheet").call("multi_on_change_account_id", [[], account_ids,                     new instance.web.compoundcontext({'user_id': self.get('user_id')})]).then(function (accounts_defaults) {                     accounts = _(timesheet_lines_by_account_id_task_id).chain().map(function (lines, account_id_task_id) {                         account_defaults = _.extend({}, default_get, (accounts_defaults[lines[0].account_id] || {}).value || {});                         // group days                         var index = _.groupby(lines, "date");                         var days = _.map(dates, function (date) {                             var day = {day: date, lines: index[instance.web.date_to_str(date)] || []};                             // add line insert/remove hours                             var to_add = _.find(day.lines, function (line) {                                 return line.name === self.description_line                             });                             if (to_add) {                                 day.lines = _.without(day.lines, to_add);                                 day.lines.unshift(to_add);                             } else {                                 day.lines.unshift(_.extend(_.clone(account_defaults), {                                     name: self.description_line,                                     unit_amount: 0,                                     date: instance.web.date_to_str(date),                                     account_id: lines[0].account_id,                                     customer: lines[0].customer,                                 }));                             }                             return day;                         });                         return {                             account_task: account_id_task_id,                             account: lines[0].account_id,                             task: lines[0].customer,                             days: days,                             account_defaults: account_defaults                         };                     }).value();                      // need name_get of analytic accounts                     return new instance.web.model("account.analytic.account").call("name_get", [_.pluck(accounts, "account"),                         new instance.web.compoundcontext()]).then(function (result) {                         account_names = {};                         _.each(result, function (el) {                             account_names[el[0]] = el[1];                         });                         // need name_get of customer                         return new instance.web.model("res.partner").call("name_get", [_(accounts).chain().pluck("task").filter(function (el) {                             return el;                         }).value(),                             new instance.web.compoundcontext()]).then(function (result) {                             task_names = {};                             _.each(result, function (el) {                                 task_names[el[0]] = el[1];                             });                             accounts = _.sortby(accounts, function (el) {                                 return account_names[el.account];                             });                         });                     });                 });             })).then(function (result) {                 // put gathered data in self, render                 self.dates = dates;                 self.accounts = accounts;                 self.account_names = account_names;                 self.task_names = task_names;                 self.default_get = default_get;                 //real rendering                 self.display_data();             });         },         init_add_account: function () {             var self = this;             if (self.dfm)                 return;             self.$(".oe_timesheet_weekly_add_row").show();             self.dfm = new instance.web.form.defaultfieldmanager(self);             self.dfm.extend_field_desc({                 account: {                     relation: "account.analytic.account",                 },                 task: {                     relation: "res.partner",                 },             });             self.task_m2o = new instance.web.form.fieldmany2one(self.dfm, {                 attrs: {                     name: "task",                     type: "many2one",                     modifiers: '{"required": true}',                 },             });               self.account_m2o = new instance.web.form.fieldmany2one(self.dfm, {                  attrs: {                     name: "account",                     type: "many2one",                     domain: [                         ['type', 'in', ['normal', 'contract']],                         ['use_timesheets', '=', 1],                     ],                     widget:"selection",                      context: {                         default_use_timesheets: 1,                         default_type: "contract",                         default_partner_id:self.task_m2o.value,                     },                     modifiers: '{"required": true}',                 },             });               self.task_m2o.prependto(self.$(".oe_timesheet_weekly_add_row td"));             self.account_m2o.prependto(self.$(".oe_timesheet_weekly_add_row td"));              self.account_m2o.$input.focusout(function () {                 var account_id = self.account_m2o.get_value();                 if (account_id === false) {                     return;                 }                 self.task_m2o.init(self.dfm, {                     attrs: {                         name: "task",                         type: "many2one",                         context: {                             'account_id': account_id,                         },                     },                 });                 // reset value selected                 self.task_m2o.set_value(false);                 self.task_m2o.render_value();             });              self.$(".oe_timesheet_weekly_add_row button").click(function () {                 var id = self.account_m2o.get_value();                 if (id === false) {                     self.dfm.set({display_invalid_fields: true});                     return;                 }                 var ops = self.generate_o2m_value();                 new instance.web.model("hr.analytic.timesheet").call("on_change_account_id", [[], id]).then(function (res) {                     var def = _.extend({}, self.default_get, res.value, {                         name: self.description_line,                         unit_amount: 0,                         date: instance.web.date_to_str(self.dates[0]),                         account_id: id,                         customer: self.task_m2o.get_value(),                     });                     ops.push(def);                     self.set({"sheets": ops});                 });             });         },          get_box: function (account, day_count) {             return this.$('[data-account-task="' + account.account_task + '"][data-day-count="' + day_count + '"]');         },         get_total: function (account) {             return this.$('[data-account-task-total="' + account.account_task + '"]');         },     }); }; 


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -