PDA

View Full Version : Grid with Locking and Grouping Capability



anderson.resendes
11 Aug 2011, 12:28 PM
Hello

I am developing a grid with the option of grouping and locking, and isn´t necessary to apply the two options together. If I'm using the option of locking, grouping is disabled. The same occurs if the locked is true for one or more columns of the grid, then the grouping option is disabled.


Tks!

mankz
11 Aug 2011, 11:54 PM
Not trivial, but possible :). Here's the grouping demo along with the mother-of-all-hacks applied to Ext 4.0.5 (probably won't work with earlier versions). Use with caution....



/*

This file is part of Ext JS 4

Copyright (c) 2011 Sencha Inc

Contact: http://www.sencha.com/contact

Commercial Usage
Licensees holding valid commercial licenses may use this file in accordance with the Commercial Software License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Sencha.

If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.

*/
Ext.require(['Ext.data.*', 'Ext.grid.*']);
Ext.onReady(function() {
// wrapped in closure to prevent global vars.
Ext.define('Restaurant', {
extend: 'Ext.data.Model',
fields: ['name', 'cuisine']
});

var Restaurants = Ext.create('Ext.data.Store', {
storeId: 'restaraunts',
model: 'Restaurant',
sorters: ['cuisine','name'],
groupField: 'cuisine',
data: [{
name: 'Cheesecake Factory',
cuisine: 'American'
},{
name: 'University Cafe',
cuisine: 'American'
},{
name: 'Slider Bar',
cuisine: 'American'
},{
name: 'Shokolaat',
cuisine: 'American'
},{
name: 'Gordon Biersch',
cuisine: 'American'
},{
name: 'Crepevine',
cuisine: 'American'
},{
name: 'Creamery',
cuisine: 'American'
},{
name: 'Old Pro',
cuisine: 'American'
},{
name: 'Nola\'s',
cuisine: 'Cajun'
},{
name: 'House of Bagels',
cuisine: 'Bagels'
},{
name: 'The Prolific Oven',
cuisine: 'Sandwiches'
},{
name: 'La Strada',
cuisine: 'Italian'
},{
name: 'Buca di Beppo',
cuisine: 'Italian'
},{
name: 'Pasta?',
cuisine: 'Italian'
},{
name: 'Madame Tam',
cuisine: 'Asian'
},{
name: 'Sprout Cafe',
cuisine: 'Salad'
},{
name: 'Pluto\'s',
cuisine: 'Salad'
},{
name: 'Junoon',
cuisine: 'Indian'
},{
name: 'Bistro Maxine',
cuisine: 'French'
},{
name: 'Three Seasons',
cuisine: 'Vietnamese'
},{
name: 'Sancho\'s Taquira',
cuisine: 'Mexican'
},{
name: 'Reposado',
cuisine: 'Mexican'
},{
name: 'Siam Royal',
cuisine: 'Thai'
},{
name: 'Krung Siam',
cuisine: 'Thai'
},{
name: 'Thaiphoon',
cuisine: 'Thai'
},{
name: 'Tamarine',
cuisine: 'Vietnamese'
},{
name: 'Joya',
cuisine: 'Tapas'
},{
name: 'Jing Jing',
cuisine: 'Chinese'
},{
name: 'Patxi\'s Pizza',
cuisine: 'Pizza'
},{
name: 'Evvia Estiatorio',
cuisine: 'Mediterranean'
},{
name: 'Cafe 220',
cuisine: 'Mediterranean'
},{
name: 'Cafe Renaissance',
cuisine: 'Mediterranean'
},{
name: 'Kan Zeman',
cuisine: 'Mediterranean'
},{
name: 'Gyros-Gyros',
cuisine: 'Mediterranean'
},{
name: 'Mango Caribbean Cafe',
cuisine: 'Caribbean'
},{
name: 'Coconuts Caribbean Restaurant & Bar',
cuisine: 'Caribbean'
},{
name: 'Rose & Crown',
cuisine: 'English'
},{
name: 'Baklava',
cuisine: 'Mediterranean'
},{
name: 'Mandarin Gourmet',
cuisine: 'Chinese'
},{
name: 'Bangkok Cuisine',
cuisine: 'Thai'
},{
name: 'Darbar Indian Cuisine',
cuisine: 'Indian'
},{
name: 'Mantra',
cuisine: 'Indian'
},{
name: 'Janta',
cuisine: 'Indian'
},{
name: 'Hyderabad House',
cuisine: 'Indian'
},{
name: 'Starbucks',
cuisine: 'Coffee'
},{
name: 'Peet\'s Coffee',
cuisine: 'Coffee'
},{
name: 'Coupa Cafe',
cuisine: 'Coffee'
},{
name: 'Lytton Coffee Company',
cuisine: 'Coffee'
},{
name: 'Il Fornaio',
cuisine: 'Italian'
},{
name: 'Lavanda',
cuisine: 'Mediterranean'
},{
name: 'MacArthur Park',
cuisine: 'American'
},{
name: 'St Michael\'s Alley',
cuisine: 'Californian'
},{
name: 'Cafe Renzo',
cuisine: 'Italian'
},{
name: 'Osteria',
cuisine: 'Italian'
},{
name: 'Vero',
cuisine: 'Italian'
},{
name: 'Cafe Renzo',
cuisine: 'Italian'
},{
name: 'Miyake',
cuisine: 'Sushi'
},{
name: 'Sushi Tomo',
cuisine: 'Sushi'
},{
name: 'Kanpai',
cuisine: 'Sushi'
},{
name: 'Pizza My Heart',
cuisine: 'Pizza'
},{
name: 'New York Pizza',
cuisine: 'Pizza'
},{
name: 'California Pizza Kitchen',
cuisine: 'Pizza'
},{
name: 'Round Table',
cuisine: 'Pizza'
},{
name: 'Loving Hut',
cuisine: 'Vegan'
},{
name: 'Garden Fresh',
cuisine: 'Vegan'
},{
name: 'Cafe Epi',
cuisine: 'French'
},{
name: 'Tai Pan',
cuisine: 'Chinese'
}]
});

var grid = Ext.create('Ext.grid.Panel', {
renderTo: Ext.getBody(),
collapsible: true,
iconCls: 'icon-grid',
frame: true,
store: Restaurants,
width: 600,
height: 400,
title: 'Restaurants',

lockedGridConfig : {
features : [Ext.create('Ext.grid.feature.Grouping',{
groupHeaderTpl: 'Cuisine: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})'
})]
},

normalGridConfig : {
features : [Ext.create('Ext.grid.feature.Grouping',{
groupHeaderTpl: 'Cuisine: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})'
})]
},
columns: [{
text: 'Name',
width : 100,
dataIndex: 'name',
locked : true
},{
text: 'Cuisine',
width : 200,
locked : true,
dataIndex: 'cuisine'
},
{
text: 'Cuisine',
width : 500,
dataIndex: 'cuisine'
},
{
text: 'Name',
width : 300,
dataIndex: 'name',
locked : false
}],
fbar : ['->', {
text:'Clear Grouping',
iconCls: 'icon-clear-group',
handler : function(){
groupingFeature.disable();
}
}]
});
});


