在模塊的列表視圖中創(chuàng)建自定義按鈕,點(diǎn)擊觸發(fā)彈出瞬態(tài)模型對(duì)應(yīng)form的事件
1.創(chuàng)建Js文件,注冊(cè)按鈕事件,放置到 'static/src/js/' 路徑下
odoo.define('起個(gè)名字', function (require) {
"use strict";
var ListController = require('web.ListController');
var ListView = require('web.ListView');
var viewRegistry = require('web.view_registry');
function renderGenerateButton() {
if (this.$buttons) {
var self = this;
// var lead_type = self.initialState.getContext()['default_type'];
this.$buttons.on('click', '.o_button_模塊名', function () {
self.do_action({
name: '名字',
type: 'ir.actions.act_window',
res_model: '點(diǎn)擊按鈕后彈出Form對(duì)應(yīng)模型名',
target: 'new',
views: [[false, 'form']],
context: {'is_modal': true,},
});
});
}
}
var ModelListController = ListController.extend({
willStart: function () {
var self = this;
var ready = this.getSession().user_has_group('base.group_user')
.then(function (is_sale_manager) {
if (is_sale_manager) {
self.buttons_template = '模塊列表視圖.buttons';
}
});
return Promise.all([this._super.apply(this, arguments), ready]);
},
renderButtons: function () {
this._super.apply(this, arguments);
renderGenerateButton.apply(this, arguments);
}
});
var ModelListView = ListView.extend({
config: _.extend({}, ListView.prototype.config, {
Controller: ModelListController,
}),
});
//將tree視圖的按鈕注冊(cè)到視圖
viewRegistry.add('XXX_tree', ModelListView );
});
2.在 'views/template.xml' 內(nèi)引入上一步創(chuàng)建的Js文件;或者在 'views/' 目錄下新建一個(gè)xml文件引入也行,但是要注意 'template' 標(biāo)簽的id一定要與其他xml文件內(nèi) 'template' 的 id 不一樣,不然會(huì)被覆蓋,導(dǎo)致Js導(dǎo)入失效
<odoo>
<data>
<template id="assets_backend" name="ecn_bom assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/模塊/static/src/js/XXX.js"/>
</xpath>
</template>
</data>
</odoo>
3.創(chuàng)建xml文件,定義按鈕樣式,按鈕文字以及限制在指定模塊顯示按鈕,放置到 'static/src/xml/' 路徑下
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="模塊簡(jiǎn)稱.generate_leads_button">
//在指定模塊內(nèi)顯示按鈕
<t t-if="widget.modelName=='模塊名字'">
<button type="button" class="btn btn-secondary o_button_plan">
按鈕文字
</button>
</t>
</t>
<t t-extend="ListView.buttons" t-name="模塊列表視圖.buttons">
//在對(duì)應(yīng)視圖的導(dǎo)出按鈕后面顯示
<t t-jquery="button.o_list_export_xlsx" t-operation="after">
<t t-call="模塊簡(jiǎn)稱.generate_leads_button"/>
</t>
</t>
</templates>
4.在你想要顯示按鈕的視圖中的tree引用這個(gè)js事件
<record model="ir.ui.view" id="view_tree">
<field name="title">模塊.tree</field>
<field name="model">模型</field>
<field name="arch" type="xml">
<!--這里js_class里填入的是上面Js文件中最底下注冊(cè)到視圖中tree對(duì)應(yīng)的名字-->
<tree js_class="XXX_tree">
<field name='XXX'/>
</tree>
</field>
</record>
5.在manifest.py中引入文件
'data': [
'views/templates.xml',
],
'qweb': [
'static/src/xml/第三步的xml文件.xml',
]
6.效果圖

效果圖