Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
5378 Lượt xem

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất
  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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất
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.

Ảnh đại diện
Huỷ bỏ