Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Have Row Number In Kendo UI Grid

I have kendo grid in asp.net mvc and i use server wrapper.I want Additional column named "Row Number" that is simple counter (1,2,3,...). I want this counter never change by client sorting. Always first row be 1 second row be 2 ,... in column "RowNumber"

how can I do this in kendo grid ?

like image 947
Behrouz Ghazi Avatar asked Jan 14 '14 11:01

Behrouz Ghazi


2 Answers

You can use the dataBound event:

$("#grid").kendoGrid({
    sortable: true,
    dataSource: [{
        name: "Jane Doe",
        age: 30
    }, {
        name: "John Doe",
        age: 33
    }],
    columns: [{
        field: "name"
    }, {
        field: "age"
    }, {
        field: "rowNumber",
        title: "Row number",
        template: "<span class='row-number'></span>"
    }],
    dataBound: function () {
        var rows = this.items();
        $(rows).each(function () {
            var index = $(this).index() + 1;
            var rowLabel = $(this).find(".row-number");
            $(rowLabel).html(index);
        });
    }
});

(demo)

like image 167
Lars Höppner Avatar answered Oct 04 '22 02:10

Lars Höppner


Other answers are OK but they don't apply pagination effects. So I think better implementation would be:

var grid = $( "#grid" ).kendoGrid( {
    sortable: true,
    dataSource: [ {
        name: "Jane Doe",
        age: 30
    }, {
        name: "John Doe",
        age: 33
    } ],
    pageable: {
        refresh: false,
        pageSizes: true,
        pageSize: 10,
    },
    columns: [ {
        field: "name"
    }, {
        field: "age"
    }, {
        field: "rowNumber",
        title: "Row number",
        template: dataItem => grid.dataSource.indexOf(dataItem) + 1
    } ],
} ).data().kendoGrid;
like image 31
Morteza Tourani Avatar answered Oct 04 '22 04:10

Morteza Tourani