Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
470 Widoki

can i use a js function in the odoo view

Awatar
Odrzuć
Najlepsza odpowiedź

This is the most common method of defining a function in the Odoo .js.
this type of function definition is Odoo module based and also this is completely js. 

odoo.define('module.Name', function (require) {

    "use strict";


    // Module code here

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

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

    var QWeb = core.qweb;


    return AbstractAction.extend({

        // Custom module logic

    });

});

And python related functions also is there in which we are calling the python method from back-end model.

like given below

_fetchData: function () {

    return this._rpc({

        model: 'res.partner',

        method: 'search_read',

        domain: [['active', '=', true]],

        fields: ['name', 'email', 'phone']

    }).then((result) => {

        // Process returned data

        this.data = result;

    }).catch((error) => {

        // Error handling

        console.error('Fetch error:', error);

    });

},

here the model, method etc. are we will define in the python code

for using in the view you can use eventListner like belowodoo.define('module.Name', function (require) {

    "use strict";

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

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

    var QWeb = core.qweb;


    return AbstractAction.extend({

        // Define template with a button

        template: 'module.template',


        // Define events object

        events: {

            'click .fetch-data-btn': '_onFetchDataClick'

        },


        // Event handler method

        _onFetchDataClick: function (event) {

            event.preventDefault();

            this._fetchData();

        },


        // RPC method to fetch data

        _fetchData: function () {

            return this._rpc({

                model: 'res.partner',

                method: 'search_read',

                domain: [['active', '=', true]],

                fields: ['name', 'email', 'phone']

            }).then((result) => {

                // Process returned data

                this.data = result;

                this._renderData(result);

            }).catch((error) => {

                // Error handling

                console.error('Fetch error:', error);

            });

        },

    });

});

also you can mention the button(triggering element) with the class name inside the view like 

<button class="btn btn-primary fetch-data-btn"> Fetch Partner Data </button>

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,

Please refer the following blog:https://www.cybrosys.com/blog/how-to-use-js-class-in-form-views-in-odoo-17#

Hope it helps

Awatar
Odrzuć