Skip to content

Latest commit

 

History

History
104 lines (85 loc) · 2.78 KB

README.md

File metadata and controls

104 lines (85 loc) · 2.78 KB

JTree

Build Status License

Simple fast generation tree and traversing tree tools

How to use

1.use maven

    <dependency>
      <groupId>com.github.xchendeveloper</groupId>
      <artifactId>JTree</artifactId>
      <version>1.0</version>
    </dependency>

2.download code by command :

git clone https://github.com/xchendeveloper/JTree.git

Get JTree

Specify the name of id and parentId of dataItem

Jtree jtree = JTree.init("mytree","xxxid","xxxpid");

Using "id" and "pid" as data items by default

Jtree jtree = JTree.init("mytree");

Generate tree

Node tree = jtree.generalTree(dataList);
// For details, please see test case

we can also do it when we init JTree

Node tree = JTree.init("mytree").generalTree(dataList);

Transfer tree to map

Map<String,Object> map = tree.toMap();

if your dateItem is not javaBean,toMap() function will failed,you should implement your own NodeData2MapGenerator,eg:

public class Bean2MapGenerator implements NodeData2MapGenerator {
    public Map generate(Object obj) {
        return BeanUtil.beanToMap(obj);
    }
}

and use your generator like this:

tree.toMap(new Bean2MapGenerator);

Transfer tree to node list

Node node = JTree.init("mytree").generalTree(dataList);
List<Node> nodeList = new ArrayList<Node>();
JTree.get("mytree").treeToNodeList(node,nodeList);

Getting node through id

Node node = JTree.init("mytree").generalTree(dataList);
Node node = JTree.get("mytree").getNodeById(node,3);

Get all the sub nodes under the specified node

Node node = JTree.init("mytree").generalTree(dataList);
Node node1 = JTree.get("mytree").getNodeById(node,3);
// getSubNodeList(Node node, boolean recursion, boolean includeSelf)
List<Node> nodeList = JTree.get("mytree").getSubNodeList(node1,true,true); 

Get all the sub nodes under the specified nodeid under the tree

Node node = JTree.init("mytree").generalTree(dataList);
List<Node> nodeList = JTree.get("mytree").getTreeSubNodeListById(node,3,true,true);

Get a list of the node Id of the specified node

Node node = JTree.init("mytree").generalTree(dataList);
Node node1 = JTree.get("mytree").getNodeById(node,3);
List<Object> idList = JTree.get("mytree").getSubNodeIdList(node1,true,true);

Get a list of the node Id of the specified nodeid under the tree

Node node = JTree.init("mytree").generalTree(dataList);
List<Object> nodeList = JTree.get("mytree").getTreeSubNodeIdListById(node,3,true,true);