首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >kendo treeview动态更改数据源

kendo treeview动态更改数据源
EN

Stack Overflow用户
提问于 2014-04-11 10:58:53
回答 1查看 4.6K关注 0票数 2

我有一个连接到OData源代码的kendoTreeView,就像官方网站上的演示中给出的一样。我需要将kendoTreeView的dataSource更改为其他值。下面是包含脚本的html。

当我单击change按钮时,树会发生更改,但所有节点都显示为未定义。它无法显示新的dataTextField值。当我恢复它时,即使没有设置dataTextField,它也可以工作。

我需要更改树的dataSource,或者销毁树,从DOM中删除它,然后添加一个新的树。有什么帮助或建议吗?

代码语言:javascript
运行
复制
<body>
    <input type="button" id="expandAllNodes" value="expandAllNodes" />
    <input type="button" id="changedatasource" value="changedatasource" />
    <input type="button" id="revert" value="revert" />
    <span>First Tree</span>
    <div id="treeview"></div>
    <br/>
    <span>Second Tree</span>
    <div id="treeview2"></div>

    <!--<div id="example" class="k-content">
        <div id="treeview" class="demo-section"></div>-->

        <script>


        </script>

        <style scoped>
            #example {
                text-align: center;
            }

            .demo-section {
                display: inline-block;
                vertical-align: top;
                width: 320px;
                height: 300px;
                text-align: left;
                margin: 0 2em;
            }
        </style>
    <!--</div>-->
</body>

   $(document).ready(function () {
    // TreeView bound to Categories > Products > OrderDetails

    var viewModel = {
        self: this,
        OrderDetails: {
            type: "odata",
            transport: {
                read: {
                    url: function (options) {
                        return kendo.format("http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products({0})/Order_Details", options.ProductID);
                    }
                }
            },
            schema: {
                model: {
                    hasChildren: function () {
                        return false;
                    }
                }
            }
        },
        Products: new kendo.data.HierarchicalDataSource({
            type: "odata",
            transport: {
                read: {
                    url: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
                }
            },
            schema: {
                model: {
                    id: "ProductID",
                    hasChildren: "Order_Details",
                    children: {
                        type: "odata",
                        transport: {
                            read: {
                                url: function (options) {
                                    return kendo.format("http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products({0})/Order_Details", options.ProductID);
                                }
                            }
                        },
                        schema: {
                            model: {
                                hasChildren: function () {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
        }),        

        Categories: new kendo.data.HierarchicalDataSource({
            type: "odata",
            transport: {
                read: {
                    url: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Categories"
                }
            },
            schema: {
                model: {
                    hasChildren: "Products",
                    id: "CategoryID",
                    children: {
                        type: "odata",
                        schema: {
                            model: {
                                id: "ProductID",
                                hasChildren: "Order_Details",
                                children: {
                                    type: "odata",
                                    transport: {
                                        read: {
                                            url: function (options) {
                                                return kendo.format("http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products({0})/Order_Details", options.ProductID);
                                            }
                                        }
                                    },
                                    schema: {
                                        model: {
                                            hasChildren: function () {
                                                return false;
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        transport: {
                            read: {
                                url: function (options) {
                                    return kendo.format("http://demos.telerik.com/kendo-ui/service/Northwind.svc/Categories({0})/Products", options.CategoryID);
                                }
                            }
                        }
                    }
                }
            }
        })
    }

    $("#treeview").kendoTreeView({
        dataSource: viewModel.Categories,
        dataTextField: ["CategoryName", "ProductName", "OrderID"],
        checkboxes: {
            checkChildren: true
        }
    });
    $("#treeview2").kendoTreeView({
        dataSource: viewModel.Products,
        dataTextField: ["ProductName", "OrderID"],
        checkboxes: {
            checkChildren: true
        }
    });
    ko.applyBindings(viewModel);

    $("#expandAllNodes").click(function () {
        var treeView = $("#kendoTreeView").data("kendoTreeView");
        treeView.expand(".k-item");
    });

    $("#changedatasource").click(function () {
        var treeview: kendo.ui.TreeView = $("#treeview").data("kendoTreeView");
        //treeview.destroy();

        treeview.setOptions({ dataTextField: ["ProductName", "OrderID"] });
        treeview.setDataSource(viewModel.Products);

    });
    $("#revert").click(function () {
        var treeview: kendo.ui.TreeView = $("#treeview").data("kendoTreeView");

        //treeview.setOptions({ dataTextField: ["CategoryName", "ProductName", "OrderID"] });
        treeview.setDataSource(viewModel.Categories);

    });
});
EN

回答 1

Stack Overflow用户

发布于 2015-06-22 22:12:28

将数据源设置为HierarchicalDataSource的新实例,请参见此处:http://d585tldpucybw.cloudfront.net/forums/datasource-property-update-and-refresh。Alex Gyoshev直接链接到Dojo:http://dojo.telerik.com/@gyoshev/iJIhI

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23002716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档