我正在尝试在剑道网格中实现服务器端过滤。
我有发布请求“ / api / Account / Filter”,该请求返回过滤器数据, 我还从小提琴手那里检查了一下,这意味着它可以正常工作,但是我不知道为什么在过滤器调用kendo网格后,所有数据都会更新!
这是我的代码:
$scope.DS = new kendo.data.DataSource({
type: "json",
transport: {
read: {
url: "/api/Employees",
dataType: "json",
contentType: "application/json",
type: "GET",
headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("accessToken") }
},
update: {
url: "/api/Employees/PUT",
dataType: "json",
contentType: "application/json",
type: "PUT",
headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("accessToken") }
},
destroy: {
url: "/api/Employees/DELETE",
dataType: "json",
contentType: "application/json",
type: "DELETE",
headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("accessToken") } },
create: {
url: "/api/Employees/POST",
dataType: "json",
contentType: "application/json",
type: "POST",
headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("accessToken") } },
parameterMap: function (options, operation) {
if (operation !== "read") {
console.log(JSON.stringify(options.models));
return JSON.stringify(options.models);
}}},
batch: true,
pageSize: 5,
serverFiltering: true,
schema: {
model: {
id: "Id",
data: 'Data',
total: 'Total',
// errors: 'Errors',
fields: {
Id: { editable: false, nullable: false, type: "number" },
Email: { editable: true, type: "string" },
UserName: {editable: true,type: "string"},
FirstName: { editable: true, type: "string" },
LastName: { editable: true, type: "string"},
Age: { editable: true, validation: { required: true } },
Phone: { editable: true, validation: { required: true } },
Department: { editable: true,type: "string"}
} }
},
});
$scope.mainGridOptions = {
dataSource: $scope.DS,
dataBound: function () {
var data = this.dataSource.view();
},
toolbar: ["create"],
columns: [
{ field: "Email", title: "Email", width: "100px" },
{ field: "UserName", title: "User Name", width: "100px"},
{ field: "FirstName", title: "First Name", width:"100px"},
{ field: "LastName", title: "Last Name", width: "100px" },
{ field: "Phone", title: "Phone", width: "100px" },
{ field: "Age", title: "Age", width: "100px" },
{ field: "Department", title: "Department", width: "100px"},
{
command: [{
name: "edit",
text: {
edit: "Edit",
update: "Save",
cancel: "Cancel"
}
}, "destroy"], title: "Actions", width: "150px"
}
],
filterable: true,
pageable: {
refresh: true,
input: true,
numeric: false,
pageSizes: [5, 10, 20, 30, 50, 75, 100, 500, 1000]
},
sortable: true,
resizable: true,
navigatable: true,
serverFiltering: true,
editable: { mode: "inline" },
filter: function (data) {
console.log((data));
console.log((data.filter));
const requestOptions = {
headers: { 'Authorization': 'Bearer ' + sessionStorage.getItem("accessToken") }
};
$http.post("/api/Account/Filter", data, requestOptions).then(function (d) {
console.log(JSON.stringify(d.data.Data));
// $scope.DS.read = d.data.Data;
debugger;
$scope.mainGridOptions.dataSource.query(d.data.Data);
}, function () {
alert("Failed.Please try again.")
});
},
noRecords: { template: "No results available."}
};
以下代码在控制器中以获取所有数据:
public List<RegisterBindingModel> GET()
{
var user = db.Users.Select(x => new RegisterBindingModel()
{
Id = x.Id,
Email = x.Email,
UserName = x.UserName,
FirstName = x.FirstName,
LastName = x.LastName,
Age = x.Age,
Phone = x.Phone,
Department = x.Department
});
return user.ToList();
这是用于服务器端的过滤器:
[AcceptVerbs("GET","POST")]
public IHttpActionResult Filter(Models.DataSourceRequest request)
{
request.Skip = 0;
request.Take = 5;
if (request != null)
{
request.Filter.Field = request.Filter.Filters.FirstOrDefault().Field;
request.Filter.Operator = request.Filter.Filters.FirstOrDefault().Operator;
request.Filter.Value = request.Filter.Filters.FirstOrDefault().Value;
var o = request;
}
var employees = db.Users.OrderBy(ii => ii.Id).Select(x => new RegisterBindingModel()
{
Id = x.Id,
Email = x.Email,
UserName = x.UserName,
FirstName = x.FirstName,
LastName = x.LastName,
Age = x.Age,
Phone = x.Phone,
Department = x.Department
});
var i = employees.ToDataSourceResult(request.Take, request.Skip, request.Sort, request.Filter);
var data = i.Data; // This is contain filtered data
request.Take = i.Total;
var d = Json(data);
return Json(new { Data = data, Total = request.Take });
//return data.Cast <RegisterBindingModel>().ToList();
}
你能告诉我我在做什么错吗?