我不确定我这样做是否正确,因为我是整个这些系统的新手,但我想管理我的剑道网格数据(分页、过滤器等)。我在 ASP.NET Web API 2 中使用这个 ODataController,它是从 OData 包的版本 4 驱动的,它支持如下参数:$top、$skip、$count。ETC...
由于阅读了许多不完整的hello world,示例,堆栈线程,测试了其中的许多,我到达了这部分(下面的代码),它支持其他不存在的参数(如:take):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<title>AngularJS</title>
<link href="../content/shared/styles/examples-offline.css" rel="stylesheet">
<link href="../Content/telerik/content/web/kendo.common.min.css" rel="stylesheet">
<link href="../Content/telerik/content/web/kendo.rtl.min.css" rel="stylesheet">
<link href="../Content/telerik/content/web/kendo.default.min.css" rel="stylesheet">
<script src="../Content/telerik/scripts/jquery.min.js"></script>
<script src="../Content/telerik/scripts/angular.min.js"></script>
<script src="../Content/telerik/scripts/kendo.all.min.js"></script>
<script src="../content/shared/js/console.js"></script>
<script>
</script>
</head>
<body>
<a class="offline-button" href="../index.html">Back</a>
<div id="example" ng-app="KendoDemos">
<div ng-controller="MyCtrl">
<kendo-grid options="mainGridOptions"></kendo-grid>
</div>
</div>
<script>
angular.module("KendoDemos", [ "kendo.directives" ]);
function MyCtrl($scope) {
$scope.mainGridOptions = {
dataSource: {
type: "odata-v4",
transport: {
//read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Employees"
read: "/odata/people",
dataType: "json"
},
parameterMap: function (data, operations) {
var paramMap = kendo.data.transports.odata.parameterMap(data);
if (paramMap.$inlinecount) {
if (paramMap.$inlinecount == "allpages") {
paramMap.$count = true;
}
delete paramMap.$inlinecount;
}
if (paramMap.$take) {
paramMap.$top = paramMap.$take;
delete paramMap.$take;
}
if (paramMap.$filter) {
paramMap.$filter = paramMap.$filter.replace(/substringof\((.+),(.*?)\)/, "contains($2,$1)");
}
return paramMap;
},
schema: {
data: function (data) { return data.value; },
total: function (data) { return data['@odata.count']; },
model: {
id: "Email",
fields: {
Name: {type: "string"},
Email: {type: "string"},
}
}
},
pageSize: 5,
serverPaging: true,
serverSorting: true
},
sortable: true,
pageable: true,
//detailTemplate: kendo.template($("#template").html()),
dataBound: function() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
},
columns: [
{
field: "Name",
title: "Name",
width: "200px"
},
{
field: "Email",
title: "E-Mail",
width: "200px"
},
]
};
}
</script>
</body>
</html>
该代码使用注入的匿名函数,它可以定义自定义参数并映射它们。
parameterMap: function (data, operations) {
var paramMap = kendo.data.transports.odata.parameterMap(data);
if (paramMap.$inlinecount) {
if (paramMap.$inlinecount == "allpages") {
paramMap.$count = true;
}
delete paramMap.$inlinecount;
}
if (paramMap.$take) {
paramMap.$top = paramMap.$take;
delete paramMap.$take;
}
if (paramMap.$filter) {
paramMap.$filter = paramMap.$filter.replace(/substringof\((.+),(.*?)\)/, "contains($2,$1)");
}
return paramMap;
},
schema: {
data: function (data) { return data.value; },
total: function (data) { return data['@odata.count']; },
model: {
id: "Email",
fields: {
Name: {type: "string"},
Email: {type: "string"},
}
}
},
pageSize: 5,
serverPaging: true,
serverSorting: true
},
在检查完之后,我在这里和那里进行了一些更改,例如设置模式,并添加新条件(因为它们在示例代码中留空)。
现在,发布给我的是,在运行我的应用程序之后,我注意到,即使它没有应用我的代码段......
if (paramMap.$take) {
paramMap.$top = paramMap.$take;
delete paramMap.$take;
}
但它甚至没有应用其他部分,而且 JavaScript 调试器也不会在函数内部中断...。因此,例如,它不会添加诸如计数之类的参数...
所以,我来这里首先,确保我走的是正确的路,其次,如果它是正确的,那么如何解决它......
我还阅读了一些关于将内容更改为 JSON 的内容,但我假设我的数据是 JSON,因为我没有定义任何内联字符串...