1。使用率のシナリオ
組織構造ツリー、通常、コード(コード)、PCODE(上部コード)、名前(組織名)などのフィールドを含む組織構造テーブルがあります
2。データを構築します(次のデータは組織データではなく、純粋に私が構成したデータです)
リスト<ツリー<TEST >> TREES = NEW ARRAYLIST <TREE <TESH>(); tests.Add(new Test( "0" "、" "、" me ")); tests.add(new Test(" 1 "、" 0 "、" Technical Learning "); tests.add(new Test(" 2 "、" 0 "、" "java")) "1"、 "linux"))))))(new Test( "9"、 "2"、 "Cycling")) "4"、 "sql")))) "etc")); tests.add(new Test( "19"、 "4"、 "など")); tests.add(new Test( "20"、 "5"、 "" ")); tests.ad(new test(" 20 "、" 5 "、")) "etc")); tests.add(new Test( "19"、 "4"、 "など")); tests.add(new Test( "20"、 "5"、 "")); tests.ad(new Test( "20"、 "5"、 "など));
3。ソースコード
tree.java
パッケージpers.kangxu.datautils.bean.tree; Import java.util.arraylist; Import java.util.list; Import java.util.map; Import com.alibaba.fastjson.json; ID; / ***ノードテキストを表示*/プライベート文字列テキスト。 / ** *ノードステータス、開く閉じた */ private文字列state = "open"; / ** *ノードが選択されているかどうか真のfalse */ private boolean checked = false; / ***ノード属性*/プライベートリスト<Map <String、Object >>属性; / ***ノードの子供*/プライベートリスト<tree <t >> children = new arrayList <tree <t >>(); / ** *親ID */ private string parentID; / ***親ノードがあるかどうか*/ private boolean isparent = false; / ***子ノードがあるかどうか*/ private boolean ischildren = false; public string getId(){return id; } public void setid(string id){this.id = id; } public string getText(){return text; } public void settext(string text){this.text = text; } public string getState(){return state; } public void setState(string state){this.state = state; } public boolean ischecked(){return checked; } public void setChecked(boolean checked){this.checked = checked; } public list <Map <String、object >> getattributes(){return attributes; } public void setattributes(list <map <string、object >> astributes){this.attributes = attributes; } public list <tree <t >> getChildren(){return children; } public void setChildren(list <tree <t >>子供){this.children = children; } public boolean isparent(){return isparent; } public void setParent(boolean isparent){this.isparent = isparent; } public boolean ischildren(){return ischildren; } public void setchildren(boolean ischildren){this.ischildren = ischildren; } public string getParentId(){return derparentID; } public void setParentID(string derparentId){this.parentid = darterid; } public Tree(string id、string text、string state、boolean checked、list <map <string、object >> astributes、list <tree <t >> children、boolean isparent、boolean ischildren、string dareid){super(); this.id = id; this.text = text; this.state = state; this.checked = checked; this.attributes = attributes; this.children = children; this.isparent = isparent; this.ischildren = ischildren; this.parentid = dartid; } public tree(){super(); } @Override public String toString(){return json.tojsonstring(this); }}buildtree.java
パッケージpers.kangxu.datautils.common.tree; Import java.util.arraylist; import java.util.list; Import pers.kangxu.datautils.bean.tree.tree; @author kangxu2 2017-1-7 * * @param nodes * @return */ public static <t> tree <t> build(list <tree <t >> nodes){if(nodes == null){return null; } list <tree <t >> topnodes = new arrayList <tree <t >>(); for(tree <t> children:nodes){string pid = children.getparentid(); if(pid == null || "" .equals(pid)){topnodes.add(children);続く; } for(tree <t> parent:nodes){string id = parent.getId(); if(id!= null && id.equals(pid)){parent.getChildren()。add(children); children.setParent(true); parent.setchildren(true);続く; }}} tree <t> root = new Tree <T>(); if(topnodes.size()== 0){root = topnodes.get(0); } else {root.setId( "-1"); root.setParentID( ""); root.setParent(false); root.setchildren(true); root.setchecked(true); root.setchildren(topnodes); root.settext( "トップレベルノード"); }ルートを返します。 }}BuildTreetester.java
パッケージPERS.KANGXU.DATAUTILS.TEST;インポートJava.util.arraylist; Import java.util.list; Import pers.kangxu.datautils.bean.tree.tree; import pers.kangxu.datautils.common.tree.buildtree; public class build build sitic buid(] List <tree <test >> trees = new ArrayList <tree <test >>();リスト<test> tests = new arraylist <test>(); tests.add(new test( "0"、 ""、 "me")); tests.add(new Test( "1"、 "0"、 "技術学習")); tests.add(new test( "2"、 "0"、 "rest")); tests.add(new Test( "3"、 "1"、 "Java")); tests.add(new Test( "4"、 "1"、 "Oracle")); tests.add(new test( "5"、 "1"、 "spring")); tests.add(new test( "6"、 "1"、 "springmvc")); tests.add(new test( "7"、 "1"、 "fastdfs")); tests.add(new Test( "8"、 "1"、 "Linux")); tests.add(new Test( "9"、 "2"、 "Cycling")); tests.add(new Test( "10"、 "2"、 "食事、飲酒、楽しみ")); tests.add(new Test( "11"、 "2"、 "Learning")); tests.add(new test( "12"、 "3"、 "string")); tests.add(new Test( "13"、 "4"、 "SQL")); tests.add(new Test( "14"、 "5"、 "IOC")); tests.add(new Test( "15"、 "5"、 "AOP")); tests.add(new Test( "16"、 "1"、 "")); tests.add(new test( "17"、 "2"、 "et")); tests.add(new Test( "18"、 "3"、 "")); tests.add(new Test( "19"、 "4"、 "など)); tests.add(new Test( "20"、 "5"、 "")); for(テストテスト:テスト){tree <test> tree = new Tree <test>(); tree.setid(test.getId()); tree.setParentID(test.getPid()); tree.settext(test.getText()); trees.add(tree); } tree <test> t = buildtree.build(trees); System.out.println(t); }} class test {private string id;プライベート文字列PID;プライベート文字列テキスト; public string getId(){return id; } public void setid(string id){this.id = id; } public string getPid(){return pid; } public void setPid(string pid){this.pid = pid; } public string getText(){return text; } public void settext(string text){this.text = text; } public Test(string id、string pid、string text){super(); this.id = id; this.pid = pid; this.text = text; } public test(){super(); } @Override public String toString(){return "test [id =" + id + "、pid =" + pid + "、text =" + text + "]"; }}4。操作結果
JSONデータ:
{"checked":true、 "children":[{"checked":false "、children":[{"checked":false ":" checked ":false、" children ":{" checked ":false、":[{"checked":fals ":fals、":[]、 "" "" pers ":" "" "pers": "" "perid": "" perid ":" "" perid ":" "perid": "" frue ":" "parent": "" parent ":" pern "Text": "string"}、{"checked":false、 "children":[]、 "id": "18"、 "parent":true、 "parentid": "3"、 "state": "open"、 "text": ""}]、 "id": "3"、 "parent":true、 "parentid": "" "" " false、 "Children":[{"checked":false "children":[]、 "id": "13"、 "parent":true、 "dartid": "4"、 "state": "open"、 "text": "sql"}、{"checked":fals、 "children":[]、 "id": "" "" "" "" "4" "et}]、" id ":" 4 "、" parent、 "dartid": "1"、 "state": "open"、 "text": "oracle"}、{"checked":false、 "children":[{"checked":fals、 "children":[]、 "": "14"、 "" "" 5 " }、{"checked":false、 "children":[]、 "id": "15"、 "parent":true、carentid ":" 5 "、" state ":" open "、" text ":" aop "}、{" checked ":fals、" false、 "false、" false ":[]、" id ":" "20"、 "" "" "" "" " ]、 "id": "5"、 "parent":true、 "darperid"、 "state": "open"、 "text": "spring"}、{"checked":fals、 "children":[]、 "id": "6": "parent":true、 "dartid": "1"、 "state": "springmvc" "" ":" springmvc "" "" "open": "open": "springmvc": "fired" "" "oppen [], "id": "7", "parent": true, "parentId": "1", "state": "open", "text": "fastdfs" }, { "checked": false, "children": [], "id": "8", "parent": true, "parentId": "1", "state": "open", "text": "linux" }, { "checked": false, "children": []、 "id": "16"、 "parent":true、 "darperid": "" 1 "、" state ":" open "、" text ":" et "}]、" id ":" 1 "、" parent ":true、" wartid ":" 0 "、" state ":" open "、" text ":" "checked": "fals": " "id": "9", "parent": true, "parentId": "2", "state": "open", "text": "cycling" }, { "checked": false, "children": [], "id": "10", "parent": true, "parentId": "2", "state": "open", "text": "eating, drinking and having fun" }, { "checked": false, "children": []、 "id": "11"、 "parent":true、 "parentid": "2"、 "state": "open"、 "text": "Learning"}、{"checked":fals、 "children":[]、 "id": "17"、 "parent":true、 "dartid": "2"、 "" "2" ":" "" 2 ":" "" 2 ":" "" 2 ":" "" 2 "": "" 2 " "parentId": "0", "state": "open", "text": "interest" } ], "id": "0", "parent": false, "parentId": "", "state": "open", "text": "about me" } ], "id": "-1", "parent": false, "parentId": "", "state": "open", "text": "Top Node"}上記は、データベーステーブルのコンテンツに基づいてツリー構造JSONデータを生成するJavaメソッドの完全な内容です。誰もがwulin.comをサポートできることを願っています〜