Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add row number to kendo ui grid?

I have a kendo ui grid in my page that has some columns. Now I want to add a column to it that shows me row number. How to I do this? Thanks.

like image 217
Tavousi Avatar asked Jun 29 '13 08:06

Tavousi


People also ask

How do I show row numbers in kendo grid?

You can do it in 3 simple steps: create a variable that will keep row number. drop the value to 0 in the dataBinding. pass function to the template which will increment your variable for each row.

How do I add a row in kendo grid?

Adds an empty data item to the grid. In "incell" and "inline" editing mode a table row will be appended. Popup window will be displayed in "popup" editing mode.

How do I set data for Kendo grid?

Bind data to Kendo Grid by using AJAX Read action method. Change the datasource on change event of any HTML controls. Normally, a developer can bind the data to Grid by using AJAX Read method. This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.

What is pageSize in kendo grid?

It sets the page size to the total number of records. If a pageSize setting is provided for the data source then this value will be selected initially.


2 Answers

Initialize a variable and show it in column as template: "#= ++record #"

Working Demo

Here is code:

var record = 0;

$("#grid").kendoGrid({
  dataSource: {
    data: [{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" }, { foo: "foo" }, { foo : "foo1" }],
    pageSize: 10
  },
  sortable: true,
  columns: [ {
    title: " ",
    template: "#= ++record #",
    width: 30
  }, { field: "foo" }],
  pageable: true,
  dataBinding: function() {
    record = (this.dataSource.page() -1) * this.dataSource.pageSize();
  }
});
like image 73
Zaheer Ahmed Avatar answered Oct 12 '22 16:10

Zaheer Ahmed


For Razor you also need to actually show the number also: (not enough point thingies to comment)

above the grid:

@{int counter = 1;}

inside column definitions:

columns.Template(@<text> 
        <span>@counter @{ counter++; }</span>
        </text>).Title("#");
like image 37
WhiteRuski Avatar answered Oct 12 '22 15:10

WhiteRuski