Javascript ExtJS 4:grid:cell编辑:自动编辑功能所需的建议和帮助

Javascript ExtJS 4:grid:cell编辑:自动编辑功能所需的建议和帮助,javascript,extjs,extjs4,Javascript,Extjs,Extjs4,我搜索了ExtJS相关的问题,并没有找到任何参考资料,但若我错过了,很抱歉提前提出了重复的问题 我想就如何制作ExtJS 4 grid:cell editing:auto edit功能寻求一些帮助——我的意思是,当我按下一个键时,我想进入单元格编辑模式(例如,在突出显示的单元格中按“123”,文本将被替换(如果有)为“123”)。此时,按ENTER键或用鼠标单击即可进入单元格编辑模式 作为基础,我使用Sencha提供的示例: 任何提示和建议都将不胜感激 提前感谢!:) 事实上,我确实部分解决了我

我搜索了ExtJS相关的问题,并没有找到任何参考资料,但若我错过了,很抱歉提前提出了重复的问题

我想就如何制作ExtJS 4 grid:cell editing:auto edit功能寻求一些帮助——我的意思是,当我按下一个键时,我想进入单元格编辑模式(例如,在突出显示的单元格中按“123”,文本将被替换(如果有)为“123”)。此时,按ENTER键或用鼠标单击即可进入单元格编辑模式

作为基础,我使用Sencha提供的示例:

任何提示和建议都将不胜感激

提前感谢!:)

事实上,我确实部分解决了我的问题。找到一种方法使单元格在按键时可编辑,将selectOnFocus配置参数用于在单元格中选择文本,现在我需要在单元格中插入第一个字符(启动编辑模式)

这可能不是最漂亮的解决方案,但对我来说很有效:) 这是到目前为止的完整代码

var tStore = Ext.create('Ext.data.Store', {
    storeId:'simpsonsStore',
    fields:['name', 'email', 'phone'],
    data:{'items':[
        {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
        {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
        {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},
        {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}
    ]},
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});

var tCellEdit = Ext.create('Ext.grid.plugin.CellEditing', {
    clicksToEdit: 1
});

var tGrid = Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    store: tStore,
    columns: [
        {header: 'Name',  dataIndex: 'name', field: 'textfield'},
        {header: 'Email', dataIndex: 'email', flex:1,
            editor: {
                xtype:'textfield',
                allowBlank:false,
                selectOnFocus: true
            }
        },
        {header: 'Phone', dataIndex: 'phone'}
    ],
    selType: 'cellmodel',
    plugins: [tCellEdit],
    listeners: {
        keypress: {
            element: 'el',
            fn: function(iEvent, iElement) {
                iCode = iEvent.getKey();
                if (iCode != undefined && iCode != iEvent.LEFT && iCode != iEvent.RIGHT && iCode != iEvent.UP && iCode != iEvent.DOWN && iCode != iEvent.ENTER && iCode != iEvent.ESC) {
                    var iView = tGrid.getView();
                    var position = iView.selModel.position;

                    tCellEdit.startEditByPosition(position);
                }
            }
        }
    },
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});

