【问题标题】:Passing rowIndex to a window in extjs 4将rowIndex传递给extjs 4中的窗口
【发布时间】:2013-03-13 09:09:32
【问题描述】:

我有一个商店、一个网格、一个窗口和窗口中的几个文本框。我需要的是,在单击网格中的 actioncolumn 时,我需要获取单击行的 rowIndex 并将其作为参数传递给窗口。在那里,我需要加载商店并获取值并将其设置在适当的文本框中。我不确定如何在单击时将 rowIndex 从网格传递到窗口。文我尝试在testfunction 中提醒a 的值,它出现为undefined。请帮助,下面是我的代码。

Js 文件:

Ext.onReady(function() {
    var rIx;
    Ext.create('Ext.data.Store', {
        storeId:'employeeStore',
        fields:['firstname', 'lastname', 'senority', 'dep', 'hired'],
        data:[
            {firstname:"Michael", lastname:"Scott", senority:7, dep:"Manangement", hired:"01/10/2004"},
            {firstname:"Dwight", lastname:"Schrute", senority:2, dep:"Sales", hired:"04/01/2004"},
            {firstname:"Jim", lastname:"Halpert", senority:3, dep:"Sales", hired:"02/22/2006"},
            {firstname:"Kevin", lastname:"Malone", senority:4, dep:"Accounting", hired:"06/10/2007"},
            {firstname:"Angela", lastname:"Martin", senority:5, dep:"Accounting", hired:"10/21/2008"}
        ]
    });

    Ext.create('Ext.grid.Panel', {
        title: 'Employee Data',
        store: Ext.data.StoreManager.lookup('employeeStore'),
        columns: [
            { text: 'First Name',  dataIndex: 'firstname' },
            {
                header: 'Button',
                xtype: 'actioncolumn',
                icon : 'test.png',
                handler: function(grid, rowIndex, colIndex, item, e , record) {
                    rIx=rowIndex;
                    //var rec = grid.getStore().getAt(rowIndex);
                    //alert("Edit " + rec.get('firstname'));
                    Ext.create('MyWindow').show();
                }
            }

        ],

        width: 500,
        renderTo: Ext.getBody()
    });

    var testFunction = function(a){alert("a= "+a);

         var r = Ext.data.StoreManager.lookup('employeeStore').getAt(a);
         var firstname = r.get('firstname');
         Ext.getCmp('fname').setValue(firstname);   
     };

    Ext.define('MyWindow', {
        extend: 'Ext.window.Window',
        store : Ext.data.StoreManager.lookup('employeeStore'),
        height: 250,
        width: 250,
        title: 'My Window',
        items: [
                     {
                        xtype: 'textfield',           
                        id : 'fname',
                        fieldLabel:'Name'
                     }

                 ],
        listeners: {
                      afterrender: Ext.Function.pass(testFunction, [rIx])
                }
        });

});

【问题讨论】:

    标签: extjs4


    【解决方案1】:

    Ext.Function.pass(testFunction, [rIx]) 在执行pass 时采用rIx 的当前值。在 rIx 被设置为任何有意义的东西之前很久就调用了该方法。 Javascript 是一种按值传递的语言。最终将 rIx 设置为行索引并不重要。此时 Ext.Function.pass 已经执行完毕,传入的参数未定义。

    另一种方法是将 rowIndex 作为属性推送到您的窗口中。

    Ext.create('Ext.grid.Panel', {
        title: 'Employee Data',
        store: Ext.data.StoreManager.lookup('employeeStore'),
        columns: [
            { text: 'First Name',  dataIndex: 'firstname' },
            {
                header: 'Button',
                xtype: 'actioncolumn',
                icon : 'test.png',
                handler: function(grid, rowIndex, colIndex, item, e , record) {
                    Ext.create('MyWindow', {rIx: rowIndex}).show();
                }
            }
    
        ],
        width: 500,
        renderTo: Ext.getBody()
    });
    
    Ext.define('MyWindow', {
        extend: 'Ext.window.Window',
        store : Ext.data.StoreManager.lookup('employeeStore'),
        height: 250,
        width: 250,
        title: 'My Window',
        items: [
            {
                xtype: 'textfield',           
                id : 'fname',
                fieldLabel:'Name'
            }
        ],
        listeners: {
            afterrender: function(win){
                alert("idx= " + win.rIx);
                var r = Ext.data.StoreManager.lookup('employeeStore').getAt(win.rIx);
                var firstname = r.get('firstname');
                Ext.getCmp('fname').setValue(firstname);   
            }
        }
    });
    

    另一种选择是在您的处理函数中设置窗口的afterrender 侦听器,而不是将其添加到窗口的类定义中。在我看来,这种方法更干净一些。我不太喜欢向组件添加不相关的状态属性。

    Ext.create('Ext.grid.Panel', {
        title: 'Employee Data',
        store: Ext.data.StoreManager.lookup('employeeStore'),
        columns: [
            { text: 'First Name',  dataIndex: 'firstname' },
            {
                header: 'Button',
                xtype: 'actioncolumn',
                icon : 'test.png',
                handler: function(grid, rowIndex, colIndex, item, e , record) {
                    var win = Ext.create('MyWindow');
                    // add the listener after to avoid bashing any listener config
                    // that may already exist for the window.
                    win.on('afterrender', function() {
                        // rowIndex is available in the closure
                        alert("idx= " + rowIndex);
                        var r = Ext.data.StoreManager.lookup('employeeStore').getAt(rowIndex);
                        var firstname = r.get('firstname');
                        Ext.getCmp('fname').setValue(firstname);
                    });
                    win.show();
                }
            }
    
        ],
        width: 500,
        renderTo: Ext.getBody()
    });
    

    【讨论】:

    • 您好,Wantok,您指出了我所犯的错误以及正确的程序。非常感谢。我确实需要 2 个说明:1) 使用的 win 是预定义的。 2) 正如我所假设的,win 代表一个窗口,如果我想将 rowIndex 传递给面板,我应该指定什么...
    • afterrender 事件处理程序的第一个参数是刚刚渲染的东西,在本例中是窗口。如果你愿意,可以称它为“cmp”。它只是渲染的组件。您可以以同样的方式将 rIx 属性添加到您喜欢的任何组件中。
    猜你喜欢
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2010-12-16
    • 2010-10-06
    • 2016-10-04
    相关资源
    最近更新 更多