# 一、直接在tree根元素中新增。这种有个限制就是必须要勾选一或多条记录的时候按钮才会显示
<tree>
<header>
<button type="object" class="btn-primary" name="tests" icon="fa-whatsapp" string="Import Part"/>
</header>
</tree>
# 然后在编写处理方法
@api.model
def tests(self, select_ids):
pass
# 二、如果你要按钮一直显示的话
# 1.tree_button_template.xml
<?xml version="1.0" encoding="UTF-8"?>
<templates id="sync_import_base_partinfo" xml:space="preserve">
<t t-extend="ListView.buttons">
<t t-jquery=".o_list_buttons" t-operation="append">
<!-- 找到名为 "shipyard.parttype"的模型,并在它的列表(tree)视图后面append一个按钮 -->
<t t-if="widget and widget.modelName == 'shipyard.parttype'">
<!--btn表示按钮,btn-sm小按钮,btn-default默认按钮,btn-primary主要按钮?-->
<button class="btn btn-primary o_list_tender_bt_sync_equip" type="button">Sync Import</button>
</t>
</t>
</t>
</templates>
# 2.tree_button.js
odoo.define('shipyard_parttype.list_sync_import_base_partinfo_button_create', function (require) {
"use strict";
var ListView = require('web.ListView');
var viewRegistry = require('web.view_registry');
var ListController = require('web.ListController');
ListController.include({
renderButtons: function ($node) {
this._super.apply(this, arguments);
if (this.$buttons) {
# .o_list_tender_bt_sync_equip该选择器是上边template中的
this.$buttons.on('click', '.o_list_tender_bt_sync_equip', this._sync_import_base_partinfo.bind(this));
}
},
_sync_import_base_partinfo: function () {
var self = this;
var records = this.getSelectedIds();
self._rpc({ # rpc不明白可以往上翻其他小节
model: 'shipyard.parttype', # 模块
method: 'import_partitem', # 调用的方法
args: [records]
},
[]
);
}
});
})
;
# 3.定义python处理
@api.model
def import_partitem(self, pids): # pids为tree列表中勾选记录的id列表
pass
# 4.将js和xml加入到资源中去
<template id="assets_end" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script src="/ship_manage/static/src/js/tree_button.js" type="text/javascript" />
</xpath>
</template>
'qweb': [ # __manifase__.py中
'static/src/xml/tree_button_template.xml',
],