user1477388
user1477388

Reputation: 21430

How to access plugin in ExtJs MVC

I am trying to add a toolbar to my grid with an "Add New Record" button.

The code example provided by Sencha is here:

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: '[email protected]',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

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

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

I am having trouble applying this to my code since I use the MVC pattern. This is my code:

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: '[email protected]',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

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

How can I access the "rowEditing" plugin so as to call it's required methods?

Upvotes: 1

Views: 3775

Answers (1)

Rob Boerman
Rob Boerman

Reputation: 2168

the handler from the buttons gets a reference to the button as a first argument. You can use that reference with a componentquery to get a reference to your grid panel that houses it. The gridPanel has a getPlugin method with which you can fetch a plugin based on a pluginId.The only thing you have to make sure of is to give the plugin a pluginId, otherwise the Grid cannot find it. This should work:

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: '[email protected]',
                    start: new Date(),
                    salary: 50000,
                    active: true
                });

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

Cheers, Rob

EDIT: added complete example: - Go to http://docs.sencha.com/extjs/4.2.2/extjs-build/examples/grid/row-editing.html - Open the javascript console - Paste the below code in there

It will create a second grid with a button that finds the row editing plugin and cancels your edit. doubleclick a row to start editing, click the button in the tbar to cancel it. Works like a charm. Make sure you have specified the pluginId, otherwise the grid's getPlugin method cannot find it.

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

Upvotes: 3

Related Questions