很抱歉耽搁了这么长时间,但让我们说一下,我在度假,坐在海边,喝着莫吉托酒……思考着生活、土豆以及我对即将到来的grid wise项目的真正需求。我总结了以下几点:

  • 因为在我的表格里,人们会写数字。我需要通过在当前单元格中按数字来进入编辑模式
  • 我需要按下数字键,不仅激活编辑模式,而且将其作为新值插入(所以按键盘上的1,单元格将进入编辑模式并将1作为新值)
  • 我需要让ESC像往常一样进入魔术
  • 总的来说,我对Ext.core.Element(修复在Windows7上使用IE9和Firefox 6.0.2出现的奇怪错误。有关更多详细信息,请参阅代码中的注释。)、Ext.grid.plugin.Editing(通过按数字键进入编辑模式)和Ext.Editor(设置新值)

    TODO:在编辑模式下,按ENTER键不仅可以完成编辑,如果有单元格,还可以向下移动一个(类似于Excel)

    对不起,我的英语…不是我的母语,但希望它或多或少是可以理解的。另外,感谢您的意见和建议!;)


    您是否可以确认希望“网格编辑器”字段的所有文本都选择在焦点上?类似于链接到此处但在网格中的页面上的操作?是的,这就是我的想法。在我看来,我可以通过使用config param selectOnFocus:True来做我想做的事情。你应该将上面的内容移动到一个答案-如果没有人有更好的答案,请接受你自己的答案,并将其移出“无答案”栏。:)这是一个在extjs6.5Classic中运行的更新版本。
    /**
    * Fix for bug with cursor position in editor grid textfield
    *
    * Bug description: after loading an editor grid which contains a textfield, the cursor / caret is positioned at the
    * beginning of text field the first time the editor is activated. Subsequent activations position the caret at the end
    * of the text field.
    * In my case this behavior is not observed in Opera 11.51 (Windows 7) and IE8, Firefox 6.0.2 (Windows XP), but observed in IE9 and Firefox 6.0.2 (Windows 7)
    *
    * Current fix helps with IE9 problem, but Firefox 6.0.2 (Windows 7) still putting the cursor / caret at the beginning of text field.
    *
    * Forum post for ExtJS v3 about same problem and where the fix was found: http://www.sencha.com/forum/showthread.php?88046-OPEN-3.1-Caret-Cursor-Position-in-Editor-Grid-Textfield
    */
    Ext.core.Element.prototype.focus = function(defer, /* private */dom) {
        var me = this,
            dom = dom || me.dom;
        try {
            if (Number(defer)) {
                Ext.defer(me.focus, defer, null, [null, dom]);
            } else {
                dom.focus();
                // start override
                dom.value = dom.value;
                dom.focus();
                if (dom.sof) {
                    dom.select();
                }
                // end override
            }
        } catch (e) { }
        return me;
    };
    /**
    * END OF ALL FIXES
    */
    
    var tStore = Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['name', 'email', 'phone'],
        data:{'items':[
            {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
            {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
            {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},
            {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    Ext.onReady(function() {
    
        var newValue = '';
    
        /**
         *  Rewriting class Ext.grid.pluging.Editing to make cell go into edit mode by pressing numeric keys.
         *
         * changed: requirements: Ext.util.KeyNav -> Ext.util.KeyMap
         * changed: accordingly made changes to use Ext.util.KeyMap in initEditTriggers function
         * added: new function onNumberKey for replacing original value with new one and entering cell in edit mode
         *
         * tested only for Cell selection model, too lazy for Row selection model testing :P
         */
        Ext.override(Ext.grid.plugin.Editing, {
    
            requires: [
                'Ext.grid.column.Column',
                'Ext.util.KeyMap'
            ],
    
            initEditTriggers: function() {
                var me = this,
                    view = me.view,
                    clickEvent = me.clicksToEdit === 1 ? 'click' : 'dblclick';
    
                // Start editing
                me.mon(view, 'cell' + clickEvent, me.startEditByClick, me);
                view.on('render', function() {
                    me.keyNav = Ext.create('Ext.util.KeyMap', view.el, [
                        {
                            key: [48, 49, 50, 51, 52, 53, 54, 55, 56, 57],  // 0123456789
                            fn: me.onNumberKey,
                            scope: me
                        }, {
                            key: 13,    // ENTER
                            fn: me.onEnterKey,
                            scope: me
                        }, {
                            key: 27,    // ESC
                            fn: me.onEscKey,
                            scope: me
                        }
                    ]);
                }, me, { single: true });
            },
    
            onNumberKey: function(e) {
                var me = this,
                    grid = me.grid,
                    selModel = grid.getSelectionModel(),
                    record,
                    columnHeader = grid.headerCt.getHeaderAtIndex(0);
    
                // Calculate editing start position from SelectionModel
                // CellSelectionModel
                if (selModel.getCurrentPosition) {
                    pos = selModel.getCurrentPosition();
                    record = grid.store.getAt(pos.row);
                    columnHeader = grid.headerCt.getHeaderAtIndex(pos.column);
                }
                // RowSelectionModel
                else {
                    record = selModel.getLastSelected();
                }
    
                // if current cell have editor, then save numeric key in global variable
                ed = me.getEditor(record, columnHeader);
                if (ed) {
                    newValue = String.fromCharCode(e);
                }
    
                // start cell edit mode
                me.startEdit(record, columnHeader);
            }
    });
    
        Ext.override(Ext.Editor, {
            startEdit : function(el, value) {
                var me = this,
                    field = me.field;
    
                me.completeEdit();
                me.boundEl = Ext.get(el);
                value = Ext.isDefined(value) ? value : me.boundEl.dom.innerHTML;
    
                if (!me.rendered) {
                    me.render(me.parentEl || document.body);
                }
    
                if (me.fireEvent('beforestartedit', me, me.boundEl, value) !== false) {
                    me.startValue = value;
                    me.show();
                    field.reset();
                    field.setValue((newValue != '' ? newValue : value));
                    me.realign(true);
                    field.focus(false, 10);
                    if (field.autoSize) {
                        field.autoSize();
                    }
                    me.editing = true;
    
                    // reset global newValue
                    newValue = '';
                }
            }
        });
        /**
         *  END OF ALL OVERRIDES (thank god!) :)
         */
    
    
        var tCellEdit = Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        });
    
        var tGrid = Ext.create('Ext.grid.Panel', {
            title: 'Simpsons',
            store: tStore,
            columns: [
                {header: 'Name',  dataIndex: 'name',
                    editor: {
                        xtype: 'textfield',
                        maskRe: /[\d]/
                    }
                },
                {header: 'Email', dataIndex: 'email', flex:1,
                    editor: {
                        xtype:'textfield'
                    }
                },
                {header: 'Phone', dataIndex: 'phone'}
            ],
            selType: 'cellmodel',
            plugins: [tCellEdit],
            height: 200,
            width: 400,
            renderTo: 'testgrid'
        });
    });