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

What I have so far is the 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

        });
    });
});

      

The problem is that the finished documents run across the entire ODOO system. And trying to find a field under its name $(#fieldname)

doesn't work at all.

Is there any SPECIFIC ODOO solution for this problem? Or maybe you know some really good documentation or an example that explains the method for modifying ODOO FIELD. Postscript I write ODOO in headers because everyone responds to simple JQuery style, and it's not just JQuery, it should be something more ODOO specific. Or maybe I can call a Python function of a certain kind of form after the field has been changed, something like this. All the documentation that I have found gives very little information or not.

UPDATE:

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

.

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"/>

      

+3


source to share


1 answer


first of all you need to set the class attribute for python fed into xml code. Example:

<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;
    });

      

+1


source







All Articles