Ext.grid.Lockable.override({
injectLockable: function() {

this.lockable = true;


this.hasView = true;

var me = this,



xtype = me.determineXTypeToCreate(),

selModel = me.getSelectionModel(),
lockedGrid = Ext.apply({
xtype: xtype,

enableAnimations: false,
scroll: false,
scrollerOwner: false,
selModel: selModel,
border: false,
cls: Ext.baseCSSPrefix + 'grid-inner-locked'
}, me.lockedGridConfig || {}),
normalGrid = Ext.apply({
xtype: xtype,
enableAnimations: false,
scrollerOwner: false,
selModel: selModel,
border: false
}, me.normalGridConfig || {}),
i = 0,
columns,
lockedHeaderCt,
normalHeaderCt;

me.addCls(Ext.baseCSSPrefix + 'grid-locked');


Ext.copyTo(normalGrid, me, me.normalCfgCopy);
Ext.copyTo(lockedGrid, me, me.lockedCfgCopy);
for (; i < me.normalCfgCopy.length; i++) {
delete me[me.normalCfgCopy[i]];
}
for (i = 0; i < me.lockedCfgCopy.length; i++) {
delete me[me.lockedCfgCopy[i]];
}

me.lockedHeights = [];
me.normalHeights = [];

columns = me.processColumns(me.columns);

lockedGrid.width = columns.lockedWidth;
lockedGrid.columns = columns.locked;
normalGrid.columns = columns.normal;

me.store = Ext.StoreManager.lookup(me.store);
lockedGrid.store = me.store;
normalGrid.store = me.store;


normalGrid.flex = 1;
lockedGrid.viewConfig = me.lockedViewConfig || {};
lockedGrid.viewConfig.loadingUseMsg = false;
normalGrid.viewConfig = me.normalViewConfig || {};

Ext.applyIf(lockedGrid.viewConfig, me.viewConfig);
Ext.applyIf(normalGrid.viewConfig, me.viewConfig);

me.normalGrid = Ext.ComponentManager.create(normalGrid);
me.lockedGrid = Ext.ComponentManager.create(lockedGrid);

me.view = Ext.create('Ext.grid.LockingView', {
locked: me.lockedGrid,
normal: me.normalGrid,
panel: me
});

if (me.syncRowHeight) {
me.lockedGrid.getView().on({
refresh: me.onLockedGridAfterRefresh,
itemupdate: me.onLockedGridAfterUpdate,
scope: me
});

me.normalGrid.getView().on({
refresh: me.onNormalGridAfterRefresh,
itemupdate: me.onNormalGridAfterUpdate,
scope: me
});
}

lockedHeaderCt = me.lockedGrid.headerCt;
normalHeaderCt = me.normalGrid.headerCt;

lockedHeaderCt.lockedCt = true;
lockedHeaderCt.lockableInjected = true;
normalHeaderCt.lockableInjected = true;

lockedHeaderCt.on({
columnshow: me.onLockedHeaderShow,
columnhide: me.onLockedHeaderHide,
columnmove: me.onLockedHeaderMove,
sortchange: me.onLockedHeaderSortChange,
columnresize: me.onLockedHeaderResize,
scope: me
});

normalHeaderCt.on({
columnmove: me.onNormalHeaderMove,
sortchange: me.onNormalHeaderSortChange,
scope: me
});

me.normalGrid.on({
scrollershow: me.onScrollerShow,
scrollerhide: me.onScrollerHide,
scope: me
});

me.lockedGrid.on('afterlayout', me.onLockedGridAfterLayout, me, {single: true});

me.modifyHeaderCt();
me.items = [me.lockedGrid, me.normalGrid];

me.layout = {
type: 'hbox',
align: 'stretch'
};
var lockedView = me.lockedGrid.getView();
var normalView = me.normalGrid.getView();

// HACK Sync group expand/collapse for the two views.
lockedView.on({
groupexpand : onViewGroupExpand,
groupcollapse : onViewGroupCollapse,

scope : normalView
});
normalView.on({
groupexpand : onViewGroupExpand,
groupcollapse : onViewGroupCollapse,

scope : lockedView
});

// HACK Sync group expand/collapse for the two views.
function onViewGroupExpand (v, groupName) {
var featureProt = Ext.grid.feature.Grouping.prototype,
groupBd = Ext.get(this.id + "-gp-" + groupName);

groupBd.removeCls(featureProt.collapsedCls);
groupBd.prev().removeCls(featureProt.hdCollapsedCls);
}

// HACK Sync group expand/collapse for the two views.
function onViewGroupCollapse (v, groupName) {
var featureProt = Ext.grid.feature.Grouping.prototype,
groupBd = Ext.get(this.id + "-gp-" + groupName);

groupBd.addCls(featureProt.collapsedCls);
groupBd.prev().addCls(featureProt.hdCollapsedCls);
}


}
});

