Python 如何在自定义按钮上创建操作
我已经能够使用xml创建自定义按钮Python 如何在自定义按钮上创建操作,python,openerp,odoo-8,odoo-9,Python,Openerp,Odoo 8,Odoo 9,我已经能够使用xml创建自定义按钮addbro 这里是xml <templates> <tr t-extend="ListView.buttons"> <t t-jquery="button.o_list_button_add" t-operation="after"> <button id="tahu" name="action" type="object" class="btn btn-sm btn-primary"&g
addbro
这里是xml
<templates>
<tr t-extend="ListView.buttons">
<t t-jquery="button.o_list_button_add" t-operation="after">
<button id="tahu" name="action" type="object" class="btn btn-sm btn-primary">
Add Bro
</button>
</t>
</tr>
</templates>
在XML代码中是这样的,因为此模板不直接调用any方法,所以可以使用xpath
<xpath expr="/form/header/button[@name='invoice-open']" position="after">
<!-- put your button here -->
</xpath>
例如:
<record id="invoice_form_my" model="ir.ui.view">
<field name="name">account.invoice.form.my</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="/form/header/button[2][@string='Print']" position="after">
<button name="my_button" string="Print2" class="oe_highlight"/>
</xpath>
</field>
</record>
账户、发票、表格、我的
帐户、发票
您需要扩展列表视图
小部件并向按钮添加触发器:
openerp.you_module_name_here = function(instance){
var _t = instance.web._t,
_lt = instance.web._lt;
var QWeb = instance.web.qweb;
instance.web.ListView.include({
load_list: function(data) {
if (this.$buttons) {
this.$buttons.find('#tahu').click(this.proxy('action')) ;
}
},
action: function () {
var model_obj = new instance.web.Model('ir.model.data');
view_id = model_obj.call('get_object_reference', ["account", "invoice_form"]);
this.do_action(
name: _t('Customer Invoice'),
type: 'ir.actions.act_window',
res_model: 'purchase.order',
view_type: 'form',
view_mode: 'form',
view_id: view_id,
target: 'new'
);
}
});
}
创建一个js
文件(script.js
),在/static/src/js/
下包含上述代码,并创建一个xml
文件(`module_view.xml),其中包含以下代码:
<template id="assets_backend_custom" name="custom assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/your_module_name_here/static/src/js/script.js"></script>
</xpath>
</template>
<template id="assets_backend_custom" name="custom assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/your_module_name_here/static/src/js/script.js"></script>
</xpath>
</template>
...
'data': [
...
"module_view.xml",
...
],
...