双击后在网格面板中对值进行分组

时间:2013-08-09 22:07:40

标签: javascript json extjs4.2

我想在网格面板中对值进行分组

以下是代码:

var store = Ext.create('Ext.data.TreeStore', {
root: {
    expanded: true,
    children: [
        { text: "School Friends", expanded: true, children: [
            { text: "Mike", leaf: true, name: "Mike", email: "mike@stackoverflow.com", phone: "345-2222"},
            { text: "Laura", leaf: true, name: "Laura", email: "laura@stackoverflow.com", phone: "345-3333"}
        ] },
        { text: "Facebook Friend", expanded: true, children: [
            { text: "Steve", leaf: true, name: "Steve", email: "steve@stackoverflow.com", phone: "345-2222"},
            { text: "Lisa", leaf: true, name: "Lisa", email: "lisa@stackoverflow.com", phone: "345-3333"}
        ] },
    ]
}});

Ext.create('Ext.tree.Panel', {
title: 'All My Friends',
width: 200,
height: 150,
store: store,
rootVisible: false,
renderTo: Ext.getBody(),
listeners : {
        itemdblclick : function(tree, record, index){
            Ext.getStore('simpsonsStore').loadRawData([record.raw], true);
        }
}});
Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
fields:['name', 'email', 'phone'],
data:{'items':[
    { 'name': 'Bart',  "email":"bart@simpsons.com",  "phone":"555-222-1234" },
    { 'name': 'Homer', "email":"home@simpsons.com",  "phone":"555-222-1244"  },
    { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254"  }
]},
proxy: {
    type: 'memory',
    reader: {
        type: 'json',
        root: 'items'
    }
}});

Ext.create('Ext.grid.Panel', {
title: 'Best Friends',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
    { text: 'Name',  dataIndex: 'name' },
    { text: 'Email', dataIndex: 'email', flex: 1 },
    { text: 'Phone', dataIndex: 'phone' }
],
height: 200,
width: 400,
renderTo: Ext.getBody()});

通过上面的代码,我可以通过双击将树形图中的值提供给网格面板。

如果我们双击树形图中的相同叶子,我想要一个额外的列来对值进行分组。

例如,如果我们双击Bart 6次

Name  email              phonenumber     groupby(number of times)
Bart  bart@simpsons.com   555-222-1234    6

它不应在网格面板中附加相同的值。 任何人都可以帮助我。

此致 斯里卡特

1 个答案:

答案 0 :(得分:1)

您需要在商店的字段中添加计数字段。然后,您需要将字段添加到网格中。双击树时,您需要检查商店以查看记录是否已存在。如果是,请更改计数字段中的值;否则,添加一个新行。

        itemdblclick: function (tree, record, index) {
            var s = Ext.getStore('simpsonsStore'),
                existingRecIdx = s.findBy(function (r) {
                    return r.get('email') === record.raw['email'];
                });

            if (existingRecIdx === -1) { //row not found
                record.raw.clickCt = 1;
                s.loadRawData([record.raw], true);
            } else {
                var r = s.getAt(existingRecIdx);
                r.data.clickCt++;
                grid.getView().refresh(); //once the data has changed
                                          //refresh the grid
            }
        }

请参阅http://jsfiddle.net/Kk7gL/