草庐IT

javascript - ExtJS 4 : grid: cell editing: auto edit feature 需要的建议和帮助

coder 2024-05-06 原文

我搜索了与 ExtJS 相关的问题,但没有找到任何引用,但如果我错过了,请提前提出重复问题。

我想寻求一些关于如何制作 ExtJS 4 网格的帮助:单元格编辑:自动编辑功能——我的意思是,我想在按下一个键时进入单元格编辑模式(例如,通过按“123” 在突出显示的单元格中,文本将替换(如果有的话)为“123”)。目前可以通过按回车键或点击鼠标进入单元格编辑模式。

作为基础,我正在使用 Sencha 提供的示例:http://dev.sencha.com/deploy/ext-4.0.2a/examples/grid/cell-editing.html

如有任何提示和指点,我们将不胜感激。

提前致谢! :)

实际上我确实部分解决了我的问题。找到了一种使单元格在按键时可编辑的方法,将 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()
});

最佳答案

抱歉耽搁了这么久,但我只想说我正在度假,坐在海边喝着莫吉托酒……思考着生活、土 bean 以及我对即将到来的网格项目真正需要的东西。我得出以下几点结论:

  1. 因为在我的网格中人们会写数字。我需要专注于通过按当前单元格中的数字进入编辑模式。
  2. 我需要按下数字键,不仅激活编辑模式,而且将其作为新值插入(因此通过在键盘上按 1,单元格进入编辑模式并将 1 作为新值)
  3. 我需要让 ESC 和 ENTER 魔法像往常一样工作。

总的来说,我对 Ext.core.Element 进行了一些覆盖(以修复在 Windows 7 上使用 IE9 和 Firefox 6.0.2 时出现的奇怪错误。有关更多详细信息,请参阅代码中的注释。),Ext.grid.plugin。编辑(通过按数字键进入编辑模式)和 Ext.Editor(设置新值)。

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

附言。对不起我的英语……不是我的母语,但希望它或多或少是可以理解的。另外,感谢您的输入和评论! ;)

/**
* 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'
    });
});

关于javascript - ExtJS 4 : grid: cell editing: auto edit feature 需要的建议和帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7026744/

有关javascript - ExtJS 4 : grid: cell editing: auto edit feature 需要的建议和帮助的更多相关文章

  1. ruby-on-rails - 一般建议和推荐的文件夹结构 - Sinatra - 2

    您将如何构建一个简单的Sinatra应用程序?我正在制作,我希望该应用具有以下功能:“应用程序”更像是一个包含所有信息的管理仪表板。然后另一个应用程序将通过REST访问信息。我还没有创建仪表板,只是从数据库中获取东西session和身份验证(尚未实现)您可以上传图片,其他应用可以显示这些图片我已经使用RSpec创建了一个测试文件通过Prawn生成报告目前的设置是这样的:app.rbtest_app.rb因为我实际上只有应用程序和测试文件。到目前为止,我已经将Datamapper用于ORM,将SQLite用于数据库。这是我的第一个Ruby/Sinatra项目,所以欢迎任何和所有建议-我应

  2. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  3. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

  4. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  5. ruby-on-rails - 我将 Rails3 与 tinymce 一起使用。如何呈现用户关闭浏览器javascript然后输入xss? - 2

    我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如

  6. ruby - 使用 Selenium WebDriver 启用/禁用 javascript - 2

    出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=

  7. ruby - Watir-Webdriver 是否支持点击目标为 javascript 的链接? - 2

    我是Ruby和Watir-Webdriver的新手。我有一套用VBScript编写的站点自动化程序,我想将其转换为Ruby/Watir,因为我现在必须支持Firefox。我发现我真的很喜欢Ruby,而且我正在研究Watir,但我已经花了一周时间试图让Webdriver显示我的登录屏幕。该站点以带有“我同意”区域的“警告屏幕”开头。用户点击我同意并显示登录屏幕。我需要单击该区域以显示登录屏幕(这是同一页面,实际上是一个表单,只是隐藏了)。我整天都在用VBScript这样做:objExplorer.Document.GetElementsByTagName("area")(0).click

  8. 网页设计期末作业,基于HTML+CSS+JavaScript超酷超炫的汽车类企业网站(6页) - 2

    🎉精彩专栏推荐💭文末获取联系✍️作者简介:一个热爱把逻辑思维转变为代码的技术博主💂作者主页:【主页——🚀获取更多优质源码】🎓web前端期末大作业:【📚毕设项目精品实战案例(1000套)】🧡程序员有趣的告白方式:【💌HTML七夕情人节表白网页制作(110套)】🌎超炫酷的Echarts大屏可视化源码:【🔰Echarts大屏展示大数据平台可视化(150套)】🔖HTML+CSS+JS实例代码:【🗂️5000套HTML+CSS+JS实例代码(炫酷代码)继续更新中…】🎁免费且实用的WEB前端学习指南:【📂web前端零基础到高级学习视频教程120G干货分享】🥇关于作者:💬历任研发工程师,技术组长,教学总监;

  9. ruby-on-rails - 在页面的最底部包含 javascript 文件 - 2

    我有一个Rails应用程序。还有一个javascript(javascript1.js)文件必须包含在每个View的最底部。我把它放在/assets/javascripts文件夹中。Application.js包含以下代码//=requirejquery//=requirejquery_ujs//=someotherfiles//=require_directory.即使Application.js中不包含javascript1.js,它也会自动包含,不是吗?那么我怎样才能做我想做的事呢? 最佳答案 单独定义、包含和执行您的java

  10. ruby-on-rails - 为 rails 中的 javascript 生成完整的 url(类似于 javascript_path,但是是 url) - 2

    如何生成指向javascript文件的绝对链接。我想应该有类似下面的东西(不幸的是它似乎不可用):javascript_url'main'#->'http://localhost:3000/javascripts/main.js'代替:javascript_path'main'#->'/javascripts/main.js'我需要绝对URL,因为该javascript文件将用于书签。另外我需要相同的css文件。谢谢,德米特里。 最佳答案 javascript和css文件的绝对URL现在在Rails4中可用ActionView::H

随机推荐