前段時間寫的【odoo自定義按鈕】不是很清晰,今天有時間,翻一遍,掛好注釋。
開始了還是那個需求:
在odoo的前端頁面,我們也許需要在【創建】/【保存】/【導入】等按鈕后,增加自定義按鈕,比如【打印XXX】、【合並XXX】這種odoo沒有提供的按鈕。
下面是一個自定義按鈕的例子,例子是在繼承 hr.employee 的基礎上做的:
創建 page_button/static/src/xml/tree_view_button.xml
這里創建template去拓展odoo原有的列表視圖和from視圖,不要忘了把它加載到 __manifest__.py里面
在第五行,o_list_button_save,是odoo的列表視圖提供的【保存】按鈕的標識,
在第十一行,o_form_button_create,是odoo的表單視圖提供的【創建】按鈕的標識。
我選擇分別在這兩個按鈕后,新增我的自定義按鈕【Tree:Say Hello】和【Form:Say Hello】
<?xml version="1.0" encoding="UTF-8"?> <template id="template" xml:space="preserve"> <!-- 拓展Tree視圖增加自定義按鈕 --> <t t-extend="ListView.buttons"> <t t-jquery="button.o_list_button_save" t-operation="after"> <button type="button" class="btn btn-primary o_list_tender_button_say_hello" style="display:inline-block;">Tree:Say Hello</button> </t> </t> <!-- 拓展Form視圖增加自定義按鈕 --> <t t-extend="FormView.buttons"> <t t-jquery="button.o_form_button_create" t-operation="after"> <button type="button" class="btn btn-primary o_list_tender_button_say_hello" style="display:inline-block;">Form:Say Hello</button> </t> </t> </template>
創建 page_button/wizard/widzard_employee.py
此文件是做提示用,所以這里用了個瞬態模型TransientModel
from odoo import api, fields, models, _ class ShowMessageWizard(models.TransientModel): _name = "message.wizard" _description = "提示一下" def say_hello(self): context = dict(self._context or {}) view_type = context.get('view_type') actived_id = context.get('actived_id') active_ids = context.get('active_ids') print("視圖類型:", view_type) if view_type == "form": print("Form Selected ID:", actived_id) elif view_type == "list": print("Tree Selected IDs:", active_ids) else: print("其他視圖類型的ID在JS里自行傳值吧。") print("接下來做你想做的")
創建 page_button/wizard/widzard_employee.xml
這個文件時提示的視圖,代碼中的say_hello就是上面py文件的方法。
<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <record id="show_message_wizard" model="ir.ui.view"> <field name="name">Show Message Wizard</field> <field name="model">message.wizard</field> <field name="arch" type="xml"> <form string="Message"> <h3>要說Hello嗎?</h3> <footer> <button name="say_hello" string="Yes" type="object" class="btn-primary"/> <button string="No" class="btn-primary" special="cancel"/> </footer> </form> </field> </record> </data> </odoo>
創建 page_button/static/src/js/extend_view_button.js
這個文件主要是監聽上面定義的按鈕,根據觸發的事件,操作后台。
在JS中你只需要關注的是:self.do_action 里的代碼
odoo.define('hr_employee.tree_view_button', function (require) { "use strict"; var core = require('web.core'); var ListView = require('web.ListView'); var ListController = require('web.ListController'); var FormView = require('web.FormView'); var FormController = require('web.FormController'); var ImportViewMixin = { init: function (viewInfo, params) { var importEnabled = 'import_enabled' in params ? params.import_enabled : true; this.controllerParams.importEnabled = importEnabled; }, }; var ImportControllerMixin = { init: function (parent, model, renderer, params) { this.importEnabled = params.importEnabled; }, _bindImport: function () { if (!this.$buttons) { return; } var self = this; this.$buttons.on('click', '.o_list_tender_button_say_hello', function () { var view_type = self.viewType; var actived_id; var actived_ids = []; if (view_type == "form") { actived_id = self.model.get(self.handle).data.id; console.log(actived_id); // 至此你獲取到了 當前form 的ID,你可以在JS里拿這着這個ID搞點事 // 當然,你也可以去調用后台的方法,或者打開一個新的頁面,一個新的wizard } else { var state = self.model.get(self.handle, {raw: true}); for (var i = 0; i < $('tbody .o_list_record_selector input').length; i++) { if ($('tbody .o_list_record_selector input')[i].checked === true) { actived_ids.push(state.res_ids[i]); } } var ctx = state.context; ctx['active_ids'] = actived_ids; console.log(actived_ids); // 至此你獲取到了你勾選的項的ID,你可以在JS里拿這着這些ID搞點事 // 當然,你也可以去調用后台的方法,或者打開一個新的頁面,一個新的wizard } var resmodel = "message.wizard"; var resname = "提示一下"; if ((view_type == "list" && actived_ids.length >= 1 ) || (view_type == "form")) { // 這里的例子是彈出一個wizard提示,根據用戶選擇操作后台 self.do_action ({ type: 'ir.actions.act_window', name: resname, res_model: resmodel, views: [[false, 'form']], target: 'new', context: { view_type: view_type, active_ids: actived_ids, actived_id: actived_id, }, }, { on_reverse_breadcrumb: function () { self.reload(); }, on_close: function () { self.reload(); } }); } else { $(function () { alert("啥都沒選擇啊") }); } }); } }; // 拓展LIST ListView.include({ init: function () { this._super.apply(this, arguments); ImportViewMixin.init.apply(this, arguments); }, }); ListController.include({ init: function () { this._super.apply(this, arguments); ImportControllerMixin.init.apply(this, arguments); }, renderButtons: function () { this._super.apply(this, arguments); ImportControllerMixin._bindImport.call(this); } }); // 拓展FORM FormView.include({ init: function (viewInfo) { this._super.apply(this, arguments); this.controllerParams.viewID = viewInfo.view_id; ImportViewMixin.init.apply(this, arguments); }, }); FormController.include({ init: function (parent, model, renderer, params) { this._super.apply(this, arguments); this.viewID = params.viewID; ImportControllerMixin.init.apply(this, arguments); }, renderButtons: function () { this._super.apply(this, arguments); // Sets this.$buttons ImportControllerMixin._bindImport.call(this); } }); });
創建 page_button/views/tree_view_asset.xml
此文件用於引入上面的JS,不要忘了把它加載到 __manifest__.py里面
<?xml version="1.0" encoding="utf-8"?> <odoo> <data> <template id="assets_backend" name="tree view menu" inherit_id="web.assets_backend"> <xpath expr="." position="inside"> <script type="text/javascript" src="/page_button/static/src/js/extend_view_button.js"></script> </xpath> </template> </data> </odoo>
如有問題,請指正。
2020 05 25 更新: 按照 qq_36762265 給出的建議,在xml 中增加了 t-if 判斷。之前我是在JS中用樣式控制的,代碼沒貼上來。大家可以按下面這樣去實現。
補充:ODOO13 可用
<?xml version="1.0" encoding="UTF-8"?> <template id="template" xml:space="preserve"> <!-- 拓展Tree視圖增加自定義按鈕 --> <t t-extend="ListView.buttons"> <t t-jquery="button.o_list_export_xlsx" t-operation="after"> <t t-if="widget.modelName=='hr.employee'"> <button type="button" class="btn btn-primary o_button_send_to_phosee" style="display:inline-block;">xxxxxxxx</button> </t> </t> </t> ..... </template>