ajax - Kendo UI : Can't add a node to treeview when using custom schema

标签 ajax kendo-ui kendo-treeview

我有一个使用以下数据源初始化的 Kendo Tree :

var dataSource = new kendo.data.HierarchicalDataSource({
            transport: {
                read: {
                url: '/Quota/Home/GetTemplateHierarchy',
                dataType: 'json',
                data: { hierarchyID: hierarchyID, quotaSetID: quotaSetID, batchSize: 10 }
            }
            },
            schema: {
                model: {
                    id: 'id',
                    hasChildren: 'hasChildren',
                    children: 'items',
                    fields: {
                        text: 'text'
                    }
                }
            }
        });

有谁知道如何为这个数据源添加和创建一个新节点?我试过通用的 treeview.append({ text: "Boo"}) 但它没有做任何事情。我已成功删除节点,但似乎无法添加任何节点。文档不清楚如何在使用自定义模式时添加任何内容。

最佳答案

保罗,我想提出另一个解决方案......





    <!-- Kendo UI Web styles-->
    <link href="../styles/kendo.common.min.css" rel="stylesheet" type="text/css"/>
    <link href="../styles/kendo.default.min.css" rel="stylesheet" type="text/css"/>

    <!-- Kendo UI Web scripts-->
    <script src="../js/jquery.min.js" type="text/javascript"></script>
    <script src="../js/kendo.web.min.js" type="text/javascript"></script>

    <!-- Local Styles -->
    <style type="text/css">
    </style>

    <!-- Initialize Form Elements -->
    <script type="text/javascript">
        $(document).ready(function () {
            function loadMore() {
                var uid = $(this).data("uid");
                var node = tree.findByUid(uid);
                tree.insertBefore(content, node);
                tree.remove(node);
                addLoadMore(".k-i-pencil");
            }

            function addLoadMore(clss) {
                $(clss, tree.element).closest(".k-item").on("click", loadMore);
            }

            var content = [
                {
                    text :"node1",
                    items:[
                        { text:"node1.1" },
                        { text:"node1.2" },
                        { text:"node1.3", spriteCssClass:"k-icon k-i-pencil" },
                        { text:"node1.4" }
                    ]
                }
            ];

            var tree = $("#tree").kendoTreeView({
                dataSource:content
            }).data("kendoTreeView");
            addLoadMore(".k-i-pencil");
        });
    </script>

</head>
<body>
<div id="tree"></div>
</body>
</html>

在这里,我创建了一个从 JSON 加载内容的树(它应该替换为您的 ajaxAntiForgery )。树中有一个节点有一个图标(k-i-pencil)。然后我调用一个函数 addLoadMore ,它用 k-i-pencil 拦截节点上的点击,并将新内容添加到这个节点 - 使用 insertBefore 在带有 k-i-pencil 的内容之前插入新内容,然后删除旧节点)。
我认为这个示例与您使用按钮所做的非常相似。
因此,查看 loadMore 函数以了解我如何检测与我单击的位置相对应的 node(我提取 uid 并使用 uid 找到具有该 tree.findByUid 的节点)。
最后,我删除原始节点(调用 tree.remove )并使用 k-i-pencil 再次设置新节点的拦截器。
希望这与您拥有的非常接近。

关于ajax - Kendo UI : Can't add a node to treeview when using custom schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498734/

相关文章:

javascript - 当嵌套在 Kendo 网格中时如何重新加载 Kendo TabStrip 选项卡

kendo-ui - Kendo UI TreeView - 如何将内容从一个 Treeview 复制到另一个 Treeview ?

jquery - 如何最初根据数据源的字段检查 Kendo Treeview 中的复选框

javascript - AJAX:jQuery ajax api 与 Javascript xhr

javascript - 如何从异步调用返回响应?

javascript - 将回调函数传递给 jQuery AJAX 成功函数

javascript - 在ajax请求之前从文本框中删除焦点

kendo-ui - 我如何检查节点是否在 Kendo treeView 中展开

jquery - 清除 Kendo 多重选择的选定值

javascript - 获取树节点的所有uid