JBOSS 點滴

          豐豐的博客

          springboot中樹形結構的開發

          POM中引入
                  <!--JSON樹形結構 轉換-->
                  
          <dependency>
                      
          <groupId>net.sf.json-lib</groupId>
                      
          <artifactId>json-lib</artifactId>
                      
          <version>2.4</version>
                      
          <classifier>jdk15</classifier>
                  
          </dependency>
          實體類創建
          import net.sf.json.JSONArray;

          import java.util.ArrayList;
          import java.util.List;

          /**
           * 構造目錄JSON樹
           * Created by   network on 2019/5/22
           
          */
          public class TreeBuilder {

              List
          <Node> nodes = new ArrayList<>();

              
          public String buildTree(List<Node> nodes) {

                  TreeBuilder treeBuilder 
          = new TreeBuilder(nodes);

                  
          return treeBuilder.buildJSONTree();
              }

              
          public TreeBuilder() {
              }

              
          public TreeBuilder(List<Node> nodes) {
                  
          super();
                  
          this.nodes = nodes;
              }

              
          // 構建JSON樹形結構
              public String buildJSONTree() {
                  List
          <Node> nodeTree = buildTree();
                  JSONArray jsonArray 
          = JSONArray.fromObject(nodeTree);
                  
          return jsonArray.toString();
              }

              
          // 構建樹形結構
              public List<Node> buildTree() {
                  List
          <Node> treeNodes = new ArrayList<>();
                  List
          <Node> rootNodes = getRootNodes();
                  
          for (Node rootNode : rootNodes) {
                      buildChildNodes(rootNode);
                      treeNodes.add(rootNode);
                  }
                  
          return treeNodes;
              }

              
          // 遞歸子節點
              public void buildChildNodes(Node node) {
                  List
          <Node> children = getChildNodes(node);
                  
          if (!children.isEmpty()) {
                      
          for (Node child : children) {
                          buildChildNodes(child);
                      }
                      node.setChildren(children);
                  }
              }

              
          // 獲取父節點下所有的子節點
              public List<Node> getChildNodes(Node pnode) {
                  List
          <Node> childNodes = new ArrayList<>();
                  
          for (Node n : nodes) {
                      
          if (pnode.getId().equals(n.getPid())) {
                          childNodes.add(n);
                      }
                  }
                  
          return childNodes;
              }

              
          // 判斷是否為根節點
              public boolean rootNode(Node node) {
                  
          boolean isRootNode = true;
                  
          for (Node n : nodes) {
                      
          if (node.getPid().equals(n.getId())) {
                          isRootNode 
          = false;
                          
          break;
                      }
                  }
                  
          return isRootNode;
              }

              
          // 獲取集合中所有的根節點
              public List<Node> getRootNodes() {
                  List
          <Node> rootNodes = new ArrayList<>();
                  
          for (Node n : nodes) {
                      
          if (rootNode(n)) {
                          rootNodes.add(n);
                      }
                  }
                  
          return rootNodes;
              }

              
          public static class Node {

                  
          private String id;
                  
          private String pid;
                  
          private String name;
                  
          private List<Node> children;

                  
          public Node() {
                  }

                  
          public Node(String id, String pid, String name) {
                      
          super();
                      
          this.id = id;
                      
          this.pid = pid;
                      
          this.name = name;
                  }

                  
          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 getName() {
                      
          return name;
                  }

                  
          public void setName(String name) {
                      
          this.name = name;
                  }


                  
          public List<Node> getChildren() {
                      
          return children;
                  }

                  
          public void setChildren(List<Node> children) {
                      
          this.children = children;
                  }
              }
          }
          mapper.xml
              <!--根據條件查詢返回Node-->
              
          <select id="findByItemtypeTree" parameterType="BsItemtype" resultType="Node">
                  SELECT upcode as pid,itemname as name,itemtypecode as id FROM 表名 WHERE 1=1
                  
          <if test="upcode != null  and upcode != '' ">
                      and upcode = #{值}
                  
          </if>
              
          </select>
          controller:返回結果
              //樹形結構
              @RequestMapping("left")
              ModelAndView left() 
          throws JSONException {
                  String mavStr 
          = "";
                  BsItemtype queryItemtype 
          = new BsItemtype();
                  
          //查詢根目錄樹
                
          //  queryItemtype.setUpcode("00");
                  
          // 獲取全部目錄節點
                  List<TreeBuilder.Node> itemtypeTree = itemtypeService.findByItemtypeTree();

                  
          // 拼裝樹形json字符串
                   mavStr = new TreeBuilder().buildTree(itemtypeTree);
                  mavStr 
          = mavStr.replaceAll("\"children\"","\"nodes\"");
                  mavStr
          = mavStr.replaceAll("\"id\"","\"value\"");
                  mavStr 
          = mavStr.replaceAll("\"name\"","\"text\"");
                  ModelAndView mav 
          = new ModelAndView("/bs/itemtype/itemtypeLeft.html");//配置返回路徑
                  System.out.println(mavStr);
                  mav.addObject(
          "mavStr", mavStr.toString());
                  
          return mav;
              }
          DAO層:
          List<TreeBuilder.Node> findByItemtypeTree();

          html
          <!DOCTYPE html>
          <html xmlns:th="http://www.thymeleaf.org">
          <html lang="en">
          <head>
              
          <meta charset="UTF-8">
              
          <title></title>

              
          <!-- Required Stylesheets -->
              
          <link href="/oastyle/css/bootstrap.css" rel="stylesheet">
              
          <!-- Required Javascript -->
              
          <script src="/js/jquery.js"></script>
              
          <script src="/js//bootstrap-treeview.js"></script>
          </head>
          <body>
          <div id="tree"></div>
          <script th:inline="javascript">
          var tree = eval([[${mavStr}]]);
                   
          function getTree() {
                  
          // Some logic to retrieve, or generate tree structure
                  return tree;
              }

              $('#tree').treeview({
                  data: getTree(),
                  enableLinks: 
          true});

              $('#tree').on('nodeSelected',
          function(event, data) {
                    window.parent.itemtypeRight.location.href
          ="/itemtype/list?upcode="+data.value;
              });

          </script>

          </body>
          </html>

          posted on 2019-05-21 19:08 半導體 閱讀(2071) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 望江县| 南投县| 孟村| 山阳县| 柳江县| 平果县| 漳州市| 张北县| 巩留县| 云安县| 呼伦贝尔市| 蒙城县| 建湖县| 深水埗区| 龙南县| 正阳县| 云阳县| 乌鲁木齐县| 鄂伦春自治旗| 阿合奇县| 彰武县| 虹口区| 兴国县| 永和县| 河西区| 疏附县| 水城县| 乐陵市| 敦化市| 长宁区| 延边| 久治县| 中山市| 鄂州市| 泊头市| 钦州市| 清水河县| 饶平县| 黄龙县| 正蓝旗| 婺源县|