在使用LINQ .GroupBy()和使用数据的Kendo UI Grid之间需要做些什么,就像它自己分组一样?
参考this post和this Kendo UI Grid doc看起来我是否要进行服务器端分组,然后将JSON响应返回给Grid,我必须返回响应然后在JavaScript Grid选项中,通过配置schema.groups映射函数对其进行过滤,以使Grid使用它。
示例
控制器代码
[System.Web.Http.AcceptVerbs("GET", "ASPNETMVC-AJAX")]
public Kendo.Mvc.UI.DataSourceResult IndexDataSource(
[System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))] DataSourceRequest request)
{
// do something to get a query result and generate a kendoRequest
var data = queryResult.DataList.AsQueryable()
.GroupBy(x => x.Symbol) // *** IS THIS RIGHT? ***
.ToDataSourceResult(kendoRequest);
var result = new DataSourceResult()
{
AggregateResults = data.AggregateResults,
Data = data.Data,
Errors = data.Errors,
Total = queryResult.TotalCount
};
return result;
}
然后在我的Grid DataSource 选项中:
var gridDataSourceOptions = {
autoLoad: true,
//autoSync: true,
//batch: false,
error: function (e) {
// console.error("dataSource error: " + e);
debugger;
},
pageable: {
buttonCount: 5
},
pageSize: 10,
schema: $.extend(true, {}, kendo.data.schemas["aspnetmvc-ajax"], {
aggregates: function (data) {
return data.AggregateResults;
},
data: function (data) {
return data.Data;
},
// something
groups: function (response) {
return MapGroupResponse(response); // *** Important ***
}
}),
group: {
field: groupField
},
serverAggregates: false,
serverFiltering: false,
serverGrouping: true,
serverPaging: true,
serverSorting: true,
sortable: {
mode: 'multiple',
allowUnsort: true
},
transport: {
// something
parameterMap: function (data, type) {
// something
}
},
};
然后使用mama-jamma函数将我的LINQ .GroupBy()转换成我想的是Kendo UI Grid的格式。
var groupField = "Symbol";
function MapGroupResponse (response) {
var result = [];
$.each(response.Data, function (index, item) {
result.push({
aggregates: null,
field: groupField,
value: item[0][groupField],
hasSubgroups: false, // true if there are subgroups
items: item
});
});
return result;
}
这可以通过它获取数据并且没有例外&错误,但网格不加载任何内容。