3
votes

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. P.S. 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"/>
1
You should change the java tag to javascript. They are unrelated programming languages despite their name.OH GOD SPIDERS
Why do you try to fire an onchange event there (JS)? What is your goal?CZoellner
I need to change value of other field whenever #FIELD value changes and if you want to suggest me to do that with Python, no its not gonna work.enigmq
Do you want to call python function from javascript? Is that work for you or you want to change field value using js only?Vishal Khichadiya
Well calling python function from javascript would we really good, I know that you can do that with new Model.... But I don't know how to do that exactly when field changed, all the standard stuff which works in regular website development doesn't work in odoo, there has to be do something more and I don't know what.enigmq

1 Answers

1
votes

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