簡體   English   中英

如何訪問ExtJs MVC中的插件

[英]How to access plugin in ExtJs MVC

我正在嘗試使用“添加新記錄”按鈕向我的網格添加工具欄。

Sencha提供的代碼示例如下:

var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
        clicksToMoveEditor: 1,
        autoCancel: false
    });

    // create the grid and specify what field you want
    // to use for the editor at each column.
    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        tbar: [{
            text: 'Add Employee',
            iconCls: 'employee-add',
            handler : function() {
                rowEditing.cancelEdit();

                // Create a model instance
                var r = Ext.create('Employee', {
                    name: 'New Guy',
                    email: 'new@sencha-test.com',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

                store.insert(0, r);
                rowEditing.startEdit(0, 0);
            }
        }],
        //etc...
    });

示例: http//dev.sencha.com/deploy/ext-4.1.0-gpl/examples/grid/row-editing.html

因為我使用MVC模式,所以我無法將其應用於我的代碼。 這是我的代碼:

Ext.define('RateManagement.view.Grids.AirShipmentGrid', {
    extend: 'Ext.grid.Panel',
    xtype: 'AirShipmentGrid',
    plugins: [
        {
            clicksToMoveEditor: 1,
            autoCancel: false,
            ptype: 'rowediting'
        },
        'bufferedrenderer'
    ],
    tbar: [{
            text: 'Add Rate',
            //iconCls: 'rate-add',
            handler : function() {
                rowEditing.cancelEdit(); // rowEditing is not defined...

                // Create a model instance
                var r = Ext.create('Employee', {
                    name: 'New Guy',
                    email: 'new@sencha-test.com',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

                store.insert(0, r);
                rowEditing.startEdit(0, 0); // rowEditing is not defined...
            }
        }],
    // etc...
});

如何訪問“rowEditing”插件以調用它所需的方法?

按鈕中的處理程序獲取對按鈕的引用作為第一個參數。 您可以將該引用與componentquery一起使用,以獲取對包含它的網格面板的引用。 gridPanel有一個getPlugin方法,您可以使用該方法根據pluginId獲取插件。您唯一需要確保的是給插件一個pluginId,否則Grid無法找到它。 這應該工作:

Ext.define('RateManagement.view.Grids.AirShipmentGrid', {
    extend: 'Ext.grid.Panel',
    xtype: 'AirShipmentGrid',
    plugins: [
        {
            clicksToMoveEditor: 1,
            autoCancel: false,
            ptype: 'rowediting',
            pluginId: 'rowediting'
        },
        'bufferedrenderer'
    ],
    tbar: [{
            text: 'Add Rate',
            //iconCls: 'rate-add',
            handler : function(button) {
                var grid = button.up('gridpanel');
                var rowEditing = grid.getPlugin('rowediting');
                rowEditing.cancelEdit(); // rowEditing is now defined... :)

                // Create a model instance
                var r = Ext.create('Employee', {
                    name: 'New Guy',
                    email: 'new@sencha-test.com',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

                store.insert(0, r);
                rowEditing.startEdit(0, 0); // rowEditing is not defined...
            }
        }],
    // etc...
});

干杯,羅布

編輯:添加完整示例: - 轉到http://docs.sencha.com/extjs/4.2.2/extjs-build/examples/grid/row-editing.html - 打開javascript控制台 - 將以下代碼粘貼到那里

它將創建第二個網格,其中包含一個按鈕,用於查找行編輯插件並取消編輯。 雙擊一行開始編輯,單擊tbar中的按鈕取消它。 奇跡般有效。 確保已指定pluginId,否則網格的getPlugin方法無法找到它。

Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*',
    'Ext.form.*'
]);

Ext.onReady(function() {
    // Define our data model
    Ext.define('Employee', {
        extend: 'Ext.data.Model',
        fields: [
            'name',
            'email', {
                name: 'start',
                type: 'date',
                dateFormat: 'n/j/Y'
            }, {
                name: 'salary',
                type: 'float'
            }, {
                name: 'active',
                type: 'bool'
            }
        ]
    });

    // Generate mock employee data
    var data = (function() {
        var lasts = ['Jones', 'Smith', 'Lee', 'Wilson', 'Black', 'Williams', 'Lewis', 'Johnson', 'Foot', 'Little', 'Vee', 'Train', 'Hot', 'Mutt'],
            firsts = ['Fred', 'Julie', 'Bill', 'Ted', 'Jack', 'John', 'Mark', 'Mike', 'Chris', 'Bob', 'Travis', 'Kelly', 'Sara'],
            lastLen = lasts.length,
            firstLen = firsts.length,
            usedNames = {},
            data = [],
            s = new Date(2007, 0, 1),
            eDate = Ext.Date,
            now = new Date(),
            getRandomInt = Ext.Number.randomInt,

            generateName = function() {
                var name = firsts[getRandomInt(0, firstLen - 1)] + ' ' + lasts[getRandomInt(0, lastLen - 1)];
                if (usedNames[name]) {
                    return generateName();
                }
                usedNames[name] = true;
                return name;
            };

        while (s.getTime() < now.getTime()) {
            var ecount = getRandomInt(0, 3);
            for (var i = 0; i < ecount; i++) {
                var name = generateName();
                data.push({
                    start: eDate.add(eDate.clearTime(s, true), eDate.DAY, getRandomInt(0, 27)),
                    name: name,
                    email: name.toLowerCase().replace(' ', '.') + '@sencha-test.com',
                    active: getRandomInt(0, 1),
                    salary: Math.floor(getRandomInt(35000, 85000) / 1000) * 1000
                });
            }
            s = eDate.add(s, eDate.MONTH, 1);
        }

        return data;
    })();

    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        // destroy the store if the grid is destroyed
        autoDestroy: true,
        model: 'Employee',
        proxy: {
            type: 'memory'
        },
        data: data,
        sorters: [{
            property: 'start',
            direction: 'ASC'
        }]
    });

    // create the grid and specify what field you want
    // to use for the editor at each column.
    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [{
            header: 'Name',
            dataIndex: 'name',
            flex: 1,
            editor: {
                // defaults to textfield if no xtype is supplied
                allowBlank: false
            }
        }, {
            header: 'Email',
            dataIndex: 'email',
            width: 160,
            editor: {
                allowBlank: false,
                vtype: 'email'
            }
        }, {
            xtype: 'datecolumn',
            header: 'Start Date',
            dataIndex: 'start',
            width: 105,
            editor: {
                xtype: 'datefield',
                allowBlank: false,
                format: 'm/d/Y',
                minValue: '01/01/2006',
                minText: 'Cannot have a start date before the company existed!',
                maxValue: Ext.Date.format(new Date(), 'm/d/Y')
            }
        }, {
            xtype: 'numbercolumn',
            header: 'Salary',
            dataIndex: 'salary',
            format: '$0,0',
            width: 90,
            editor: {
                xtype: 'numberfield',
                allowBlank: false,
                minValue: 1,
                maxValue: 150000
            }
        }, {
            xtype: 'checkcolumn',
            header: 'Active?',
            dataIndex: 'active',
            width: 60,
            editor: {
                xtype: 'checkbox',
                cls: 'x-grid-checkheader-editor'
            }
        }],
        renderTo: 'editor-grid',
        width: 600,
        height: 400,
        title: 'Employee Salaries',
        frame: true,
        tbar: [{
            text: 'Cancel editing Plugin',
            handler: function(button) {
                var myGrid = button.up('grid');
                var myPlugin = myGrid.getPlugin('rowediting')

                myPlugin.cancelEdit();
                console.log(myPlugin);
            }
        }],
        plugins: [{
            clicksToMoveEditor: 1,
            autoCancel: false,
            ptype: 'rowediting',
            pluginId: 'rowediting'
        }]
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM