分页TreeList或可折叠的分层网格

时间:2015-04-23 15:29:22

标签: kendo-ui telerik kendo-grid odata hierarchical-data

我希望使用Kendo UI Grid。它看起来非常完整。然而它有一个致命的缺陷。他们的Grid / Hierarchy只是嵌套网格而不是真正的层次结构。绝对不是递归或层次化的层次结构。

幸运的是,他们有一个TreeList网格就可以做到这一点。但它并不像普通网格那样完整。它实际上缺乏对分页的支持,这使得它完全无法使用:(

我在某处读到他们没有实现分页,因为在扩展父级时,页面将包含一个额外的项目,并可能超过页面大小。如果你问我我的论点相当薄弱......显然它需要按照页面进行分页。

这真的很简单。这是使用OData

的示例

OData示例

GET ~/Products?$filter=ParentId eq null&$expand=Children&$top={pagesize}&$skip={pagenumber}&$count=true

结果

{
    "@odata.count": 33,
    "value": [
        {
            "Id": 1,
            "ParentId": null,
            ...
            "Children": [
                {
                    "Id": 2,
                    "ParentId": 1,
                }
            ]
        },
        ...
    ]
}

完美!换句话说,它不是后端的限制。虽然我可以看到节目过滤可能会变得棘手。

回到主题。我正在寻找有关如何为TreeList实现分页的建议,或者我如何在普通网格中获取可折叠行(递归层次结构样式)。实际上后者会更好,因为它的功能更完整。

2 个答案:

答案 0 :(得分:1)

只要您的结构看起来相同,因为父对象具有子数组,那么您可以在子网格上使用与父网格相同的detailInit函数,从而为您提供基本上递归的分层网格,并使用分页在每个网格级别进行过滤。

这是我做的一个例子。 http://jsbin.com/zosiji/1/edit?html,js,output我认为就是这样。 父对象有一个id和name属性,以及一个Children数组,它们一直遵循相同的结构。

答案 1 :(得分:0)

在此处找到一个示例: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;
    }
});