How to specify total items with server paging in grid

4 posts, 0 answers
  1. Eric
    Eric avatar
    24 posts
    Member since:
    Dec 2010

    Posted 23 Apr 2020 Link to this post

    I use ToDataSource to return a subset of records to display in the grid, using the DataSourceRequest's Page and PageSize to determine which rows to return.  What I can't figure out is how to indicate to the grid how many total records there are, so that it can display the right page count.  Is this as simple as returning a different structure that includes the total record count or what?  I cannot find any examples of this online.

    return Json(enumerable.ToDataSourceResult(request));

  2. Nikolay
    Admin
    Nikolay  avatar
    264 posts

    Posted 27 Apr 2020 Link to this post

    Hello Eric,

    The ToDataSourceResult extension method converts the collection of items to a Kendo.Mvc.UI.DataSourceResult object. This extension method will page automatically the data using the information provided by the DataSourceRequest object. More information can be found in the following Ajax binding article:

    However, in some scenarios, the predefined Ajax settings do not help us and our project requires full control over the DataSource client-side API options. Here Custom DataSource can be used, for instance:

    @(Html.Kendo().Grid<OrderViewModel>()
        .Name("grid")
        .DataSource(dataSource => dataSource
            .Custom()
            .PageSize(10)
            .ServerPaging(true)
            .ServerSorting(true)
            .ServerFiltering(true)
            .Transport(transport => transport
                .Read(read => read.Action("Read", "Home"))
            )
            .Schema(schema => schema
                .Data("Data")
                .Total("Total") //The field from the server response which contains the total number of data items.
            )
        )
    )

    For more information on Custom DataSOurce please refer to the following article:

    Let me know if the above answers the question.

    Regards,
    Nikolay
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. Eric
    Eric avatar
    24 posts
    Member since:
    Dec 2010

    Posted 27 Apr 2020 Link to this post

    I eventually discovered that DataSourceResult has a Total property.  I just happened to stumble on that, as I could not find any examples of this online anywhere.  I wish this had just been included in the online tutorial examples.

     

    var gridModel = results.ToDataSourceResult(request);

    gridModel.Total = resultsPackage.TotalRecords;

    return Json(gridModel);

  4. Nikolay
    Admin
    Nikolay  avatar
    264 posts

    Posted 29 Apr 2020 Link to this post

    Hello Eric,

    I am glad to hear my suggestions are helping you move forward with the project.

    I will bring up the topic to the Team and if necessary this will be further added to the documentation.

    Regards,


    Nikolay
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top