在listview的web视图上增加一个按钮的问题
- 
拿到里面来嘛~
<br /><templates><br />        <t t-extend="ListView.buttons">            <br />                <t t-jquery="span.oe_alternative" t-operation="append"><br />                        <t t-if="widget.options.import_lead_enable !== false"><br />                                      <span class="oe_fade">or</span><br />                                      <a href="#" class="oe_bold oe_list_button_import_lead">Import Lead</a><br />                        </t><br />                </t><br />        </t><br />    </t><br /></templates><br /> - 
我自己写了一下测试了,找到解决方法了,load_list里面可以remove,那就说明或/按钮已经存在了,也就是说t-if已经执行过了,所以不行,我实验了下,修改到start方法中就可以了,<t t-jquery="span.oe_alternative" t-operation="append">这个你写错了,应该是after,还有,直接判断<t t-if="widget.options.import_lead_enable !== false">,就不需要在remove按钮了,t-if把或和按钮都包含进去就可以了。
js代码:openerp.add_button = function(instance) {<br />    var _t = instance.web._t,<br />        _lt = instance.web._lt;<br />    var QWeb = instance.web.qweb;<br /><br />    instance.add_button = {};<br /><br />    var MODELS_NOT_TO_HIDE = ['crm.lead'];<br />    instance.web.ListView.include({<br />        start: function() {<br />            this.options.my_btn = true;<br />            this.$el.addClass('oe_list');<br />            return this._super();<br />        },<br />    });<br />}
xml代码:    <t t-extend="ListView.buttons"><br />        <t t-jquery="span.oe_alternative" t-operation="after"><br />            <t t-if="widget.options.my_btn"><br />                <span class="oe_fade">or</span><br />                <a href="#" class="oe_bold oe_list_button_import_lead">Import Lead</a><br />            </t><br />        </t><br />    </t> - 
直接include listview就好了。
instance.web.ListView.include({
load_list: function () {
var self = this;
this._super.apply(this, arguments);
var b_length= self.__parentedParent.$el.find('.ok_bn').length;
if(b_length ==0 && self.model=="res.partner"){ //通过获取this中的对象信息进行分析最终确定在那个地方加按钮。
console.log(self.$buttons[0].firstChild.className!="oe_button ok_bn");
console.log(+b_length<=0+"ok_bnok_bnok_bnok_bnok_bnok_bnok_bn");
var button = $("服务");
root=self.$el.parents();
button.prependTo(root.find('.oe_list_buttons'));
this.$buttons.on('click', '.ok_bn', function() {
self.do_action({
type: 'ir.actions.act_window',
res_model: "product.product",
res_id: 1,
views: [[false, 'form']],
target: 'current',
context:"",
}, {
on_reverse_breadcrumb: function () {
self.reload();
}
});
});
}
},
});
但是不推荐这么做。不符合odoo的思想。有旭哥(http://odoo.nbzx.me) 找到一个一个好方法。
(推荐使用)
<record id="action_purchase_need" model="ir.actions.act_window">
<field name="name">采购 </field>
<field name="res_model">purchase.need</field>
<field name="view_type">form</field>
<field name='view_mode'>tree_purchase_need</field>
<field name="view_id" ref="purchase_need_tree"/>
</record>
instance.web.views.add('tree_purchase_need', 'instance.web.dftg_ext.NeedListView');
instance.web.dftg_ext.NeedListView = instance.web.ListView.extend({
init: function() {
var self = this;
this._super.apply(this, arguments);
this.on('list_view_loaded', this, function() {
if(self.__parentedParent.$el.find('.oe_purchase_need').length == 0){
var button1 = $("<button type='button' class='oe_button oe_purchase_need'>刷新采购需求</button>")
.click(this.proxy('do_refresh_action'));
self.__parentedParent.$el.find('.oe_list_buttons').append(button1);
}
});
},
do_refresh_action: function () {
var self = this;
var action_manager = new instance.web.ActionManager(self);
return action_manager.do_action('dftg_ext.action_purchase_need_refresh_wizard', {
on_close: function () {
self.do_search(self.last_domain, self.last_context, self.last_group_by);
}
});
},
//重载列表数据
do_search: function(domain, context, group_by) {
var self = this;
this.last_domain = domain;
this.last_context = context;
this.last_group_by = group_by;
this.old_search = _.bind(this._super, this);
return self.old_search(new instance.web.CompoundDomain(self.last_domain), self.last_context, self.last_group_by);
},
}); - 
[quote author=开阖-静静 link=topic=14531.msg31382#msg31382 date=1447298909]
直接include listview就好了。
instance.web.ListView.include({
load_list: function () {
var self = this;
this._super.apply(this, arguments);
var b_length= self.__parentedParent.$el.find('.ok_bn').length;
if(b_length ==0 && self.model=="res.partner"){ //通过获取this中的对象信息进行分析最终确定在那个地方加按钮。
console.log(self.$buttons[0].firstChild.className!="oe_button ok_bn");
console.log(+b_length<=0+"ok_bnok_bnok_bnok_bnok_bnok_bnok_bn");
var button = $("服务");
root=self.$el.parents();
button.prependTo(root.find('.oe_list_buttons'));
this.$buttons.on('click', '.ok_bn', function() {
self.do_action({
type: 'ir.actions.act_window',
res_model: "product.product",
res_id: 1,
views: [[false, 'form']],
target: 'current',
context:"",
}, {
on_reverse_breadcrumb: function () {
self.reload();
}
});
});
}
},
});
但是不推荐这么做。不符合odoo的思想。有旭哥(http://odoo.nbzx.me) 找到一个一个好方法。
(推荐使用)
<record id="action_purchase_need" model="ir.actions.act_window">
<field name="name">采购 </field>
<field name="res_model">purchase.need</field>
<field name="view_type">form</field>
<field name='view_mode'>tree_purchase_need</field>
<field name="view_id" ref="purchase_need_tree"/>
</record>
instance.web.views.add('tree_purchase_need', 'instance.web.dftg_ext.NeedListView');
instance.web.dftg_ext.NeedListView = instance.web.ListView.extend({
init: function() {
var self = this;
this._super.apply(this, arguments);
this.on('list_view_loaded', this, function() {
if(self.__parentedParent.$el.find('.oe_purchase_need').length == 0){
var button1 = $("<button type='button' class='oe_button oe_purchase_need'>刷新采购需求</button>")
.click(this.proxy('do_refresh_action'));
self.__parentedParent.$el.find('.oe_list_buttons').append(button1);
}
});
},
do_refresh_action: function () {
var self = this;
var action_manager = new instance.web.ActionManager(self);
return action_manager.do_action('dftg_ext.action_purchase_need_refresh_wizard', {
on_close: function () {
self.do_search(self.last_domain, self.last_context, self.last_group_by);
}
});
},
//重载列表数据
do_search: function(domain, context, group_by) {
var self = this;
this.last_domain = domain;
this.last_context = context;
this.last_group_by = group_by;
this.old_search = _.bind(this._super, this);
return self.old_search(new instance.web.CompoundDomain(self.last_domain), self.last_context, self.last_group_by);
},
});
[/quote]
 静静 你是男是女? - 
[quote author=开阖-静静 link=topic=14531.msg31382#msg31382 date=1447298909]
直接include listview就好了。
instance.web.ListView.include({
load_list: function () {
var self = this;
this._super.apply(this, arguments);
var b_length= self.__parentedParent.$el.find('.ok_bn').length;
if(b_length ==0 && self.model=="res.partner"){ //通过获取this中的对象信息进行分析最终确定在那个地方加按钮。
console.log(self.$buttons[0].firstChild.className!="oe_button ok_bn");
console.log(+b_length<=0+"ok_bnok_bnok_bnok_bnok_bnok_bnok_bn");
var button = $("服务");
root=self.$el.parents();
button.prependTo(root.find('.oe_list_buttons'));
this.$buttons.on('click', '.ok_bn', function() {
self.do_action({
type: 'ir.actions.act_window',
res_model: "product.product",
res_id: 1,
views: [[false, 'form']],
target: 'current',
context:"",
}, {
on_reverse_breadcrumb: function () {
self.reload();
}
});
});
}
},
});
但是不推荐这么做。不符合odoo的思想。有旭哥(http://odoo.nbzx.me) 找到一个一个好方法。
(推荐使用)
<record id="action_purchase_need" model="ir.actions.act_window">
<field name="name">采购 </field>
<field name="res_model">purchase.need</field>
<field name="view_type">form</field>
<field name='view_mode'>tree_purchase_need</field>
<field name="view_id" ref="purchase_need_tree"/>
</record>
instance.web.views.add('tree_purchase_need', 'instance.web.dftg_ext.NeedListView');
instance.web.dftg_ext.NeedListView = instance.web.ListView.extend({
init: function() {
var self = this;
this._super.apply(this, arguments);
this.on('list_view_loaded', this, function() {
if(self.__parentedParent.$el.find('.oe_purchase_need').length == 0){
var button1 = $("<button type='button' class='oe_button oe_purchase_need'>刷新采购需求</button>")
.click(this.proxy('do_refresh_action'));
self.__parentedParent.$el.find('.oe_list_buttons').append(button1);
}
});
},
do_refresh_action: function () {
var self = this;
var action_manager = new instance.web.ActionManager(self);
return action_manager.do_action('dftg_ext.action_purchase_need_refresh_wizard', {
on_close: function () {
self.do_search(self.last_domain, self.last_context, self.last_group_by);
}
});
},
//重载列表数据
do_search: function(domain, context, group_by) {
var self = this;
this.last_domain = domain;
this.last_context = context;
this.last_group_by = group_by;
this.old_search = _.bind(this._super, this);
return self.old_search(new instance.web.CompoundDomain(self.last_domain), self.last_context, self.last_group_by);
},
});
[/quote]
因为我是对一个对象写多个不同的视图,我在图一中这样指定view_id,没有找到,所以我又在下面按图二的方式又指定了一下,按钮就不显示了,请问这个怎么解决??? - 
//修正odoo自定义view_mode不会取得view_id的问题
instance.web.ViewManagerAction.include({
init: function(parent, action) {
var self = this;
if (!action.views[0][0] && action.view_id){
if (action.views[0][1] == action.view_mode){
action.views[0][0] = action.view_id[0]
}
}
return this._super.apply(this, arguments);
}
});
试试这段代码,不知道好用不. - 
[quote author=Smile佳佳 link=topic=14531.msg32198#msg32198 date=1461296520]
[quote author=开阖-静静 link=topic=14531.msg31382#msg31382 date=1447298909]
直接include listview就好了。
instance.web.ListView.include({
load_list: function () {
var self = this;
this._super.apply(this, arguments);
var b_length= self.__parentedParent.$el.find('.ok_bn').length;
if(b_length ==0 && self.model=="res.partner"){ //通过获取this中的对象信息进行分析最终确定在那个地方加按钮。
console.log(self.$buttons[0].firstChild.className!="oe_button ok_bn");
console.log(+b_length<=0+"ok_bnok_bnok_bnok_bnok_bnok_bnok_bn");
var button = $("服务");
root=self.$el.parents();
button.prependTo(root.find('.oe_list_buttons'));
this.$buttons.on('click', '.ok_bn', function() {
self.do_action({
type: 'ir.actions.act_window',
res_model: "product.product",
res_id: 1,
views: [[false, 'form']],
target: 'current',
context:"",
}, {
on_reverse_breadcrumb: function () {
self.reload();
}
});
});
}
},
});
但是不推荐这么做。不符合odoo的思想。有旭哥(http://odoo.nbzx.me) 找到一个一个好方法。
(推荐使用)
<record id="action_purchase_need" model="ir.actions.act_window">
<field name="name">采购 </field>
<field name="res_model">purchase.need</field>
<field name="view_type">form</field>
<field name='view_mode'>tree_purchase_need</field>
<field name="view_id" ref="purchase_need_tree"/>
</record>
instance.web.views.add('tree_purchase_need', 'instance.web.dftg_ext.NeedListView');
instance.web.dftg_ext.NeedListView = instance.web.ListView.extend({
init: function() {
var self = this;
this._super.apply(this, arguments);
this.on('list_view_loaded', this, function() {
if(self.__parentedParent.$el.find('.oe_purchase_need').length == 0){
var button1 = $("<button type='button' class='oe_button oe_purchase_need'>刷新采购需求</button>")
.click(this.proxy('do_refresh_action'));
self.__parentedParent.$el.find('.oe_list_buttons').append(button1);
}
});
},
do_refresh_action: function () {
var self = this;
var action_manager = new instance.web.ActionManager(self);
return action_manager.do_action('dftg_ext.action_purchase_need_refresh_wizard', {
on_close: function () {
self.do_search(self.last_domain, self.last_context, self.last_group_by);
}
});
},
//重载列表数据
do_search: function(domain, context, group_by) {
var self = this;
this.last_domain = domain;
this.last_context = context;
this.last_group_by = group_by;
this.old_search = _.bind(this._super, this);
return self.old_search(new instance.web.CompoundDomain(self.last_domain), self.last_context, self.last_group_by);
},
});
[/quote]
因为我是对一个对象写多个不同的视图,我在图一中这样指定view_id,没有找到,所以我又在下面按图二的方式又指定了一下,按钮就不显示了,请问这个怎么解决???
[/quote]
除了上楼静静的方法,还有一个方法,在action中添加<field name="context">{'tree_view_ref': '包名.视图id'}</field>