Skip to Content
Menu
This question has been flagged
1 Reply
4768 Views

Hi,

Is it possible to execute a JavaScript function while clicking on a menu item?

Avatar
Discard
Best Answer

Hi,

Yes it is possible by creating a client action for the menuitem.

For live examples you can refer the apps below

https://apps.odoo.com/apps/modules/13.0/hrms_dashboard/

https://apps.odoo.com/apps/modules/13.0/base_accounting_kit/

For example you can refer the code below.

Create a js file

odoo.define('model_name.action_name', function (require){
"use strict";
var core = require('web.core');
var AbstractAction = require('web.AbstractAction');
var NewAction = AbstractAction.extend({
init: function(){
this.function_name();
},
function_name: function(){
//Write the code here to preform menu item click.
});
core.action_registry.add('client_action_tag', NewAction);
});
});

Create a record with model ir.actions.client and specify the record id on menu item action.

<record id="action_menu" model="ir.actions.client">
<field
name="name">Render Template</field>
<field
name="tag">client_action_tag</field>
</record>

<menuitem
id="menu_client_action"
name="Client action Menu"
action="action_menu"
parent="model_name.parent_menu"/>
Avatar
Discard