简体   繁体   中英

How can I check when certain form view is opened and field has changed in it on Odoo JavaScript?

What I Have so far is basic code:

odoo.define('partner_data.res_partner_widget', function(require) {
"use strict";

    var core = require('web.core');
    var Dialog = require('web.Dialog');
    var form_common = require('web.form_common');
    var Widget = require('web.Widget');

    var Model = require('web.Model');

    var _t = core._t;
    var QWeb = core.qweb;

    console.log('JS loaded');

    $(document).on('ready', function() {
        console.log('Doc is ready');
        $('#FIELD').on('change', function() {
            // Change value of other fields in this form

        });
    });
});

Problem is that document ready triggers in whole ODOO system. And trying to find the field by its name $(#fieldname) doesn't work at all.

Is there any solution SPECIFIC FOR ODOO for this problem? Or maybe you know really good documentation or example that explain on change method OF ODOO FIELD. PS I write ODOO in caps because everybody answers simple JQuery style, and this is not just simple JQuery, this must be something more specific related to ODOO. Or maybe I can call Python function of specific form view after the field has been changed, something like that. All odoo documentation that I found gives very little or no information about that.

UPDATE:

Thanks to @Vishal Khichadiya I got a bit close. I edited his answer by creating a widget. Now when I set this widget to the random field, let's say to some invisible field, I can use class class_partner on any field I want and it will trigger onchange method.

odoo.define('partner_data.res_partner_widget', function(require) {
"use strict";

var base = require('web_editor.base');
var options = require('web_editor.snippets.options');
var core = require('web.core');
var Dialog = require('web.Dialog');
var session = require('web.session');
var form_common = require('web.form_common');
var Widget = require('web.Widget');

var Model = require('web.Model');

var _t = core._t;
var QWeb = core.qweb;

var onchange_js_method_test = form_common.AbstractField.extend({
    start: function () {
        this._super();
        var self = this;
        $('body').on('change', '.class_partner', function() {
            console.log('start triggered');
            console.log(self)
            // Change value of other fields in this form
           //you can call python function from here to set your value
        });
    }
});
core.form_widget_registry.add('onchange_js_method_test', onchange_js_method_test);
});

xml:

<field name="random_invisible" " widget="onchange_js_method_test"/>
<field name="on_this_field_onchange_triggers" class="class_partner"/>

first of all you need to set class attribute to python filed in xml code. Ex:

<field name="partner_id" class="class_partner" />

then you need this in js and also add this js file to assets_backend.

    odoo.define('partner_data.res_partner_widget', function(require) {
    "use strict";

        var core = require('web.core');
        var Dialog = require('web.Dialog');
        var form_common = require('web.form_common');
        var Widget = require('web.Widget');

        var Model = require('web.Model');

        var _t = core._t;
        var QWeb = core.qweb;
        var my_widget = Widget.extend({
            start: function () {
                this._super();  
                var self = this;
                $('body').on('change', '.class_partner',function() {
                    // Change value of other fields in this form
                   //you can call python function from here to set your value
                });
           },
        });
        core.action_registry.add('my_widget', my_widget);
        return my_widget;
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM