js實現目錄樹

第一步:HTML結構css

1 <div class="folderDiv">
2    <p>目錄</p>
3    <ul class="folder"></ul>
4 </div>

第二步:CSS樣式html

1 /*目錄樹*/
2 .folderDiv {width: 14%;float: left;height: auto;overflow: auto;border:1px solid #ccc;border-top:2px solid #15a4fa;}
3 .folderDiv p{height: 25px;padding-left: 6px;line-height: 25px;}
4 .folderDiv .folder{width: 100%;height:396px;}
5 .folder li {list-style: none;}
6 .folder li span{display: inline-block;height: 25px;line-height: 25px;padding-left: 14px;font-size: 12px;white-space: nowrap;}
7 .folder li span:hover{cursor: pointer;}

第三步:JS函數ajax

  1 //建立目錄樹
  2 folderAjax();
  3 //ajax請求
  4 function folderAjax() {
  5     var folder; //用於接收後臺獲取的目錄樹對象
  6     var folder_span; //ajax獲取目錄樹下的全部span數量,最後賦值給folder_span_click
  7     var folder_span_click; //目錄樹點擊
  8     $.ajax({
  9         type: "post",
 10         url: "/datawarning/warningCreate/getFolderInfoList ",
 11         async: true,
 12         success: function(res) {
 13             folder = res.beans; //當前目錄樹存在的對象
 14 
 15             //重置目錄樹內容
 16             $('.folder').html('');
 17             var span = $('<span></span>')
 18             var li = $('<li></li>');
 19             li.append(span);
 20             $('.folder').append(li);
 21 
 22             //得到目錄樹級數
 23             var folderLevelCode_Array = [];
 24             for(var i = 0; i < folder.length; i++) {
 25                 if(folderLevelCode_Array.indexOf(folder[i].folderLevelCode) == -1) {
 26                     folderLevelCode_Array.push(folder[i].folderLevelCode);
 27                 }
 28             }
 29 
 30             /*按照目錄級別排序後的數組*/
 31             function compare(property) {
 32                 return function(a, b) {
 33                     var value1 = a[property];
 34                     var value2 = b[property];
 35                     return value1 - value2;
 36                 }
 37             }
 38             /*目錄級別排序排序*/
 39             folderLevelCode_Array.sort();
 40             //按目錄層級排序目錄樹
 41             folder.sort(compare('folderLevelCode'));
 42             //按目錄ID排序目錄樹
 43             folder.sort(compare('folderId'));
 44 
 45             //設置父元素自定義屬性做upperFolderId
 46             $('.folder').children('li').children('span').attr('data-folderId', folder[0].upperFolderId);
 47 
 48             /*子目錄級別計算*/
 49             for(var m = 0; m < folder.length; m++) {
 50                 //找到父級目錄元素
 51                 var parent = $("[data-folderId=" + folder[m].upperFolderId + "]");
 52                 var span = $('<span></span>');
 53                 var ul = $('<ul></ul>');
 54                 ul.css('margin-left', 20 + 'px').css('display', 'none');
 55                 var li = $('<li></li>');
 56                 /*添加目錄樹信息*/
 57                 span.html(folder[m].folderName);
 58                 span.attr('data-folderId', folder[m].folderId);
 59                 span.attr('data-folderLevelCode', folder[m].folderLevelCode);
 60                 span.attr('data-upperFolderId', folder[m].upperFolderId);
 61                 li.append(span);
 62                 ul.append(li);
 63                 parent.parent().append(ul);
 64             }
 65 
 66             //顯示一級目錄
 67             $('.folder').children('li').children('ul').css('display', 'block');
 68             //獲取全部span標籤
 69             folder_span = $('.folder').find('span');
 70 
 71             //去除第一個標籤元素,從下標1開始循環
 72             for(var z = 1; z < folder_span.length; z++) {
 73                 if(folder_span.eq(z).parent().children().length >= 2) {
 74                     folder_span.eq(z).css('background', 'url(src/assets/img/right_15a4fa_12px.png) no-repeat left center');
 75                     folder_span.eq(z).parent().parent().insertAfter(folder_span.eq(z).parent().parent().parent().parent().children('li').children('span')); //排序目錄樹
 76                 }
 77             }
 78 
 79             //目錄樹樣式設置
 80             $('.folder').children('li').children('span').css('display', 'none')
 81             $('.folder').children('li').children('ul').css('margin-left', '5px')
 82             folder_span_click = folder_span;
 83 
 84             //目錄樹點擊
 85             folder_span_click.each(function(index, value) {
 86                 _this = $(this);
 87                 _this.click(function() {
 88                     if(_this.siblings('ul').children().length == 0) { //當前點擊的span沒有子元素
 89                         var treeParam = {}; //目錄樹請求對象---這是構建目錄樹的核心
 90                         treeParam.folderId = _this.attr('data-folderId');
 91                         //執行請求
 92                         $.ajax({
 93                             type: "get", //post,get
 94                             url: "", //請求地址
 95                             data: 'treeParam', //請求數據
 96                             async: true, //true同步,false異步
 97                             success: function(res) {
 98                                 //請求成功執行函數
 99                             }
100                         });
101                         //其餘span的變化
102                         $('.folder span').css('color', '#000');
103                         _this.css('color', '#15a4fa');
104                         _this.parent().parent().siblings('ul').find('ul').hide();
105                     } else { //當前點擊的span擁有子元素
106                         if(_this.siblings('ul').is(':hidden')) {
107                             _this.parent().parent().siblings('ul').find('ul').hide(); //隱藏其餘目錄
108                             var ned = _this.parent().parent().siblings().children('li');
109                             _this.parent().parent().siblings('ul').find('span').siblings('ul').siblings('span').css('background', 'url(src/assets/img/right_15a4fa_12px.png) no-repeat left center');
110                             $.each(ned, function(value, index) {
111                                 if(_this.children().length > 1) {
112                                     _this.children('span').css('background', 'url(src/assets/img/right_15a4fa_12px.png) no-repeat left center');
113                                 }
114                             });
115                             _this.siblings('ul').css('display', 'block');
116                             _this.css('background', 'url(src/assets/img/down_15a4fa_12px.png) no-repeat left center');
117                         } else {
118                             _this.siblings('ul').css('display', 'none');
119                             $(this).css('background', 'url(src/assets/img/right_15a4fa_12px.png) no-repeat left center');
120                         }
121                     }
122                 })
123             })
124         }
125     })
126 }

第四步:效果數組

 

相關文章
相關標籤/搜索