jstree prevent moving node into child nodes

大城市里の小女人 提交于 2019-12-06 07:47:35

I accomplished this by using the Types plugin jstree plugins. There you can define types of nodes and set the valid_children variable which types are allowed to be children. That means that users also cannot DnD nodes of the restricted type into the node.

In my example below I have a type "book" that could have "folder" and "file" nodes as children. The "file" type cannot have any children at all, because valid_childrenis defined empty.

    $('#' + 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"]

  });

The type attribute can be set when adding a new node

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

or by using the set_type function. API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!