JPN*_*JPN 6 angularjs kendo-grid
我需要在Angular-Kendo网格中实现服务器端分页.我无法从Angular方面清楚地了解如何做到这一点.
有人可以帮忙吗?
使用最新版本的Kendo UI (现在在Beta中),我们可以使用$http.postAngular提供的Kendo Grid读取功能的方法实现服务器端分页.
这是一个使用MVC 5控制器作为从数据源获取的数据的端点的示例.它通过发送page和pageSize控制器模拟服务器分页,您也可以发送take并skip在需要时随时处理它.
HTML标记
<div ng-controller="MyCtrl">
<kendo-grid k-options="mainGridOptions"></kendo-grid>
</div>
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
你可以得到当前的pageSize,网页,拿,跳多了很多关的说法e在read: function(e){}声明.
由于post值引用read函数中的参数,因此每次在网格上更新页面时它们都会更新.这是您每次网格进行更改时可用于更新帖子值的内容.网格然后重新绑定自己.
Home/DataSourceResult控制器
[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");
}
Run Code Online (Sandbox Code Playgroud)
非常基本,但正是我需要的,也可以帮助你.这使用原生的Angular http.get功能,同时仍然允许Kendo Grid完成大部分繁重的工作.
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 } },
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
和我的网格标记:
<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)">
Run Code Online (Sandbox Code Playgroud)
和我的网络 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);
}
Run Code Online (Sandbox Code Playgroud)
我的服务返回 an IQueryable,但它也可以返回一个具体列表,因为返回 anIQueryable没有帮助 Kendo 计算出总共有多少个项目。对我来说,主要问题是网格没有正确意识到总项目数,例如将显示第一页(前 5 个项目),但其余项目没有注意到,因此网格分页按钮被禁用,因此我有点破解这个问题,但手动设置项目总数,就是这些代码行:
schema:{
total: function(response) {
return 13; // call some function, or some scope variable that know the total items count
},.........
Run Code Online (Sandbox Code Playgroud)
困扰我的一件事是必须手动设置总项目数。值得一提的是,在设置数据源时,您可以将函数传递给传输对象的 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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6872 次 |
| 最近记录: |