跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. 在listview的web视图上增加一个按钮的问题

在listview的web视图上增加一个按钮的问题

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
14 帖子 8 发布者 14.1k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • KevinKongK 离线
    KevinKongK 离线
    KevinKong
    写于 最后由 编辑
    #5

    拿到里面来嘛~

    <br />&lt;templates&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;t t-extend=&quot;ListView.buttons&quot;&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;t t-jquery=&quot;span.oe_alternative&quot; t-operation=&quot;append&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;t t-if=&quot;widget.options.import_lead_enable !== false&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class=&quot;oe_fade&quot;&gt;or&lt;/span&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;a href=&quot;#&quot; class=&quot;oe_bold oe_list_button_import_lead&quot;&gt;Import Lead&lt;/a&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/t&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/t&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/t&gt;<br />&nbsp; &nbsp;  &lt;/t&gt;<br />&lt;/templates&gt;<br />
    
    1 条回复 最后回复
    0
    • L 离线
      L 离线
      lipeng260
      写于 最后由 编辑
      #6

      我自己写了一下测试了,找到解决方法了,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 />&nbsp; &nbsp; var _t = instance.web._t,<br />&nbsp; &nbsp; &nbsp; &nbsp; _lt = instance.web._lt;<br />&nbsp; &nbsp; var QWeb = instance.web.qweb;<br /><br />&nbsp; &nbsp; instance.add_button = {};<br /><br />&nbsp; &nbsp; var MODELS_NOT_TO_HIDE = &#91;&#039;crm.lead&#039;];<br />&nbsp; &nbsp; instance.web.ListView.include({<br />&nbsp; &nbsp; &nbsp; &nbsp; start: function() {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.options.my_btn = true;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.$el.addClass(&#039;oe_list&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this._super();<br />&nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; });<br />}
      


      xml代码:

      &nbsp; &nbsp; &lt;t t-extend=&quot;ListView.buttons&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;t t-jquery=&quot;span.oe_alternative&quot; t-operation=&quot;after&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;t t-if=&quot;widget.options.my_btn&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;span class=&quot;oe_fade&quot;&gt;or&lt;/span&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;a href=&quot;#&quot; class=&quot;oe_bold oe_list_button_import_lead&quot;&gt;Import Lead&lt;/a&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/t&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/t&gt;<br />&nbsp; &nbsp; &lt;/t&gt;
      
      1 条回复 最后回复
      0
      • L 离线
        L 离线
        lipeng260
        写于 最后由 编辑
        #7

        还有一个问题就是,这样写全局不好吧,我想能不能在你需要添加页面,重载js文件,只修改本页面,不修改全局,请教。

        1 条回复 最后回复
        0
        • 静静静 离线
          静静静 离线
          静静
          写于 最后由 编辑
          #8

          直接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);
                  },
              });

          1 条回复 最后回复
          0
          • KevinKongK 离线
            KevinKongK 离线
            KevinKong
            写于 最后由 编辑
            #9

            [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]
              😉 静静 你是男是女?

            1 条回复 最后回复
            0
            • wjfonhandW 离线
              wjfonhandW 离线
              wjfonhand
              写于 最后由 编辑
              #10

              我也想静静

              GoodERP -- Odoo China fork

              1 条回复 最后回复
              0
              • L 离线
                L 离线
                lipeng260
                写于 最后由 编辑
                #11

                静静的方法学习了。。

                1 条回复 最后回复
                0
                • 佳先生佳 离线
                  佳先生佳 离线
                  佳先生
                  写于 最后由 编辑
                  #12

                  [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,没有找到,所以我又在下面按图二的方式又指定了一下,按钮就不显示了,请问这个怎么解决???

                  1 条回复 最后回复
                  0
                  • 静静静 离线
                    静静静 离线
                    静静
                    写于 最后由 编辑
                    #13

                        //修正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);
                            }
                        });


                    试试这段代码,不知道好用不.

                    1 条回复 最后回复
                    0
                    • 萧 离线
                      萧 离线
                      萧云飞
                      写于 最后由 编辑
                      #14

                      [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>

                      1 条回复 最后回复
                      0

                      • 登录

                      • 没有帐号? 注册

                      • 登录或注册以进行搜索。
                      • 第一个帖子
                        最后一个帖子
                      0
                      • 版块
                      • 标签
                      • 热门
                      • 用户
                      • 群组