【问题标题】:ExtJS 4 RowEditing disable edit on one column based on recordExtJS 4 RowEditing 禁用基于记录的一列编辑
【发布时间】:2021-09-16 21:14:42
【问题描述】:

我正在实现一个网格面板,其中最后四列可对大多数行进行编辑。问题是我希望能够禁用编辑,假设第一个 if record.get('status') = 4 已完成,并且只有两列应该是可编辑的。

有没有办法禁止显示这些行的编辑?我可以使用 CellEditing 来做到这一点,但我想继续使用 RowEditing 插件。

问候, 克里斯蒂安

【问题讨论】:

    标签: grid extjs4


    【解决方案1】:

    使用beforeedit事件:

    grid.on('beforeedit', function(editor, e) {
      if (e.colIdx === 0 && e.record.get('status') == 4)
        return false;
    });
    

    更新
    上述解决方案不适用于 rowEditor。
    但是,您可以在 beforeedit 上禁用所需的字段。为此,您应该能够访问行编辑插件。将 pluginId 分配给插件:

    plugins: [
        Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToEdit: 1,
            pluginId: 'rowEditing'
        })
    ],
    

    如果满足某些条件,现在只需禁用所需字段:

    grid.on('beforeedit', function(editor, e) {
        if (e.record.get('status') === 4 ){
             grid.getPlugin('rowEditing').editor.form.findField('fieldToDisable').disable();
        }
        else{
            grid.getPlugin('rowEditing').editor.form.findField('fieldToDisable').enable();
       });
    

    这里是demo(尝试编辑第一行)。

    编辑

    如果上面的 JSFiddle 不起作用,试试its updated version

    【讨论】:

    • 是的,这适用于 CellEditing。对于 RowEditing,它只会每行而不是每列触发一次 beforeedit。
    • 太棒了!像魅力一样工作。试图抓住表单域,但我错过了 findField 函数。谢谢!
    • 就我而言,它是第一个解决方案,因为我的要求是禁用对操作列单击的编辑。谢谢..
    • 2011 年的问题并帮助我到今天。感谢所有参与的人
    • @Lawrence,我怀疑简单的配置编辑会解决 OP 的问题。尤其是这部分I'd like to be able to disable editing on, let's say the first one if record.get('status') = 4。但是,如果您解决了它,只需将其作为答案发布,以便对其他人有所帮助。
    【解决方案2】:

    (基于前面的例子)另一种方法是配置编辑器的beforeedit监听器:

    listeners: {
        beforeedit: function(editor, context) {
            var form   = editor.getEditor().form;
            var field  = form.findField('column_name');
            var status = parseInt(context.record.data.status);
            if (status === 4) {field.disable();} else {field.enable();}
        }
    }
    

    【讨论】:

    • 这是@Molecular Man answser的重复
    • @alexandre1985 这不是因为监听器的定义完全不同;请在投票前了解Ext.define
    • @alexandre1985 请首先学习阅读/写作和理解 - 以及编写 ExtJS 脚本;现在我已经否决了你的答案......因为它清楚地表明你对你在说什么没有多少线索......而且我不是唯一一个有这种观点的人:)
    【解决方案3】:

    由于@Molecular Man 的回答使禁用的列在编辑行编辑时看起来有点有趣,所以我想到了另一种看起来很完美的方法。 您所要做的就是创建一个函数,例如:

    function fieldFormat() {
        if(isGuest) {
            return null; //is not editable
        } else {
            //how you want the column's field config to be formated
            var json = Ext.JSON.decode("{xtype: 'textfield',maxLength: 40}");
            return json;
        }
    }
    

    在网格中,你可以这样写:

    var grid = Ext.create('Ext.grid.Panel', {
        plugins: [grid_rowEditing],
        store: store,
        columns: [
        {
            text     : 'Name',
            dataIndex: 'name',
            field    : fieldFormat()
        }]
    });
    

    isGuest 为真时,“名称”字段将不可编辑。当它为假时,它将是可编辑的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多