2014-04-22 68 views
2

所以我一直在使用jstree一段时间,但仍然无法处理它,这几乎是一个令人头疼但很好,它决定我们会用它。我使用的数据来自HTML(不涉及JSON)。我遇到的问题是我不确定如何设置某些节点不是文件夹。每个节点都有一个类,并且基于该类,我可以更改它的图标,但是如果用户尝试发送这些节点内部不应该是文件夹的任何节点,他们将能够。我需要防止这种方式,但是到目前为止我测试过的每件事都根本不起作用。jstree防止移动节点到子节点

$("jstree").jstree({ 
      "core": { 
       "animation": 0, 
       "check_callback": true 
      }, 
      rules: { draggable: "all" }, 
      "dnd": { 
       "drop_finish": function (data) { 
        if (data.o.attr("rel") === "ds") { 
         //update chart with new data here? 
         //using data.o.attr("id") 
        } 
       }, 
       "drag_check": function (data) { 
        if (data.r.attr("rel") != "ds") { 
         return false; 
        } 
        return { 
         after: false, 
         before: false, 
         inside: true 
        }; 
       } 
      }, 
      "crrm": { 
       "move": { 
        "check_move": function (data) { 
         // alert(data.r.attr("id")); 
         if (data.r.attr("id") == "999") { 
          return false; 
         } 
         else { 
          return true; 
         } 
        } 
       } 
      }, 

      "plugins": ["dnd", "crrm"] 
     }); 

这就是我用来创建我的树。另外,我不能禁止拖放,因为如果用户想要移动某些项目,但显然用户不应该能够将某些东西拖入任何不是文件夹的东西。

在此先感谢您的帮助,

问候,

阿德里安。

回答

1

我通过使用Types插件jstree plugins完成了此操作。在那里你可以定义节点的类型,并设置允许哪些类型为儿童的变量valid_children。这意味着用户也不能将限制类型的DnD节点放入节点。

在我的例子中,我有一个类型为“book”的文件夹,它可以将“文件夹”和“文件”节点作为子节点。 “文件”类型根本不能有任何子项,因为valid_children被定义为空。

$('#' + tree_id) 
    .jstree({ 
    'core' : { 
     'check_callback' : true, //needed to enable create, remove, rename... events 
     "themes" : { 
     "stripes" : true 
     } 
    }, 
    "types" : { 
     "book" : { 
     "valid_children" : ["folder", "file"], 
     "icon" : "img/1397762742_book.png" 

     }, 
     "folder" : { 
     "valid_children" : ["folder", "file"], 
     "icon" : "img/1397751666_folder.png" 

     }, 
     "file" : { 
     "valid_children" : [], 
     "icon" : "img/1397752227_page.png" 
     } 
    }, 
    "contextmenu" : { 
     "items" : customMenu 
    }, 
    "plugins" : ["sort", "dnd", "contextmenu", "types"] 

    }); 

type属性可以添加新节点

tree.create_node("#", { 
     "text" : "sample node", 
     "type" : "file" 
     }); 

或通过使用set_type功能时进行设置。 API