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 ?
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)
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With