state events

by Johan Vandeplas

HTML

<script src="http://cdn.sencha.com/ext/gpl/4.2.1/ext-all-dev.js"></script>
<link rel="stylesheet" href="http://cdn.sencha.com/ext/gpl/4.2.1/resources/css/ext-all.css">

JavaScript

Ext.create('Ext.data.Store', {
    storeId: 'simpsonsStore',
    fields: ['name', 'email', 'phone'],
    data: {
        'items': [{
            'name': 'Lisa',
                "email": "[email protected]",
                "phone": "555-111-1224"
        }, {
            'name': 'Bart',
                "email": "[email protected]",
                "phone": "555-222-1234"
        }, {
            'name': 'Homer',
                "email": "[email protected]",
                "phone": "555-222-1244"
        }, {
            'name': 'Marge',
                "email": "[email protected]",
                "phone": "555-222-1254"
        }]
    },
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'items'
        }
    }
});

var grid = Ext.create('Ext.grid.Panel', {
    title: 'Simpsons',
    store: Ext.data.StoreManager.lookup('simpsonsStore'),
    columns: [{
        text: 'Name',
        dataIndex: 'name'
    }, {
        text: 'Email',
        dataIndex: 'email',
        flex: 1
    }, {
        text: 'Phone',
        dataIndex: 'phone'
    }],
    buttons: [{
        text: 'Restore Column Layout',
        handler: function () {
            grid.applyState(states[grid.stateId][2]);
        }
    }],
    stateful: true,
    stateId: 'invpm',
    stateEvents: ['columnresize', 'columnmove', 'columnhide', 'columnshow', 'sortchange', 'filterchange'],
    height: 200,
    width: 400,
    renderTo: Ext.getBody()
});

grid.on('statesave', function (grid, state) {
    window.grid = grid;
    console.log('add state - ', grid.stateId, '-', state);
    var states = window.states || (window.states = {});
    if (!Ext.isEmpty(grid.stateId)) {
        if (Ext.isArray(states[grid.stateId])) {
            states[grid.stateId].push(state);
        } else {
            states[grid.stateId] = [state];
        }
    }
});