在这里找到一个例子:http://www.telerik.com/forums/does-treelist-support-pagination#5VlH4IMBXEuh819pYvyEvQ
查看:
<kendo-treelist k-options="treeListOptions"></kendo-treelist>
<kendo-pager k-options="pagerOptions"></kendo-pager>
控制器逻辑:
// default filtering and sorting
var defaultFilter = { field: "ParentId", operator: "eq", value: null };
var defaultSort = { field: "Name", dir: "asc" };
var myODataSource = new kendo.data.DataSource({
type: "odata-v4",
transport: {
read: {
url: "/odata/Products",
data: {
"$expand": "Children($levels=max)"
}
}
},
pageSize: 15,
serverPaging: true,
serverSorting: true,
serverFiltering: true,
filter: defaultFilter,
sort: defaultSort,
schema: {
parse: function(response) {
if (myODataSource.transport.options.read.data.$expand == "Parent($levels=max)") {
// if "$expand=Parent($levels=max)" then the hierarchy will be reversed Children -> Parent
// thus we need to flatten Parents
var ary = _.flattenHierarchy(response.value, 'Parent');
// and remove duplicate parents coming from different tree branches
response.value = _.uniq(ary);
} else {
// if "$expand=Children($levels=max)" then the hierarchy will be as expected Parent -> Children
// thus we need to flatten Children
response.value = _.flattenHierarchy(response.value, 'Children');
}
return response;
}
},
change: function(e) {
treeListDataSource.read();
}
});
// filter hack!
// http://www.telerik.com/forums/any-filtering-event#--cNXsvF5U6zinsTsyL4eg
var originalFilterFn = kendo.data.TreeListDataSource.fn.filter;
kendo.data.TreeListDataSource.fn.filter = function (e) {
if (arguments.length > 0) {
if (e === null) {
// if e is null, then the filter is cleared. So we need to filter by roots to get the normal tree
myODataSource.transport.options.read.data.$expand = "Children($levels=max)";
myODataSource.filter(defaultFilter);
} else {
// else we're filtering and the result nodes need to include parents to maintain the tree hierarchy
myODataSource.transport.options.read.data.$expand = "Parent($levels=max)";
myODataSource.filter(e);
}
}
return originalFilterFn.apply(this, arguments);
};
// sort hack!
var originalSortFn = kendo.data.TreeListDataSource.fn.sort;
kendo.data.TreeListDataSource.fn.sort = function (e) {
if (arguments.length > 0) {
myODataSource.sort(e);
}
return originalSortFn.apply(this, arguments);
};
var treeListDataSource = new kendo.data.TreeListDataSource({
transport: {
read: function (options) {
var data = myODataSource.data().toJSON();
options.success(data);
}
},
sort: defaultSort,
schema: {
model: {
id: "Id",
fields: {
parentId: { field: "ParentId", type: "number", nullable: true },
Id: { field: "Id", type: "number" }
},
expanded: true
}
}
});
$scope.treeListOptions = {
autoBind: false,
dataSource: treeListDataSource,
filterable: true, //{ mode: "row"}, not supported (yet) by treelist
sortable: true,
resizable: true,
reorderable: true,
columns: [
{ field: "Name" },
{ field: "Description" }
]
};
$scope.pagerOptions = {
autoBind: false,
dataSource: myODataSource,
info: true,
pageSizes: [2, 3, 4, 5, 6],
refresh: true,
};
myODataSource.read();
下划线功能
_.mixin({
flattenHierarchy: function self(objAry, childPropertyName) {
// default values
childPropertyName = typeof childPropertyName !== 'undefined' ? childPropertyName : 'children';
var result = [];
_.each(objAry, function(obj) {
// the object it self without children
var strippedObj = _.omit(obj, childPropertyName);
result.push(strippedObj);
if (obj.hasOwnProperty(childPropertyName) && obj[childPropertyName] !== null) {
// child object(s)
var children = obj[childPropertyName];
if (!_.isArray(children))
children = [children];
result.pushArray(self(children, childPropertyName));
}
});
return result;
}
});