can i use a js function in the odoo view
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Księgowość
- Zapasy
- PoS
- Project
- MRP
To pytanie dostało ostrzeżenie
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>
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
Podoba Ci się ta dyskusja? Dołącz do niej!
Stwórz konto dzisiaj, aby cieszyć się ekskluzywnymi funkcjami i wchodzić w interakcje z naszą wspaniałą społecznością!
Zarejestruj się