如何在ExtJS的网格视图列中添加按钮?

6
当创建新行时,其中一个字段应包含通过Extended JS动态创建的按钮。
每个按钮应包含不同的名称和操作监听器。列应如图所示。

2
您可以使用actionColumn... http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.grid.ActionColumn - AJJ
2
最新的稳定版本是4.2.2:actionColumn - third_eye
3个回答

17
{
   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);
          }
       }
   ]
}

但它不显示按钮的文本。 - user1572225
3
renderTo必须是DOM元素而不是ID字符串。使用:renderTo: Ext.query("#"+id)[0] - v1r00z
感谢您分享代码。 - AtmiyaDas2014

16
我在(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
                        }
                    }
            }
       }
   ]
}

5
我认为这是在渲染时添加网格列按钮的最佳方法。
{
   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);
            }
       }
   ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接