dongryphon
30 Oct 2009, 8:53 AM
Below is a little grid plugin I wrote that puts (and keeps up to date) a row number. It is both a column type and a plugin, so put the instance in both.
Enjoy!
Updated: 31-Oct-2009
// Usage example:
var rowNumberer = new Ext.ux.GridRowNumberer();
new Ext.grid.GridPanel(
{
columns: [ rowNumberer, ... ],
plugins: [ rowNumberer, ... ],
...
});
Here's the plugin:
Ext.ux.GridRowNumberer = Ext.extend(Object,
{
header: "",
isColumn: true,
menuDisabled: true,
//fixed: true,
width: 16,
id: "rowNumCol",
rowspan: undefined,
/**
This CSS class is placed on the TD element of each cell.
*/
cls: "gridRowNumber",
/**
Initializes this instance. Any properties in the supplied config object
are copied on to this object.
*/
constructor: function (cfg)
{
if (cfg)
Ext.apply(this, cfg);
this.renderer = this.renderer.createDelegate(this); // annoyance of how columns work
},
init: function (grid)
{
this.grid = grid;
grid.mon(grid.getView(),
{
rowremoved: this.onRowChange,
rowsinserted: this.onRowChange,
scope: this
});
},
onRowChange: function (view, rowIndex)
{
var colIndex = this.grid.getColumnModel().getIndexById(this.id);
var sel = "div.x-grid3-cell-inner.x-grid3-col-" + this.id;
for (var i = rowIndex, n = view.getRows().length; i < n; ++i)
{
var cell = Ext.fly(view.getCell(i, colIndex)).first(sel, true);
cell.innerHTML = "" + (i+1);
}
},
renderer: function (value, metaData, record, rowIndex, colIndex, store)
{
if (this.cls)
metaData.css = this.cls;
if (this.rowspan)
metaData.cellAttr = 'rowspan="'+this.rowspan+'"';
return rowIndex + 1;
}
});
Enjoy!
Updated: 31-Oct-2009
// Usage example:
var rowNumberer = new Ext.ux.GridRowNumberer();
new Ext.grid.GridPanel(
{
columns: [ rowNumberer, ... ],
plugins: [ rowNumberer, ... ],
...
});
Here's the plugin:
Ext.ux.GridRowNumberer = Ext.extend(Object,
{
header: "",
isColumn: true,
menuDisabled: true,
//fixed: true,
width: 16,
id: "rowNumCol",
rowspan: undefined,
/**
This CSS class is placed on the TD element of each cell.
*/
cls: "gridRowNumber",
/**
Initializes this instance. Any properties in the supplied config object
are copied on to this object.
*/
constructor: function (cfg)
{
if (cfg)
Ext.apply(this, cfg);
this.renderer = this.renderer.createDelegate(this); // annoyance of how columns work
},
init: function (grid)
{
this.grid = grid;
grid.mon(grid.getView(),
{
rowremoved: this.onRowChange,
rowsinserted: this.onRowChange,
scope: this
});
},
onRowChange: function (view, rowIndex)
{
var colIndex = this.grid.getColumnModel().getIndexById(this.id);
var sel = "div.x-grid3-cell-inner.x-grid3-col-" + this.id;
for (var i = rowIndex, n = view.getRows().length; i < n; ++i)
{
var cell = Ext.fly(view.getCell(i, colIndex)).first(sel, true);
cell.innerHTML = "" + (i+1);
}
},
renderer: function (value, metaData, record, rowIndex, colIndex, store)
{
if (this.cls)
metaData.css = this.cls;
if (this.rowspan)
metaData.cellAttr = 'rowspan="'+this.rowspan+'"';
return rowIndex + 1;
}
});