【问题标题】:kendo treeview change datasource on the flykendo treeview 即时更改数据源
【发布时间】:2014-05-25 00:37:39
【问题描述】:

我有一个连接到 OData 源的 kendoTreeView,如官方网站上的演示中所示。我需要将 kendoTreeView 的数据源更改为其他内容。这是带有脚本的 html。

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

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

<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);

    });
});

【问题讨论】:

    标签: html kendo-ui treeview datasource


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      相关资源
      最近更新 更多