How Can I Have Row Number In Kendo UI Grid

前端 未结 7 2145
悲&欢浪女
悲&欢浪女 2020-12-10 18:15

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

相关标签:
7条回答
  • 2020-12-10 18:54

    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 讨论(0)
  • 2020-12-10 19:03

    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 讨论(0)
  • 2020-12-10 19:03

    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.

    0 讨论(0)
  • 2020-12-10 19:04

    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 讨论(0)
  • 2020-12-10 19:06

    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)

    0 讨论(0)
  • 2020-12-10 19:12

    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 讨论(0)
提交回复
热议问题