【问题标题】:How to add a button in grid row in Extjs?如何在 Extjs 的网格行中添加一个按钮?
【发布时间】:2018-04-15 16:31:35
【问题描述】:

目前,我正在使用 XML 文件为我的网格填充数据。

我的 XML 文件是这样的

<property>
    <category>Email</category>
    <name>To-address</name>
    <value>abc@xyz.com</value>
</property>   

<property>
    <category>Email</category>
    <name>From-address</name>
    <value>abc@xyz.com</value>
</property>
<property>
    <category>Email</category>
    <name>Email-body</name>
    <value>My Body</value>
</property>
<property>
    <category>Email</category>
    <name>Password</name>
    <value>1234</value>
</property>

我正在像这样填充我的网格:

{
    xtype: 'grid',
    columns: [{
            text: 'Name',
            width: 100,
            dataIndex: 'name'
        }, {
            text: 'Value',
            dataIndex: 'value',
            flex: 1,
            getEditor: function(record) {
                if (record.get('name') === 'password') {
                    return Ext.create('Ext.grid.CellEditor', {
                        field: Ext.create('Ext.form.field.Text', {
                            inputType: 'password'
                        })
                    })
                    else if (record.get('name') === 'Content') {
                        return Ext.create('Ext.grid.column.Action', {
                            width: 50,
                            items: [{
                                iconCls: 'x-fa fa-cog',
                                tooltip: 'Edit',
                                handler: function(grid, rowIndex, colIndex) {
                                    alert('Hello World');
                                }
                            }]
                        });
                    } else {
                        return Ext.create('Ext.grid.CellEditor', {
                            field: Ext.create('Ext.form.field.Text', {})
                        })
                    },
                    renderer: function(value, meta, rec) {
                        if (record.get('name') === 'password') {
                            value = '***';
                        }
                        return value;
                    }

                }
            }
        ],
        selType: 'cellmodel',
        plugins: {
            ptype: 'cellediting',
        }
    }
}

我想在内容行中添加一个按钮作为值,这将弹出一个窗口,以便在单击时向用户添加电子邮件正文。

这是我尝试过的。它能够添加一个处理程序正常工作的按钮,但它隐藏了其他网格值列数据

if (record.get('name') === 'Content') {
    var id = Ext.id();
    Ext.defer(function() {
        Ext.widget('button', {
            renderTo: id,
            text: 'Edit',
            width: 100,
            handler: function(record) {
                alert("Hello world");
            }
        });
    }, 50);
    return Ext.String.format('<div id="{0}"></div>', id);
}

作为初步步骤,我想选中我正在添加内容的操作列的警告框。但它没有出现在行中。

我在这里缺少什么来向网格行添加一个按钮?

【问题讨论】:

  • 你正在用你的grid 方法解决form 问题。如果我是你,我会考虑将 XML 制作成一条记录而不是四条记录,并使用带有字段的表单面板。现在至少会容易一点,从长远来看肯定会容易得多。
  • @Alexander 是的,我同意你的看法。正如我已经开始使用它一样,想知道我在这里缺少什么。无论如何感谢您的建议:)

标签: javascript extjs


【解决方案1】:

编辑器是在列上定义的,在您的情况下,该列的类型为gridcolumn(默认列类型)并通过隐式定义

 text: 'Value',
 dataIndex: 'value',
 flex: 1,

列的函数getEditor() 应该返回一个编辑器组件,其中的字段派生自Ext.form.field.Base。

但是,您有时会从 getEditor 返回一个列组件:

 getEditor: function(record){
     ...
     else if(record.get('name') === 'Content') {
          return Ext.create('Ext.grid.column.Action', {
          ...

这不起作用,因为它是错误的组件类型。此外,您的列具有固定类型,并且您无法使列的单个单元格表现得像不同的列类型,除非手动覆盖渲染器。我没有看到快速解决此问题的方法。您可能会返回一个编辑器,其中包含一个带有触发按钮的文本字段。

但是,我建议您为您的数据使用form,这样更合适。在这种形式中,您可以将按钮放置在任何您想要的位置。

【讨论】:

  • 嗨,是的,我明白了;我实现的另一种方法是用小部件按钮覆盖渲染器。实际上显示按钮,令我惊讶的是它隐藏了我所有的网格值列数据。不知道是什么错误。
  • @AmitNekar 没有看到代码就无法说出你做错了什么。
  • if(record.get('name') === 'Content'){ var id = Ext.id(); Ext.defer(function(){ Ext.widget('button', { renderTo: id, text: 'Edit', wdth: 100, handler:function(record) { alert("Hello world"); } }); }, 50); return Ext.String.format('
    ', id);这是我尝试过的。它能够添加一个处理程序正常工作的按钮,但它隐藏了其他网格值列数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 2019-09-03
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多