Kendo UI Grid - Show row number

半世苍凉 提交于 2020-01-03 18:35:15

问题


How do I show the row number in a Kendo UI Grid? The code I have is not working. The page displays the column but it's empty.

@{int counter = 1;}

@(Html.Kendo().Grid<QueueViewModel>()
      .Name("Queue")
      .Columns(columns =>
      {
          columns.Template(@<text><span>@counter @{ counter++; }</span></text>).Title("#");
      })
     .DataSource(dataSource => dataSource
     .Ajax()
     .PageSize(10) 
     .Read(read => read.Action("GetOpenQueue", "DataSource", new { GeneralQueue = true })
))

回答1:


Do this:

@{
   int counter = 1;
}

@(Html.Kendo().Grid<QueueViewModel>()
      .Name("Queue")
      .Columns(columns =>
      {
          columns.Template(@<text><span>@(counter++)</span></text>).Title("#");
      })

Or, if your DataSource is set to Ajax (client-side), do this:

<script>
    var counter = 1;

    function onDataBound(e) {
        counter = 1;
    }

    function renderNumber(data) {
        return counter++;
    }    
</script>

@(Html.Kendo().Grid()   
    .Name("Queue")
    .Columns(columns => {
        columns.Template(t => { }).ClientTemplate("#= renderNumber(data) #").Title("#");
    })
    .Events(ev => ev.DataBound("onDataBound"))
)



回答2:


Column ClientTemplate is client-side functionality. You cannot use server-side variables in it. You should define Javascript variable:

<script>
    var i = 1;
</script>

Then, inside the grid use this:

columns.Template(t => { }).ClientTemplate(#=i++#).Title("#");

Updated: it should be ClientTemplate instead of Template




回答3:


Try this way In javascript, the code will support the paging also

        <script type="text/javascript"> 
            var CountIt = 0 
            function GetCountIt() { 
                var page = $("#YourGrid").data("kendoGrid").dataSource.page();
                var pageSize = $("#YourGrid").data("kendoGrid").dataSource.pageSize();
                CountIt++;
                return (page * pageSize) - pageSize + CountIt 
            }

            function YourGrid_DataBound() { 
                CountIt = 0; $('#YourGrid').data('kendoGrid').pager.unbind("change").bind('change', function (e) {
                    CountIt = 0
                })
            }

        </script>

then add to your kindo grid

       .Events(events =>
       {
          events.DataBound("YourGrid_DataBound"); 
       })
       .Columns(columns =>
        {
    columns.Bound("").ClientTemplate("#=GetCountIt()#").Title("Sr.").Width(50);
...


来源:https://stackoverflow.com/questions/33830920/kendo-ui-grid-show-row-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!