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
I am using Angular and Kendo together and I set the index value like this (using Lodash):
dataBound : function () {
_.each(this.items(), function (item, i) {
var rowScope = angular.element(item).scope();
rowScope.dataItem.index = i;
});
}
For server side pagination this script can be used (in columns section of grid):
{ title: "#",
template: dataItem => (grid.dataSource.page() - 1) *
grid.dataSource.pageSize() +
grid.dataSource.indexOf(dataItem) + 1,
width: 45},
For me it worked like this
grid = $("#grid").kendoGrid({
dataSource: dataSource,
height: 543,
scrollable: {
virtual: true
},
pageable: {
numeric: false,
previousNext: false,
messages: {
display: "Showing {2} data items"
}
},
columns: [
{
title: "#",
// template: "#= ++record #",
template: function(dataItem) {
var rowNumber = 0
if($("#grid").data("kendoGrid")) {
rowNumber = $("#grid").data("kendoGrid").dataSource.indexOf(dataItem) + 1;
}
return "<strong>" + rowNumber + "</strong>";
},
width: 80
},
{ field: "FirstName", title: "First Name" },
{ field: "LastName", title: "Last Name" },
{ field: "City", title: "City" },
{ field: "Title" }
]
});`
without using $("#grid").data("kendoGrid") this in first renders it was showing 0 as row number.
I think better implementation would be:
dataBound: function () {
if (this.dataSource && this.dataSource._total) {
var rows = this.items();
for (var i = 0; i < rows.length; i++) {
var index = i + 1
+ (this.dataSource.pageSize() * (this.dataSource.page() - 1));
var rowLabel = $(rows[i]).find(".row-number");
$(rowLabel).html(index);
}
}
}
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)
Lars Hoppner`s Answer Was Correct, But If You Change The page, the numbering will get reset. my solution was to add page number and page size to the formula:
$("#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
+ ($("#grid").data("kendoGrid").dataSource.pageSize() * ($("#grid").data("kendoGrid").dataSource.page() - 1));;
var rowLabel = $(this).find(".row-number");
$(rowLabel).html(index);
});
}
});