【问题标题】:KendoUI undefined Node issue when trying to add to root parent node尝试添加到根父节点时的 KendoUI 未定义节点问题
【发布时间】:2016-03-11 21:10:57
【问题描述】:

我已经看到了同样问题的其他问题,数据似乎是大多数时候的问题。

我确实创建了一个 jsfiddle http://jsfiddle.net/gwvoqns8/

(始终记住必须使用 http,而不是 https )

应该能证明问题

  1. 我想让在文本框中输入的任何内容都显示为另一个任何名称的命名节点...
  2. 我正在使用的代码似乎强制我选择一个现有的父节点,我不希望这样。

为什么说“未定义”有点烦人

$("#addTopLevel").click(function () {
    console.log('in this');
    if (treeview.select().length) {
        console.log('1');
        treeview.append({
            text: $("#appendNodeText").val()
        }, treeview.select());
    } else {
        //alert("please select tree node");
        console.log('2');
    }
});

【问题讨论】:

    标签: javascript jquery kendo-ui treeview kendo-treeview


    【解决方案1】:

    试试这个:

    var ds = new kendo.data.HierarchicalDataSource({
        data: [
            {"Id":1,"ReportGroupName":"Standard Reports","ReportGroupNameResID":null,"SortOrder":1},
            {"Id":2,"ReportGroupName":"Custom Reports","ReportGroupNameResID":null,"SortOrder":2},
            {"Id":3,"ReportGroupName":"Retail Reports","ReportGroupNameResID":null,"SortOrder":3},
            {"Id":4,"ReportGroupName":"Admin Reports","ReportGroupNameResID":null,"SortOrder":5},
            {"Id":5,"ReportGroupName":"QA Reports","ReportGroupNameResID":null,"SortOrder":4}
        ]
    });
    
    
    var treeview = $("#treeview").kendoTreeView({
        dataSource: ds,
        dataTextField: "ReportGroupName",
        loadOnDemand: false
    }).data("kendoTreeView");
    
    treeview.expand(".k-item");
    
    $("#addTopLevel").click(function(e) {
        var selectedNode = treeview.select();
    
        // passing a falsy value as the second append() parameter
        // will append the new node to the root group
        if (selectedNode.length == 0) {
            selectedNode = null;
        }
    
        treeview.append({
            ReportGroupName: $("#appendNodeText").val()
        }, selectedNode);
    });
    

    【讨论】:

    • 甜!如果我想在不先选择节点的情况下添加节点父节点怎么办?我想我发现的if语句仪式根本不需要,对吧?
    • 哦,我还注意到,在我的 REAL 代码中,我确实有整个孩子的东西,所以用那个 IF 语句,我选择一个父节点,然后它使用那个 ReportGroupName 来结束添加到儿童级别...呃不是我想要的
    • kendo.all.min.js:41 Uncaught TypeError: Cannot read property 'loaded' of undefined
    • 再次,伟大的工作,非常感谢再次感谢
    【解决方案2】:

    我注意到你可以这样做

    treeview.append({
        ReportGroupName: $("#appendNodeText").val()
    }, null);
    

    但是,OP 的答案会更好,因为如果您愿意,您将能够添加到子节点,即使您说您不想这样做。

    干杯

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多