【问题标题】:How to implement Server side paging in Client side Kendo UI grid in asp.net mvc如何在asp.net mvc的客户端Kendo UI网格中实现服务器端分页
【发布时间】:2012-10-07 09:10:02
【问题描述】:

谁能告诉我如何使用客户端 Kendo UI Grid 实现服务器端分页?

【问题讨论】:

    标签: asp.net-mvc kendo-ui


    【解决方案1】:

    更新:我们有 released 一个开源 .NET 库,它使分页、排序和过滤变得更加容易。

    serverPaging 设置为true 后,网格将发送当前的pageSizeskip。在服务器端,您应该使用提供的信息对数据进行分页,并将其与项目总数一起返回。这是一个代码sn-p:

    动作

    public ActionResult Products(int pageSize, int skip) 
    {
         using (var northwind = new NorthwindDataContext())
         {
            var products = northwind.Products;
            // Get the total number of records - needed for paging
            var total = products.Count();
    
            // Page the data
            var data = products.Skip(skip).Take(pageSize).ToList();
    
            // Return as JSON - the Kendo Grid will use the response
            return Json(new { total = total, data = data });
         }
    }
    

    查看

    $("#grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                   url: "home/products",
                   dataType: "json",
                   type: "POST"
                }
            },
            schema: {
                data: "data", // records are returned in the "data" field of the response
                total: "total" // total number of records is in the "total" field of the response
            },
            serverPaging: true // enable server paging
        }
    });
    

    参考

    使用 LINQ 进行分页

    数据源配置设置

    【讨论】:

    【解决方案2】:

    接受的答案没有 UI 解决方案;只提供一个 jQuery 答案。如果它对其他人有帮助,这里是在 UI 中适用于我们的剑道网格的解决方案:

    控制器的代码sn-p

            DataSourceResult result = new DataSourceResult()
            {
                Data = dataSet,
                Total = recordCount
            };
    
            return Json(result, JsonRequestBehavior.AllowGet);
    

    View的代码sn-p

            .DataSource(dataSource => dataSource                
                .Ajax()
                .Read(read => read.Action("*<our method>*", "*<our controller>*")
            )
    

    【讨论】:

      【解决方案3】:

      要实现服务器分页,需要从服务器返回正确的格式。对于服务器端分页,JSON 格式类似于以下 JSON:-

      { "mytotal":1069, "mydata": [{ ProductID : 1, ProductName : "Chai"}, { ProductID : 2, ProductName : "Chang" }]}
      

      告诉剑道网格从 mytotal 对象中选择记录总数,并从模式中的 mydata 中选择数据行

      schema: {
          data: "mydata"
          total: "mytotal" // total is returned in the "total" field of the response
        }
      

      查看详细示例here

      【讨论】:

        猜你喜欢
        • 2017-01-19
        • 2023-03-10
        • 2013-11-11
        • 1970-01-01
        • 2023-03-12
        • 2014-12-28
        • 2011-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多