代码之家  ›  专栏  ›  技术社区  ›  user2266817

EXTJS单元格编辑使用上下箭头键递增和递减值

  •  0
  • user2266817  · 技术社区  · 1 年前

    在我的应用程序中,当我尝试编辑文本框单元格并按向上箭头或向下箭头时,它会自动递增或递减单元格的值。

    预期行为:当我编辑一个单元格并更改值,然后按向上或向下箭头时,它的行为应该像我按enter键一样,应该向上或向下移动到一个单元格。

    我们是否有任何关键字可以用来调试代码,看看是什么使值递增或递减。提前感谢。

    0 回复  |  直到 1 年前
        1
  •  1
  •   shae    1 年前

    您可以通过以下方式更新逻辑 在按下按键之前 事件!请查看以下示例!我希望这能有所帮助!

    Ext.create('Ext.grid.Panel', {
                renderTo: Ext.getBody(),
                width: 400,
                height: 200,
                title: 'Editable Grid Example',
                store: {
                    fields: ['name', 'age'],
                    data: [{
                        name: 'John',
                        age: 25
                    }, {
                        name: 'Jane',
                        age: 30
                    }, {
                        name: 'Mike',
                        age: 28
                    }]
                },
                columns: [{
                    text: 'Name',
                    dataIndex: 'name',
                    flex: 1,
                    editor: 'textfield'
                }, {
                    text: 'Age',
                    dataIndex: 'age',
                    editor: {
                        xtype: 'numberfield',
                        keyNavEnabled: false,
                    }
                }],
                plugins: {
                    ptype: 'cellediting',
                    clicksToEdit: 1,
                    id: 'celledit'
                },
                
                listeners: {
                beforecellkeydown: function (grid, td, cellIndex, record, tr, rowIndex, event, eOpts) {
                    if (event.getKey() === event.UP || event.getKey() === event.DOWN) {
                        var editingPlugin = this.getPlugin('celledit');
                        if (editingPlugin.editing) {
                            event.stopEvent();
                            var targetRow = event.getKey() === event.UP ? rowIndex - 1 : rowIndex + 1;
                            if (targetRow >= 0 && targetRow < grid.getStore().getCount()) {
                                var targetCell = this.getView().getCellByPosition({
                                    row: targetRow,
                                    column: cellIndex
                                });
                                if (targetCell) {
                                    var originalValue = record.get(this.columns[cellIndex].dataIndex);
                                    var currentValue = editingPlugin.getActiveEditor().getValue();
                                    if (originalValue !== currentValue) {
                                        editingPlugin.completeEdit(); // Complete the current edit
                                        editingPlugin.startEditByPosition({
                                            row: targetRow,
                                            column: cellIndex
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            });