在Kendo HierarchicalDataSource中PushCreate子节点

时间:2014-11-21 23:43:03

标签: javascript kendo-ui kendo-treeview

我试图展示配置了远程数据的Kendo TreeView,但是使用直接从html加载的数据预填充前两个级别。

为此,我想使用Kendo dataSource的pushCreate方法将初始元素添加到树中:

homogeneous = new kendo.data.HierarchicalDataSource({
    transport: { read: { url: serviceRoot + "/Employees", dataType: "jsonp" } },
    schema: {
        model: {
            id: "EmployeeId",
            hasChildren: "HasEmployees"
        }
    }
});

// Adding root
homogeneous.pushCreate({"EmployeeId":2,"FullName":"Andrew Fuller","HasEmployees":true,"ReportsTo":null});

// Adding children
homogeneous.pushCreate([
    {"EmployeeId":1,"FullName":"Nancy Davolio","HasEmployees":false,"ReportsTo":2},
    {"EmployeeId":3,"FullName":"Janet Leverling","HasEmployees":false,"ReportsTo":2},
    {"EmployeeId":4,"FullName":"Margaret Peacock","HasEmployees":false,"ReportsTo":2},
    {"EmployeeId":5,"FullName":"Steven Buchanan","HasEmployees":true,"ReportsTo":2},
    {"EmployeeId":8,"FullName":"Laura Callahan","HasEmployees":false,"ReportsTo":2}
]);

$("#treeview").kendoTreeView({
    dataSource: homogeneous,
    dataTextField: "FullName"
});

但是,子元素会添加到根级别(您可以看到此元素运行here)。

有一个schema.model.children配置来设置将子项保存在根元素中的属性,我可以一次性添加所有内容,但如果我使用它,那么dataSource将停止使用远程数据(你可以看到)正在运行here)。

所以问题是,有没有办法使用pushCreate在HierarchicalDataSource上添加元素作为另一个的子元素?

2 个答案:

答案 0 :(得分:0)

我在写这个问题时找到答案:)

似乎我只是使用items作为子项的键,我不需要指定schema.model.children选项,因此树仍可以使用远程数据:

homogeneous.pushCreate(
    {"EmployeeId":2,"FullName":"Andrew Fuller","HasEmployees":true,"ReportsTo":null,
        "items": [
            {"EmployeeId":1,"FullName":"Nancy Davolio","HasEmployees":false,"ReportsTo":2},
            {"EmployeeId":3,"FullName":"Janet Leverling","HasEmployees":false,"ReportsTo":2},
            {"EmployeeId":4,"FullName":"Margaret Peacock","HasEmployees":false,"ReportsTo":2},
            {"EmployeeId":5,"FullName":"Steven Buchanan","HasEmployees":true,"ReportsTo":2},
            {"EmployeeId":8,"FullName":"Laura Callahan","HasEmployees":false,"ReportsTo":2}
        ]
    }
);

Here's the working example

我不知道这是否是非预期的功能,但是可以用于向根添加整个子树(这解决了我的问题)。但问题仍然存在,我可以使用pushCreate将子元素添加到另一个吗?

答案 1 :(得分:0)

您可以通过使用append附加节点元素来执行此操作。通过telerik查看此示例:http://dojo.telerik.com/AjIti/32