5
votes

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 ?

7

7 Answers

9
votes

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)

7
votes

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;
2
votes

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;
   });
}
0
votes

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);
        });
    }
});
0
votes

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},
0
votes

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);
                }
            }
        }
0
votes

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.