【发布时间】:2012-10-07 09:10:02
【问题描述】:
谁能告诉我如何使用客户端 Kendo UI Grid 实现服务器端分页?
【问题讨论】:
标签: asp.net-mvc kendo-ui
谁能告诉我如何使用客户端 Kendo UI Grid 实现服务器端分页?
【问题讨论】:
标签: asp.net-mvc kendo-ui
更新:我们有 released 一个开源 .NET 库,它使分页、排序和过滤变得更加容易。
将serverPaging 设置为true 后,网格将发送当前的pageSize 和skip。在服务器端,您应该使用提供的信息对数据进行分页,并将其与项目总数一起返回。这是一个代码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
}
});
【讨论】:
接受的答案没有 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>*")
)
【讨论】:
要实现服务器分页,需要从服务器返回正确的格式。对于服务器端分页,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
【讨论】: