Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
5164 Visualizzazioni

for example:    when a field value changed   , I want to do something in the onchange event of the element.

Avatar
Abbandona
Risposta migliore
  1. Create a new module or identify the existing module where you want to add the custom JavaScript code.

  2. Inside your module, create a new JavaScript file, let's call it custom_script.js, and add your custom JavaScript code in it. For example:

odoo.define('your_module.custom_script', function (require) {
"use strict";

var FormController = require('web.FormController');
var core = require('web.core');

FormController.include({
_onFieldChanged: function (event) {
this._super.apply(this, arguments);

var record = this.model.get(event.target.dataset.recordId);
var fieldName = event.target.name;

#your custom logic based on the changed field value
if (fieldName === 'your_field_name') {
var fieldValue = record.data[fieldName].res_id;
# Perform actions based on the field value change
# ....
}
},
});
});


note;make sure add the js file reference in xml file


Avatar
Abbandona
Risposta migliore

Hi,

You need to include your custom event inside form controller.

var FormController = require("web.FormController");
FormController.include({
events: {
'change #any_field': '_onChange',
},
_onChange: function (event) {
//Do what you want
}
});

Regards

Avatar
Abbandona
Autore Risposta migliore
odoo.define('hx_tsl.autosave',function(require){
"use strict";
var FormController= require('web.FormController');
console.log("enter");
FormController.include({
events: {
'change #is_return': '_onChange',
},
_onChange: function (event) {
console.log("value changed!")
},

}
});

})

it seems does not work in odoo16 community edition.  "enter"  was printed,but   "value changed" not printed.

Avatar
Abbandona