Behrouz Ghazi
Behrouz Ghazi

Reputation: 399

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 ?

Upvotes: 5

Views: 18275

Answers (8)

safinilnur
safinilnur

Reputation: 317

One more alternative approach. You can do it in 3 simple steps:

  1. create a variable that will keep row number
  2. drop the value to 0 in the dataBinding
  3. pass function to the template which will increment your variable for each row

enter image description here

Upvotes: 0

chintan adatiya
chintan adatiya

Reputation: 1253

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.

Upvotes: 0

asdword
asdword

Reputation: 1

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

Upvotes: 0

Mohammad Taghipour
Mohammad Taghipour

Reputation: 631

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

Upvotes: 0

Morteza Tourani
Morteza Tourani

Reputation: 3536

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;

Upvotes: 8

Ehsan Mirsaeedi
Ehsan Mirsaeedi

Reputation: 7592

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

Upvotes: 0

sma
sma

Reputation: 9597

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

Upvotes: 2

Lars H&#246;ppner
Lars H&#246;ppner

Reputation: 18402

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)

Upvotes: 9

Related Questions