樹的遍歷和篩選

来源:http://www.cnblogs.com/qeknao/archive/2017/12/01/7941967.html
-Advertisement-
Play Games

查詢樹的時候,我們需要篩除沒用的分支, 原則就是當此節點不符合條件且他沒有子節點時,刪除此節點 方法如下 1 function cal(dataC,key) { 2 var num = 0; 3 var data = $.extend(true, {}, dataC); 4 var a = data ...


查詢樹的時候,我們需要篩除沒用的分支,

原則就是當此節點不符合條件且他沒有子節點時,刪除此節點

方法如下

 1 function cal(dataC,key) {
 2                 var num = 0;
 3                 var data = $.extend(true, {}, dataC);
 4                 var a = data.Rows;
 5                 var b = $.extend(true, {}, data);
 6                 var subclause = function(a){
 7                     if (a instanceof Array){
 8                         $.each(a, function (i, e) {
 9                             if (e.children) {
10                                 if (e.children.length !=0) {
11                                     subclause(e.children);
12                                 }else if (e.name.indexOf(key) < 0) {
13                                     filter(b.Rows, e.id);
14                                 }
15                             } else if (e.name.indexOf(key) < 0) {
16                                 filter(b.Rows, e.id);
17                             }
18                         })
19                     }else if(a.name.indexOf(key) < 0){
20                         filter(b.Rows, e.id);
21                     }
22                 }
23                 function filter(arr, id) {
24                     num = 1;
25                     for (var i = 0; i < arr.length; i++) {
26                         var el = arr[i]
27                         if (el.id === id) {
28                             arr.splice(i, 1)
29                         } else {
30                             if (el.children && el.children.length) {
31                                 filter(el.children, id)
32                             }
33                         }
34                     }
35                     return arr;
36                 }
37                 subclause(a);
38                 if(num ==0){
39                     //輸出篩選完的結果
40                     manager.options.data =  b;
41                 }else {
42                     cal(b);
43                 }
44             }
篩選代碼

數據如下

1 {"Rows":[{"id":"1","parentId":"0","name":"天安雲谷設備樹2017","code":"TAYG103102","type":"devCategory","children":[{"id":"001521148699","parentId":"1","name":"1#B座PT櫃123","code":"TAYG201710100060","type":"devCategory","children":[{"id":"000676933039","parentId":"001521148699","name":"編輯後新名稱","code":"TAYG201711070181","type":"devCategory"},{"id":"001807303666","parentId":"001521148699","name":"設備類別2","code":"TAYG201711070182","type":"devCategory"},{"id":"7j1cOVdHJNRtWu7S","parentId":"001521148699","name":"新設備1","code":"SBBM201711070454","type":"device"},{"id":"9D80gsD1ibGIdi7s","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070466","type":"device"},{"id":"ae6MsPVTnE3yQRI8","parentId":"001521148699","name":"新設備1","code":"SBBM201711070458","type":"device"},{"id":"gEYWc5n12zIkZSf2","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070460","type":"device"},{"id":"hwncx1NZlIzd149i","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070467","type":"device"},{"id":"jdaAm1BIfgmkf8pK","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070463","type":"device"},{"id":"NndrcH26kRhFrYpM","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070461","type":"device"},{"id":"P2YVKOd5RUhsFbi3","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070462","type":"device"},{"id":"ZF0Umr9kNyGYbA5g","parentId":"001521148699","name":"設備信息1","code":"SBBM201711070464","type":"device"}]},{"id":"2","parentId":"1","name":"1#B座PT櫃123","code":"YGYQ","type":"devCategory","children":[{"id":"000217285013","parentId":"2","name":"導入1","code":"TAYG04020208","type":"devCategory"},{"id":"002052056212","parentId":"2","name":"北區控制箱","code":"TAYG04020208","type":"devCategory"},{"id":"73","parentId":"2","name":"電梯系統","code":"TAYG02","type":"devCategory","children":[{"id":"74","parentId":"73","name":"客梯","code":"TAYG0201","type":"devCategory","children":[{"id":"80","parentId":"74","name":"3#C座客梯","code":"TAYG020106","type":"devCategory","children":[{"id":"001216893841","parentId":"80","name":"20170908","code":"20170908","type":"devCategory"}]},{"id":"78","parentId":"74","name":"3#A座客梯","code":"TAYG020104","type":"devCategory"},{"id":"76","parentId":"74","name":"1#B座客梯","code":"TAYG020102","type":"devCategory"},{"id":"000168338644","parentId":"74","name":"2#40客梯","code":"TAYG020140","type":"devCategory"},{"id":"001779705439","parentId":"74","name":"產招客梯","code":"TAYG201709280048","type":"devCategory"},{"id":"75","parentId":"74","name":"1#A座客梯","code":"TAYG020101","type":"devCategory"},{"id":"77","parentId":"74","name":"2#客梯","code":"TAYG020103","type":"devCategory"},{"id":"79","parentId":"74","name":"3#B座客梯","code":"TAYG020105","type":"devCategory"},{"id":"81","parentId":"74","name":"3#D座客梯","code":"TAYG020107","type":"devCategory"}]}]},{"id":"3","parentId":"2","name":"強電系統","code":"TAYG01","type":"devCategory","children":[{"id":"4","parentId":"3","name":"高壓","code":"TAYG0101","type":"devCategory","children":[{"id":"15","parentId":"4","name":"母聯櫃","code":"TAYG010104","type":"devCategory","children":[{"id":"16","parentId":"15","name":"2#母聯櫃","code":"TAYG01010401","type":"devCategory"}]},{"id":"12","parentId":"4","name":"PT櫃","code":"TAYG010103","type":"devCategory","children":[{"id":"13","parentId":"12","name":"1#B座PT櫃","code":"TAYG01010301","type":"devCategory"},{"id":"14","parentId":"12","name":"3#A座PT櫃","code":"TAYG01010302","type":"devCategory"}]},{"id":"24","parentId":"4","name":"出線櫃","code":"TAYG010107","type":"devCategory","children":[{"id":"25","parentId":"24","name":"1#B座出線櫃","code":"TAYG01010701","type":"devCategory"},{"id":"27","parentId":"24","name":"3#A座出線櫃","code":"TAYG01010703","type":"devCategory"},{"id":"26","parentId":"24","name":"2#出線櫃","code":"TAYG01010702","type":"devCategory"}]},{"id":"28","parentId":"4","name":"隔離櫃","code":"TAYG010108","type":"devCategory","children":[{"id":"29","parentId":"28","name":"2#隔離櫃","code":"TAYG01010801","type":"devCategory"},{"id":"002013743457","parentId":"28","name":"過濾器","code":"TAYG201709220039","type":"devCategory"}]},{"id":"9","parentId":"4","name":"計量櫃","code":"TAYG010102","type":"devCategory","children":[{"id":"10","parentId":"9","name":"1#B座計量櫃","code":"TAYG01010201","type":"devCategory"},{"id":"11","parentId":"9","name":"3#A座計量櫃","code":"TAYG01010202","type":"devCategory"}]},{"id":"5","parentId":"4","name":"進線櫃","code":"TAYG010101","type":"devCategory","children":[{"id":"6","parentId":"5","name":"1#B座進線櫃","code":"TAYG01010101","type":"devCategory"}]},{"id":"001016964342","parentId":"4","name":"高壓高壓","code":"TAYG201709120010","type":"devCategory"},{"id":"17","parentId":"4","name":"環網櫃","code":"TAYG010105","type":"devCategory","children":[{"id":"19","parentId":"17","name":"3#D座環網櫃","code":"TAYG01010502","type":"devCategory"},{"id":"18","parentId":"17","name":"3#C座環網櫃","code":"TAYG01010501","type":"devCategory"}]},{"id":"20","parentId":"4","name":"直流屏","code":"TAYG010106","type":"devCategory","children":[{"id":"22","parentId":"20","name":"2#直流屏","code":"TAYG01010602","type":"devCategory"},{"id":"21","parentId":"20","name":"1#B座直流屏","code":"TAYG01010601","type":"devCategory"},{"id":"23","parentId":"20","name":"3#A座直流屏","code":"TAYG01010603","type":"devCategory"}]}]},{"id":"30","parentId":"3","name":"低壓","code":"TAYG0102","type":"devCategory","children":[{"id":"55","parentId":"30","name":"低壓聯絡櫃","code":"TAYG010205","type":"devCategory","children":[{"id":"58","parentId":"55","name":"3#C座聯絡櫃","code":"TAYG01020503","type":"devCategory"},{"id":"59","parentId":"55","name":"3#D座聯絡櫃","code":"TAYG01020504","type":"devCategory"},{"id":"56","parentId":"55","name":"1#B座聯絡櫃","code":"TAYG01020501","type":"devCategory"},{"id":"57","parentId":"55","name":"3#A座聯絡櫃","code":"TAYG01020502","type":"devCategory"}]},{"id":"60","parentId":"30","name":"雙電源自動切換","code":"TAYG010206","type":"devCategory","children":[{"id":"63","parentId":"60","name":"3#A座雙電源自動切換","code":"TAYG01020603","type":"devCategory"},{"id":"62","parentId":"60","name":"2#雙電源自動切換","code":"TAYG01020602","type":"devCategory"},{"id":"64","parentId":"60","name":"3#C座雙電源自動切換","code":"TAYG01020604","type":"devCategory"},{"id":"61","parentId":"60","name":"1#B座雙電源自動切換","code":"TAYG01020601","type":"devCategory"}]},{"id":"67","parentId":"30","name":"發電機本體","code":"TAYG010209","type":"devCategory","children":[{"id":"71","parentId":"67","name":"3#D發座電機組","code":"TAYG01020904","type":"devCategory"},{"id":"70","parentId":"67","name":"3#A座發電機組","code":"TAYG01020903","type":"devCategory"},{"id":"68","parentId":"67","name":"1#B座發電機組","code":"TAYG01020901","type":"devCategory"},{"id":"69","parentId":"67","name":"2#發電機組","code":"TAYG01020902","type":"devCategory"}]},{"id":"72","parentId":"30","name":"UPS本體","code":"TAYG010210","type":"devCategory"},{"id":"31","parentId":"30","name":"變壓器","code":"TAYG010201","type":"devCategory","children":[{"id":"33","parentId":"31","name":"2#變壓器","code":"TAYG01020102","type":"devCategory"},{"id":"34","parentId":"31","name":"3#A座變壓器","code":"TAYG01020103","type":"devCategory"},{"id":"35","parentId":"31","name":"3#C座變壓器","code":"TAYG01020104","type":"devCategory"},{"id":"36","parentId":"31","name":"3#D座變壓器","code":"TAYG01020105","type":"devCategory"},{"id":"32","parentId":"31","name":"1#B座變壓器","code":"TAYG01020101","type":"devCategory"}]},{"id":"43","parentId":"30","name":"低壓出線櫃","code":"TAYG010203","type":"devCategory","children":[{"id":"46","parentId":"43","name":"3#A座低壓出線櫃","code":"TAYG01020303","type":"devCategory"},{"id":"44","parentId":"43","name":"1#B座低壓出線櫃","code":"TAYG01020301","type":"devCategory"},{"id":"45","parentId":"43","name":"2#低壓出線櫃","code":"TAYG01020302","type":"devCategory"},{"id":"47","parentId":"43","name":"3#C座低壓出線櫃","code":"TAYG01020304","type":"devCategory"},{"id":"48","parentId":"43","name":"3#D座低壓出線櫃","code":"TAYG01020305","type":"devCategory"}]},{"id":"66","parentId":"30","name":"照明配電櫃","code":"TAYG010208","type":"devCategory"},{"id":"49","parentId":"30","name":"電容補償櫃","code":"TAYG010204","type":"devCategory","children":[{"id":"50","parentId":"49","name":"1#B座電容補償櫃","code":"TAYG01020401","type":"devCategory"},{"id":"52","parentId":"49","name":"3#A座電容補償櫃","code":"TAYG01020403","type":"devCategory"},{"id":"51","parentId":"49","name":"2#電容補償櫃","code":"TAYG01020402","type":"devCategory"},{"id":"53","parentId":"49","name":"3#C座電容補償櫃","code":"TAYG01020404","type":"devCategory"},{"id":"54","parentId":"49","name":"3#D座電容補償櫃","code":"TAYG01020405","type":"devCategory"}]},{"id":"37","parentId":"30","name":"低壓進線櫃","code":"TAYG010202","type":"devCategory","children":[{"id":"38","parentId":"37","name":"1#B座低壓進線櫃","code":"TAYG01020201","type":"devCategory"},{"id":"40","parentId":"37","name":"3#A座低壓進線櫃","code":"TAYG01020203","type":"devCategory"},{"id":"41","parentId":"37","name":"3#C座低壓進線櫃","code":"TAYG01020204","type":"devCategory"},{"id":"42","parentId":"37","name":"3#D座低壓進線櫃","code":"TAYG01020205","type":"devCategory"},{"id":"39","parentId":"37","name":"2#低壓進線櫃","code":"TAYG01020202","type":"devCategory"}]},{"id":"65","parentId":"30","name":"動力配電櫃","code":"TAYG010207","type":"devCategory"}]}]}]},{"id":"001100150274","parentId":"1","name":"1#B座PT櫃123","code":"TAYG10","type":"devCategory"}]}]}
View Code

效果圖

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 很多人對正則表達式的印象都是用來做表單驗證的,這其實是不大準確的。正則表達式目前在很多軟體中都得到了廣泛的應用,包括Linux,Unix等操作系統,VBScript,Java,PHP等開發環境中,以及很多應用軟體都能應用到正則表達式。 ...
  • cu主題是由瘋狂的大叔設計,界面簡潔大方是它最大的特點之一。 手殘君也比較喜愛這款主題,在使用的過程中,根據手殘君的個人習慣,對其進行了優化。 標題居中顯示 增加標題div背景色 標題div平滑優化 相關代碼: tags頁面增加標簽彙總(隨著標簽越來越多,原有的tags頁面滿足不了數據索引的需求) ...
  • 公司突然組織需要重新搭建一個基於 的論壇系統,前端採用 ,上網找了一些腳手架,或多或少不能滿足自己的需求,最終在基於 的`react`腳手架 "generator react webpack" 上搭建改造,這裡作為記錄。 代碼在這裡: "github" 另外推薦地址: "react starter ...
  • 圖片隱寫術就是利用圖片來隱藏某些數據,讓人一眼看去以為是很普通很正常的圖片,但其實裡面隱藏著某些“機密”數據。我們可以用它來保護某些機密圖片或者隱藏特殊的文字信息。 ...
  • Angular2官網通過http請求模擬API 來請求hero 數據,感覺有點繁瑣,很讓人理解不了,我們不採用它的辦法,直接展示怎麼使用http請求來獲取我們的數據 ,直截了當。 第一、準備工作,創建一個WebApi 創建一個webapi(這裡我使用MVC4 WebApi ,你可以選擇其他途徑提供j ...
  • 具體步驟請參考:http://blog.csdn.net/a727911438/article/details/70834467 打包時出現了不少問題,逐一記錄下來以供其他人參考。 package.json文件內容 main.js文件內容 安裝好打包神器後,在執行 electron-packager ...
  • 當已知要載入圖片時方能使用,提升網頁的流暢性。 ...
  • Vue-Access-Control是一套基於Vue/Vue-Router/axios 實現的前端用戶許可權控制解決方案,通過對路由、視圖、請求三個層面的控制,使開發者可以實現任意顆粒度的用戶許可權控制。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...