Seeing that cell borders aren't supported with Ext 2.x anymore, I found it hard to keep my eyes on the right column in long grids. Adding borders and keeping header alignment turned out to be pure hell, so I toyed around to add a stripeCols option similar to the builtin stripeRows one and made a small plugin (just a few lines in fact) that does the trick for me:
Code:
Ext.ns('Ext.ux.plugins.grid');
Ext.ux.plugins.grid.stripeCols = Ext.extend( Ext.util.Observable, {
init: function(grid) {
grid.getView().afterRender = grid.getView().afterRender.createSequence( this.setCss );
grid.getView().on('rowsinserted', this.setCss.createDelegate(grid.getView()));
grid.getView().on('rowremoved', this.setCss.createDelegate(grid.getView()));
grid.getColumnModel().on('columnmoved', this.setCss.createDelegate(grid.getView()));
grid.getColumnModel().on('hiddenchange', this.setCss.createDelegate(grid.getView()));
grid.getView().on('refresh', this.setCss.createDelegate(grid.getView()));
}
,setCss: function() {
for(var j = 0; j < this.grid.getStore().getCount(); j++) {
var markop = 1;
var r = this.getRow(j);
if( r ) {
for(var i = 0; i < this.grid.getColumnModel().getColumnCount(); i++) {
var c = this.getCell(j,i);
if( this.grid.stripeCols && i % 2 == markop && ( ! this.grid.excludeCStripe || this.grid.excludeCStripe.indexOf(this.grid.getColumnModel().getDataIndex(i)) == -1 ) ) {
if( this.grid.getColumnModel().isHidden(i) || this.grid.skipCStripe && this.grid.skipCStripe.indexOf(this.grid.getColumnModel().getDataIndex(i)) != -1 )
{
markop ^= 1;
} else {
if( Ext.Element.get(c) ) {
Ext.Element.get(c).addClass('x-grid3-col-alt');
}
}
} else {
if( Ext.Element.get(c) ) {
Ext.Element.get(c).removeClass('x-grid3-col-alt');
}
}
}
}
}
}
});
Now all that is left is to set the corresponding css class for .x-grid3-col-alt (and in case you're using stripeRows at the same time, also for .x-grid3-row-alt .x-grid3-col-alt):
Code:
<style type="text/css">
.x-grid3-col-alt {
background-color:#fafafa !important;
}
.x-grid3-row-alt .x-grid3-col-alt {
background-color:#eaeaea !important;
}
/* Optionally, you can also override the styles for hovered/selected rows */
.x-grid3-row-over .x-grid3-col-alt {
background: #e0e0e0 url(../images/default/grid/row-over.gif) repeat-x left top !important;
}
.x-grid3-row-selected .x-grid3-col-alt {
background: #d0d0f0 !important;
border:1px dotted #d0d0df0;
}
</style>
To use it, simply define your grid like the following:
Code:
new Ext.grid.GridPanel({
...usual grid config options...
,stripeCols: true
,excludeCStripe: ['f3']
,skipCStripe: ['f6']
,plugins: [ new Ext.ux.plugins.grid.stripeCols() ]
...usual grid config options...
});
Online Example: http://www.chrwinter.de/app/stripeCols.html
Additional config options:
You can exclude columns from being striped by specifying those columns' dataIndexes to exclude in the grid's 'excludeCStripe' array in the grid config. Other columns' striping is not affected by this option.
Or you can skip individual columns by listing them in the 'skipCStripe' array, then striping continues with the next column.
Changes:
v1.02: (2009-04-02) Add hover/select styles to CSS examples
v1.03: (2009-04-04) Added excludeCStripe / skipCStripe config options
v1.04: (2009-04-04) Plugin now also honors column move/hide/unhide [thx tobiu]
News:
yyogev has adapted the code for ExtJS 4, you can find it here.