日期:2014-05-16  浏览次数:20356 次

Extjs4.0.7 实现Grid的嵌套

网上相关资料非常少,我看过的大多是Extjs 3.0 急以前版本的解决方案。

比如:http://mikhailstadnik.com/ext/examples/nested-grid.htm  (Extjs3.0版本的)

但是4以后的就没看到了,经自己研究和参考官方网站资料,终于测试完成。现写下来供大家参考学习:

以下为简单的测试数据,如何做到更多功能,就看各位看官的功底了,哈。。。。。

效果图如下:


subgrid2.js文件:

Ext.define('Company', {
    extend: 'Ext.data.Model',
    fields: [
            { name: 'id' },
            { name: 'company' },
            { name: 'price', type: 'float' },
            { name: 'change', type: 'float' },
            { name: 'pctChange', type: 'float' },
            { name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia' },
            { name: 'industry' },
            { name: 'desc' }
         ]
});

var dummyDataForMainGrid = [
        ['1', '3m Co', 71.72, 0.02, 0.03, '9/1 12:00am', 'Manufacturing'],
        ['2', 'Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am', 'Manufacturing'],
        ['3', 'Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am', 'Manufacturing'],
        ['4', 'American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am', 'Finance']
    ];

var mainStore = Ext.create('Ext.data.ArrayStore', {
    model: 'Company',
    data: dummyDataForMainGrid
});




function displayInnerGrid(renderId) {

    //Model for the inside grid store
    Ext.define('TestModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: 'Field1' },
            { name: 'Field2' },
            { name: 'Field3' }
        ]
    });

    //dummy data for the inside grid
    var dummyDataForInsideGrid = [
        ['dummyRow1', 1, 2],
        ['dummyRow2', 1, 2],
        ['dummyRow3', 1, 3]

    ];

    var insideGridStore = Ext.create('Ext.data.ArrayStore', {
        model: 'TestModel',
        data: dummyDataForInsideGrid
    });

    innerGrid = Ext.create('Ext.grid.Panel', {
        store: insideGridStore,
        selModel: {
            selType: 'cellmodel'
        },
        columns: [
            { text: "Column1", dataIndex: 'Field1' },
            { text: "Column2", dataIndex: 'Field2' },
            { text: "Column3", dataIndex: 'Field3' }
        ],
        columnLines: true,
        autoWidth: true,
        autoHeight: true,
        //width: 400,
        //height: 200,
        frame: false,
        iconCls: 'icon-grid',
        renderTo: renderId
    });

    innerGrid.getEl().swallowEvent([
                'mousedown', 'mouseup', 'click',
                'contextmenu', 'mouseover', 'mouseout',
                'dblclick', 'mousemove'
            ]);

}


function destroyInnerGrid(record) {

    var parent = document.getElementById(record.get('id'));
    var child = parent.firstChild;

    while (child) {
        child.parentNode.removeChild(child);
        child = child.nextSibling;
    }

}


Ext.define('MainGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.MainGrid',
    store: mainStore,
    columns: [
            { text: "Company", flex: 1, dataIndex: 'company' },
            { text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price' },
            { text: "Change", dataIndex: 'change' },
            { text: "% Change", dataIndex: 'pctChange' },
            { text: "Last Updated", renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange' }
        ],
    //autoWidth: true,
    selModel: {
        selType: 'cellmodel'
    },
    //autoHeight: true,
    plugins: [{
        ptype: 'rowexpander',
        rowBodyTpl: [
                '<div id="{id}">',
                '</div>'
            ]
    }],
    collapsible: true,
    animCollapse: false,
    title: 'Expander Rows in a Collapsable Grid',
    iconCls: 'icon-grid',
    //renderTo: Ext.getBody()
    initComponent: function () {