似乎无法将节点添加到Kendo UI TreeView

时间:2016-03-09 22:37:34

标签: javascript kendo-ui treeview kendo-treeview

我试图将Node从文本框添加到现有的树视图

输入

<input id="appendNodeText" value="Node" class="k-textbox">

按钮

<button type="button" class="btn btn-blue" id="addTopLevel">Add Top Level Menu</button>

的javascript

 <script>
     // button handler
      $("#addTopLevel").click(append);

     handleTextBox = function (callback) {
         return function (e) {
            if (e.type != "keypress" || kendo.keys.ENTER == e.keyCode) {
               callback(e);
             }
        };
     };

     var append = handleTextBox(function (e) {
     var selectedNode = treeview.select();
     console.log(selectedNode);
     // 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({
            text: $("#appendNodeText").val()
            }, selectedNode);
     });

所以我最终得到了这个点击事件,在其中传递了“追加”

老实说,我不理解handleTextBox,也没有附加

树视图确实“有效”,但我想知道它是否是问题的一部分

   var treeview = $("#treeview").kendoTreeView({
                                expanded: true,
                                dragAndDrop: true,
                                dataSource: homogeneous,
                                dataTextField: "ReportGroupName" //"name" //"id" // "FullName"
                                ,
                                change: function(e) {
                                    console.log("Change", this.select());
                                }
                            });

PER a Answer:

我试过这个

$("#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');
    }
});

console.log写出'in this'然后'1'

所以我甚至没有选择任何节点......一定是错的

这是我的json

[{"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}]

1 个答案:

答案 0 :(得分:0)

据我所知,您希望将文本框值作为节点附加到树视图中的选定节点。我已经为工作功能创建了一个jsfiddle: -

http://jsfiddle.net/GHdwR/468/

点击按钮事件: -

$("#addTopLevel").click(function() {
    if (treeview.select().length) {
        treeview.append({
            text: $("#appendNodeText").val()
        }, treeview.select());
    } else {
        alert("please select tree node");
    }
});