Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add row numbers to an ExtJS grid?

Tags:

designer

extjs

In an ExtJS GridPanel, is there a way to design a column whose sole purpose is to act as a serial number column? This column will not need a dataIndex property. Right now, I am using a custom row numberer function, but this means the row numberer is defined in the grid.js file and all columns from grid.ui.js needs to be copied into grid.js. I am using the Ext designer.

EDIT: The crux of my question is: Is there a way to define a row numberer using the Ext designer?

like image 533
Victor Avatar asked Mar 28 '11 23:03

Victor


People also ask

How do I create a grid in Extjs?

Draggable column − Add Column property "enableColumnMove: true" is grid property with which we can move columns in a grid. Renderer − This is the property to customize the view of grid data based on the data we get from the store. Note − All the properties are added in the above grid example.

What is dataIndex in Extjs?

dataIndex: The dataIndex is the field in the underlying Ext. data. Store to use as the value for the column. renderer: Allows the underlying store value to be transformed before being displayed in the grid.


3 Answers

All you need is an Ext.grid.RowNumberer in your column definition.

var colModel = new Ext.grid.ColumnModel([
    new Ext.grid.RowNumberer(),
    {header: "Name", width: 80, sortable: true},
    {header: "Code", width: 50, sortable: true},
    {header: "Description", width: 200, sortable: true}
]);
like image 112
wes Avatar answered Oct 03 '22 15:10

wes


Not an answer, but just want to share this:-

On top of the Ext.grid.RowNumberer, you can have this small nifty hack which will increments your numbers correctly according to the page number that you are viewing if you have implemented PagingToolbar in your grid.

Below is my working example. I extended the original Ext.grid.RowNumberer to avoid confliction.

Kore.ux.grid.RowNumberer = Ext.extend(Ext.grid.RowNumberer, {
    renderer: function(v, p, record, rowIndex) {
        if (this.rowspan) {
            p.cellAttr = 'rowspan="'+this.rowspan+'"';
        }
        var st = record.store;
        if (st.lastOptions.params && st.lastOptions.params.start != undefined && st.lastOptions.params.limit != undefined) {
            var page = Math.floor(st.lastOptions.params.start/st.lastOptions.params.limit);
            var limit = st.lastOptions.params.limit;
            return limit*page + rowIndex+1;
        }else{
            return rowIndex+1;
        }
    }
});

And the code below is the original renderer from Ext.grid.RowNumberer, which, to me, pretty ugly because the numbers is fixed all the time no matter what page number it is.

renderer : function(v, p, record, rowIndex){
    if(this.rowspan){
        p.cellAttr = 'rowspan="'+this.rowspan+'"';
    }
    return rowIndex+1;
}
like image 35
Lionel Chan Avatar answered Oct 03 '22 16:10

Lionel Chan


For version 4.2 very very easy:

Just add a new column like this:

{
    xtype: 'rownumberer',
    width: 40,
    sortable: false,
    locked: true
}
like image 42
VAAA Avatar answered Oct 03 '22 15:10

VAAA