【问题标题】:How to add button in Grid view column in extjs?如何在 extjs 的网格视图列中添加按钮?
【发布时间】:2014-01-08 12:18:49
【问题描述】:

创建新行时,一个字段应该包含一个在扩展 JS 中动态创建的按钮。

每个按钮都应该包含不同的名称和动作监听器。 该列应该像图像中给出的那样。

【问题讨论】:

标签: button gridview extjs


【解决方案1】:
{
   xtype: 'gridpanel',
   columns: [
       {text: 'NAME', dataIndex: 'name', width: 100},
       {text: 'SURNAME', dataIndex: 'surname', width: 100},
       {
           text: 'DELETE',
           align: 'center',
           xtype: 'actioncolumn',
           items: [
               {
                  xtype: 'button',
                  text: 'DELETE ME',
                  scale: 'small',
                  handler: function() {
                      alert("Hello World!");
                  }
               }
           ]
       }
   ]
}

下一次尝试:

{
   xtype: 'gridpanel',
   columns: [
       {text: 'NAME', dataIndex: 'name', width: 100},
       {text: 'SURNAME', dataIndex: 'surname', width: 100},
       {
          renderer: function(val,meta,rec) {
             // generate unique id for an element
             var id = Ext.id();
             Ext.defer(function() {
                Ext.widget('button', {
                   renderTo: Ext.query("#"+id)[0],
                   text: 'DELETE',
                   scale: 'small',
                   handler: function() {
                      Ext.Msg.alert("Hello World")
                   }
                });
             }, 50);
             return Ext.String.format('<div id="{0}"></div>', id);
          }
       }
   ]
}

【讨论】:

  • 但它不显示按钮的文本。
  • renderTo 必须是 dom 而不是 id 字符串。使用:renderTo: Ext.query("#"+id)[0]
  • 感谢您分享代码
【解决方案2】:

我将我的矿石放入 (ExtJS 5.1):

{
   xtype: 'gridpanel',
   columns: [
       {text: 'NAME', dataIndex: 'name', width: 100},
       {text: 'SURNAME', dataIndex: 'surname', width: 100},
       {
           text: 'DELETE',
           align: 'center',
           stopSelection: true,
           xtype: 'widgetcolumn',
           widget: {
                  xtype: 'button',
                  _btnText: "myRandomText",
                  defaultBindProperty: null, //important
                  handler: function(widgetColumn) {
                        var record = widgetColumn.getWidgetRecord();
                        console.log("Got data!", record);
                  },
                  listeners: {
                        beforerender: function(widgetColumn){
                            var record = widgetColumn.getWidgetRecord();
                            widgetColumn.setText( widgetColumn._btnText ); //can be mixed with the row data if needed
                        }
                    }
            }
       }
   ]
}

【讨论】:

    【解决方案3】:

    我认为这是在渲染时在网格列中添加按钮的最佳方式

    {
       xtype: 'gridpanel',
       columns: [
           {text: 'name'},
           {
               text: 'add',
               align: 'center',
               renderer: function(value, meta, record) {
                    var id = Ext.id();
                    Ext.defer(function(){
                        new Ext.Button({
                            text: 'add',
                            handler : function(btn, e) {
                                // do whatever you want here
                            }
                        }).render(document.body, id);
                    },50);
                    return Ext.String.format('<div id="{0}"></div>', id);
                }
           }
       ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多