// Overridden to be able to pass group name as one of the fireEvent arguments (needed to have locked+normal groups in sync)
Ext.grid.feature.Grouping.override({
expand: function(groupBd) {
var me = this,
view = me.view,
grid = view.up('gridpanel'),
groupBdDom = Ext.getDom(groupBd),
groupName = groupBdDom.id.match(view.id + '-gp-(.*)')[1];

me.collapsedState[groupBdDom.id] = false;

groupBd.removeCls(me.collapsedCls);
groupBd.prev().removeCls(me.hdCollapsedCls);

grid.determineScrollbars();
grid.invalidateScroller();
view.fireEvent('groupexpand', view, groupName);
},


collapse: function(groupBd) {
var me = this,
view = me.view,
grid = view.up('gridpanel'),
groupBdDom = Ext.getDom(groupBd),
groupName = groupBdDom.id.match(view.id + '-gp-(.*)')[1];

me.collapsedState[groupBdDom.id] = true;

groupBd.addCls(me.collapsedCls);
groupBd.prev().addCls(me.hdCollapsedCls);

grid.determineScrollbars();
grid.invalidateScroller();
view.fireEvent('groupcollapse', view, groupName);
}
});

anderson.resendes
12 Aug 2011, 4:36 AM
Thanks mankz

I still have a problem, I would like to select the locked column dinamically. Is it possible?


Thanks, again :)

mankz
12 Aug 2011, 4:38 AM
Well, you'd need to do it once for each grid. But remember, this is a hack so not sure how much time you want to invest in it :)

anderson.resendes
12 Aug 2011, 4:40 AM
Ok, thanks!