odoo8 TreeView视图创建按钮旁添加按钮
由于最近工作需要,需要在Tree视图上面添加一个合并发货的按钮。在网上查找到相关资料(http://blog.****.net/vnsoft/article/details/49621373),并模仿添加上了合并发货的按钮。经过测试发现,这种方法添加的按钮在Tree视图上,但Tree视图已经不是我定义的视图,而且不能进入form视图。因此不能满足项目的要求,这下就尴尬了, 通过百度、google搜索相关odoo在tree视图添加按钮的方法,都没有查找到完整的资料。到此已经花费了2天时间,已经严重影响到项目的开发进度,只能先去做其他的,后面再来看这个问题。最终通过多方对比,终于把该功能完成。下面是相关的代码。
首先,在js中写如下代码,
var QWeb = instance.web.qweb,
_t = instance.web._t,
_lt = instance.web._lt;
instance.web.agh_enhance = instance.web.agh_enhance || {};
/**
* 合并发货
* */
instance.web.ListView.include({
init: function() {
var self = this;
this._super.apply(this, arguments);
this.on('list_view_loaded', this, function(data) {
if(self.__parentedParent.$el.find('.oe_generate_po').length == 0
&& self.dataset.model == 'stock.picking' && self.dataset.context.name=='创建物流单'){
var button = $("<button type='button' class='oe_button oe_highlight oe_generate_po'>合并发货</button>")
.click(this.proxy('create_logistics_bill'));
self.__parentedParent.$el.find('.oe_list_buttons').append(button);
}
});
},
create_logistics_bill: function () {
var self=this;
var ctx = this.ViewManager.action.context;
var active_records = this.groups.get_selection();
ctx['active_records'] = active_records;
new instance.web.Model(self.dataset.model).call("create_logistics_bill", [false, ctx]).
then(function(result) {
var res_id = result;
// self.ViewManager.ActionManager.history_back();
self.do_action({
name: ctx['name'],
type: 'ir.actions.act_window',
res_model: 'logistics.bill',
view_mode: 'form',
view_type: 'form',
views: [[false, 'form']],
target: 'current',
res_id: res_id,
context: ctx,
params: {
model: self.dataset.model,
// self.dataset.get_context() could be a compound?
// not sure. action's context should be evaluated
// so safer bet. Odd that timezone & al in it
// though
context: self.getParent().action.context,
}
}, {
on_reverse_breadcrumb: function () {
self.reload();
},
on_close: function () {
self.reload();
}
});
});
}
});
然后在stock_picking.py文件中添加create_logistics_bill方法,实现合并发货,代码如下:
@api.multi
def create_logistics_bill(self):
ctx = self.env.context
record_ids = ctx['active_records']['ids']
if not record_ids:
raise ValidationError(u'请选择合并发货的订单!')
err_msgs = []
for record in ctx['active_records']['records']:
if record.get('wait_customer_notice'):
err_msgs.append('%s需要等待客户通知发货后才能发货'% record.get('origin'))
if err_msgs:
raise ValidationError('\n'.join(err_msgs))
dict(ctx).update({'active_model': self._name,
'active_ids': record_ids,
'active_id': len(record_ids) and record_ids[0] or False})
logistics_bill = self.env['logistics.bill'].create({'picking_ids': record_ids or False})
return logistics_bill.id
#不清楚下面这个为什么不能直接返回logistics.bill的form视图,有大神知道为什么吗?
# return self.env['logistics.bill'].wizard_view(logistics_bill.id)
效果图:
第一次写博客,写得不好,请大家见谅!