我需要在Angular-Kendo网格中实现服务器端分页。从角度来看,我不能清楚地理解如何做到这一点。
有人能帮帮忙吗?
发布于 2014-09-03 05:19:19
随着最新版本的Kendo UI out (in Beta right now),我们还有另一种方法可以实现服务器端分页,使用Angular为Kendo Grid read功能提供的$http.post方法。
这是一个使用MVC5控制器作为从数据源获取数据的端点的示例。它通过向控制器发送page和pageSize来模拟服务器寻呼,如果需要,您也可以发送take和skip,并随心所欲地处理它。
超文本标记语言
<div ng-controller="MyCtrl">
<kendo-grid k-options="mainGridOptions"></kendo-grid>
</div>JavaScript
function MyCtrl($scope, $http) {
$scope.mainGridOptions = {
dataSource: {
schema: {
data: "Data",
total: "Total"
},
transport: {
read: function (e) {//You can get the current page, pageSize etc off `e`.
var requestData = {
page: e.data.page,
pageSize: e.data.pageSize,
type: "hello"
};
console.log(e);
$http({ method: 'POST', url: 'Home/DataSourceResult', data: requestData }).
success(function (data, status, headers, config) {
e.success(data);
//console.log(data.Data);
}).
error(function (data, status, headers, config) {
alert('something went wrong');
console.log(status);
});
}
},
pageSize: 1,
serverPaging: true,
serverSorting: true
},
selectable: "row",
pageable: true,
sortable: true,
groupable: true
}
}您可以从read: function(e){}声明中的参数pageSize中获取当前页面、页面、take、skip以及更多内容。
因为post值引用了read函数中的参数,所以每次在网格上更新页面时,它们都会更新。这是您可以用来在每次网格进行更改时更新您的post值的方法。然后,网格重新绑定自身。
主目录/数据源结果控制器
[HttpPost]
public ActionResult DataSourceResult(int page, string type, int pageSize)
{
ResponseData resultData = new ResponseData();
string tempData = "";
if (page == 1)
{
tempData = "[{\"NAME\": \"Example Name 1\", \"DESCRIPTION\": \"Example Description 1\"},{\"NAME\": \"Example Name 2\",\"DESCRIPTION\": null}]";
}
else if (page == 2)
{
tempData = "[{\"NAME\": \"Example Name 3\", \"DESCRIPTION\": \"Example Description 3\"},{\"NAME\": \"Example Name 4\",\"DESCRIPTION\": \"Example Description 4\"}]";
}
resultData.Data = tempData;
resultData.Total = "4";
string json = JsonConvert.SerializeObject(resultData);
json = json.Replace(@"\", "");
json = json.Replace("\"[{", "[{");
json = json.Replace("}]\"", "}]");
return Content(json, "application/json");
}非常简单,但这正是我需要的,也可能对你有所帮助。这使用了原生的Angular http.get功能,同时仍然允许Kendo Grid完成大部分繁重的任务。
发布于 2013-12-14 03:55:16
Kendo网格本质上支持服务器端分页,至少它有一个方便的内置API来提供帮助,因此您只需将所有部分挂钩在一起即可。这是我想出来的,我的网格的数据源:
$scope.myGrid.dataSource = new kendo.data.DataSource({
transport:{
read:{
url: '/api/mygridapi?orderId=113',
dataType: 'json'
}
},
pageSize: 5,
serverPaging: true,
serverSorting: true,
serverFiltering: true,
serverGrouping: true,
serverAggregates: true,
schema:{
total: function(response) {
return 13; // call some function, or some scope variable that know the total items count
},
model: {
id: "id",
fields: {
'id': { type: "number", editable: false },
'name': { type: "string", editable: true, nullable: false, validation: { required: true } },
'price': { type: "number", editable: true, nullable: false, validation: { required: true } },
}
}
}
});和我的网格标记:
<div kendo-grid k-pageable='{ "pageSize": 5, "refresh": true, "pageSizes": false }'
k-height="'250px'" k-column-menu="false" k-filterable="true" k-sortable="true" k-groupable="true"
k-data-source="myGrid.dataSource" k-options="{{myGrid.gridOpts}}" k-on-change="onSelectHandler(kendoEvent)">和我的web api控制器:
[System.Web.Http.HttpGet]
public IEnumerable<ProductsDTO> Get(int orderId)
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
//the name value captures the paging info that kendo automatically appends to the query string when it requests data
//it has info such as teh current page, page size etc....
int take = int.Parse(nvc["take"]);
int skip = int.Parse(nvc["skip"]);
return productsSvc.GetProductsOfOrder(orderId,skip,take);
}我的服务返回一个IQueryable,但它也可以返回一个具体的列表,因为返回一个IQueryable并不能帮助Kendo计算出总共有多少个项目。对我来说,主要的问题是网格没有意识到总项目数是正确的,例如,第一页将被显示(前5个项目),而其余的项目没有被注意到,结果是网格的分页按钮被禁用,所以我有点绕过了这个问题,但手动设置了项目的总数,这就是这些代码行:
schema:{
total: function(response) {
return 13; // call some function, or some scope variable that know the total items count
},.........有一件事困扰着我,那就是必须手动设置项目总数。值得一提的是,在设置数据源时,您可以将一个函数传递给传输对象的read属性,该函数将一个包含当前分页/过滤信息的对象作为参数,因此您可以使用该对象手动构建查询字符串,而不是依赖于默认的kendo服务器请求:
transport: {
read: function (options) {
console.log(options);//see whats inside
//we can use the pageNo and pageSize property to create a query string manually
}
}https://stackoverflow.com/questions/19711159
复制相似问题