我已经能够使用xml创建自定义按钮Add Bro
。
这里是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>
我的问题是,如何在按下按钮时调用此按钮创建操作。我尝试了名为action
的创建方法,因此它与按钮的name
属性相匹配但没有发生任何事情。
@api.multi
def action(self):
view_ref = self.env['ir.model.data'].get_object_reference('account', 'invoice_form')
view_id = view_ref[1] if view_ref else False
res = {
'type': 'ir.actions.act_window',
'name': _('Customer Invoice'),
'res_model': 'purchase.order',
'view_type': 'form',
'view_mode': 'form',
'view_id': view_id,
'target': 'new',
# 'context': {'default_partner_id': client_id}
}
return res
答案 0 :(得分:3)
您需要扩展ListView
窗口小部件并为按钮添加触发器:
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>
__ openerp __。py :
...
'data': [
...
"module_view.xml",
...
],
...
答案 1 :(得分:2)
看起来就是这样,因为这个模板没有直接调用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>