例如,在Employee列表视图中添加“Sync Data”按钮。
它将在“hr.employee”模型上执行现有的“cron_process_staging”方法。
假设您的模块名称是“vit_staging”。
1.InheritListView QWeb模板
创建继承Odoo ListView模板的qweb模板,保存到static / xml / qweb.xml
<?xml version =“1.0”encoding =“UTF-8”?> <templates id =“sync_template”xml:space =“preserve”> <t t-extend =“ListView.buttons”> <t t-jquery =“button.o_list_button_add”t-operation =“after”> <t t-if =“widget.model =='hr.employee'”> <button class =“btn btn-sm btn-default sync_button”type =“button”>同步数据</ button> </ T> </ T> </ T> </模板>
2.加载模板
从清单文件加载qweb:
“qweb”: “静态/ src目录/ XML / qweb.xml” ]
3.创建JS Handler
创建该按钮的JS onclick处理程序,保存到static / src / js / sync.js
openerp.vit_staging = function(instance){ var ListView = instance.web.ListView; ListView.include({ render_buttons:function(){ //获取按钮参考 this._super.apply(this,arguments) if(this。$ buttons){ var btn = this。$ buttons.find('。sync_button') } //执行行动 btn.on('click',this.proxy('do_sync')) }, do_sync:function(){ new instance.web.Model('hr.employee') .call('cron_process_staging',[[]]) .done(function(result){ 警报(“完成”) }) } }); }
4.从Web Asset加载JS文件
从Web资源加载JS文件,保存到view / web_asset.xml
<?xml version =“1.0”encoding =“utf-8”?> <odoo> <数据> <template id =“assets_backend”name =“x_export_view assets”inherit_id =“web.assets_backend”> <xpath expr =“。” 位置=“内部”> <script type =“text / javascript”src =“/ vit_staging / static / src / js / sync.js”> </ script> </的xpath> </模板> </数据> </ odoo>
5.加载Web资产
从清单加载Web资产:
“数据”:[ “观看/ web_asset.xml”, ]
6.完成!
安装你的插件。