简体   繁体   English

在angularjs中构建树结构

[英]Building a tree structure in angularjs

I would like to build a tree dynamically in angularjs, the structure is retrieved as follows from the database. 我想在angularjs中动态构建树,从数据库中检索结构如下。

[{
  "TreeName":"String content",
  "Level":0,

}],
[{
  "TreeName":"String content",
  "Level":0,

}],
[{
    "TreeName":"String content",
    "Level":1,

}],
[{
    "TreeName":"String content",
    "Level":2,

}],
[{
    "TreeName":"String content",
    "Level":2,

}],
[{
    "TreeName":"String content",
    "Level":1,

}],
[{
    "TreeName":"String content",
    "Level":2,

}],
[{
    "TreeName":"String content",
    "Level":3,

}],
[{
    "TreeName":"String content",
    "Level":1,
}]

If level 0 then its main node and level 1 becomes parent node and level 2 becomes that parents child node and level 3 becomes that childs child node and so forth. 如果级别0,则其主节点和级别1成为父节点,级别2成为该父子节点,而级别3成为该子节点,依此类推。

I have tried many examples and doesnt work, the only thing is that I thought was to do a lot of if statements but i need a shorter and more efficient way. 我已经尝试了许多示例,但没有用,唯一的是,我认为应该做很多if语句,但是我需要一种更短,更有效的方法。

You can use jsTree.jsTree is jquery plugin, that provides interactive trees. 您可以使用jsTree.jsTree是jquery插件,它提供交互式树。 It is absolutely free, open source and distributed under the MIT license. 它是完全免费的,开源的,并根据MIT许可进行分发。 jsTree is easily extendable, themable and configurable, it supports HTML & JSON data sources and AJAX loading. jsTree易于扩展,可定义和配置,它支持HTML和JSON数据源以及AJAX加载。 You can find more detail on https://www.jstree.com/ 您可以在https://www.jstree.com/上找到更多详细信息

Here is the code 这是代码

$('#jstree_demo').jstree({
  "core" : {
    "animation" : 0,
    "check_callback" : true,
    "themes" : { "stripes" : true },
    'data' : {
      'url' : function (node) {
        return node.id === '#' ?
          'ajax_demo_roots.json' : 'ajax_demo_children.json';
      },
      'data' : function (node) {
        return { 'id' : node.id };
      }
    }
  },
  "types" : {
    "#" : {
      "max_children" : 1,
      "max_depth" : 4,
      "valid_children" : ["root"]
    },
    "root" : {
      "icon" : "/static/3.3.2/assets/images/tree_icon.png",
      "valid_children" : ["default"]
    },
    "default" : {
      "valid_children" : ["default","file"]
    },
    "file" : {
      "icon" : "glyphicon glyphicon-file",
      "valid_children" : []
    }
  },
  "plugins" : [
    "contextmenu", "dnd", "search",
    "state", "types", "wholerow"
  ]
});

Also you can find working demo on https://www.jstree.com/demo/ 您也可以在https://www.jstree.com/demo/上找到有效的演示

And here is the format of data to use jstree https://www.jstree.com/docs/json/ 这是使用jstree https://www.jstree.com/docs/json/的数据格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM