A simple method to use address reference for fast tree Arrangement
Xiaoqiang will inevitably deal with the tree. When talking about the tree, Xiaoqiang will think of recursion. it is undeniable that I have written recursion for many years. however, recursion requires a lot of loops. here, address reference is used for fast tree arrangement, and only one loop is required. 1. Find my father from my son. The test data is constructed by me:; father; son, father; son, Father ;..... copy the Code 1 var str = ";"; 2 var datas = str. split (new char [] {';'}) 3. select (s => {4 var tmp = s. split (new char [] {','}); 5 return new {6 ID = tmp [0]. toInt (), 7 PID = tmp [1]. toInt () 8 }; 9}); 10 11 var dic = new Dictionary <decimal, Tmp> (); 12 foreach (var kv in datas) {13 var tmp = new Tmp () {14 ID = kv. ID, 15 PID = kv. PID16}; 17 18 dic. set (kv. ID, tmp); 19 20 var parent = dic. get (kv. PID, new Tmp () {21 ID = kv. PID22}); 23 24 tmp. parent = parent; 25} copy code 1 private class Tmp {2 3 public int ID {4 get; 5 set; 6} 7 8 public int PID {9 get; 10 set; 11} 12 public Tmp Parent {13 get; 14 Set; 15} 16} copy the Code. In addition to constructing test data, only one loop is used and no Nesting is used. toInt and dic. the Set Method is a custom extension method. It only serves as a helper and does not matter with the Row Tree. find a son from the root, there is no existing CS example, and you are too lazy to write it. paste the code for js treegrid: copy the Code 1 var dealTree = function (tab) {2 var rows = tab. rows; 3 var row; 4 5 var treeObjs ={}; 6 var tmp ={}; 7 8 for (var I = 0; row = rows [I]; I ++) {9 var id = $ (row ). attr ("data-treegrid-id"); 10 var pId = $ (row ). attr ("data-treegrid-parentID"); 11 12 if (id = undefined | PId = undefined) 13 continue; 14 15 if (! Tmp [pId]) 16 tmp [pId] ={}; 17 18 if (! Tmp [id]) 19 tmp [id] = {hasChild: false}; 20 21 tmp [id]. pID = pId; 22 23 tmp [pId] [id] = tmp [id]; 24 tmp [pId]. hasChild = true; 25} 26 27 for (var t in tmp) {28 if (tmp [t]. pID = undefined) {29 treeObjs [t] = tmp [t]; 30} 31} 32 33 return treeObjs; 34} duplicate code works the same way. this is mainly because the treegrid applied directly to the table cannot be found from the Internet, or the tree table is constructed from json or the row order must be sorted first, it hurts a lot. I need a table function that can automatically arrange trees directly on the existing table. I haven't been satisfied with it for a long time. I can only do it myself: Copy code 1 var Tre Egrid = {}; 2 (function (t) {3 4 var self = this; 5 6 var dealTree = function (tab) {7 var rows = tab. rows; 8 var row; 9 10 var treeObjs ={}; 11 var tmp ={}; 12 13 for (var I = 0; row = rows [I]; I ++) {14 var id = $ (row ). attr ("data-treegrid-id"); 15 var pId = $ (row ). attr ("data-treegrid-parentID"); 16 17 if (id = undefined | pId = undefined) 18 continue; 19 20 if (! Tmp [pId]) 21 tmp [pId] ={}; 22 23 if (! Tmp [id]) 24 tmp [id] = {hasChild: false}; 25 26 tmp [id]. pID = pId; 27 28 tmp [pId] [id] = tmp [id]; 29 tmp [pId]. hasChild = true; 30} 31 32 for (var t in tmp) {33 if (tmp [t]. pID = undefined) {34 treeObjs [t] = tmp [t]; 35} 36} 37 38 return treeObjs; 39} 40 41 var getSubIds = function (treeObjs, arr) {42 for (var t in treeObjs) {43 if (typeof (treeObjs [t])! = "Object") 44 continue; 45 46 arr. push (t); 47 48 if (treeObjs [t]. hasChild) 49 getSubIds (treeObjs [t], arr); 50} 51 return arr; 52} 53 54 var generate = function (treeObjs, tab, tbody, level) {55 for (var t in treeObjs) {56 var node = treeObjs [t]; 57 58 if (typeof (node )! = "Object") 59 return; 60 61 var row = $ (tab ). find ("tr [data-treegrid-id =" + t + "]"); 62 var td = row. find ("td: eq (0)"); 63 var indents = new Array (level ). join ("<span class = 'treegridindent '> </span>"); 64 65 var htmls = [indents]; 66 67 if (node. hasChild) {68 var subIds = getSubIds (node, new Array (); 69 htmls. push ("<span data-treegrid-subs = '" + subIds. join (",") + "'class = 'glyphicon glyphicon-chevr On-down '> </span> ") 70} 71 htmls.push(td.html (); 72 73 td.html (htmls. join (""); 74 tbody. append (row); 75 generate (node, tab, tbody, level + 1); 76} 77} 78 79 var init = function (tab) {80 if ($ (tab ). attr ("data-treegrid-dealed") = true) 81 return; 82 83 var treeObjs = dealTree (tab ); 84 85 var tbody = $ ("<tbody>"); 86 87 generate (treeObjs, tab, tbody, 0); 88 $ (tab ). append (tbody); 89 $ (tab ). attr ("Data-treegrid-dealed", true); 90} 91 92 $. fn. treegrid = function () {93 this. each (function () {94 init (this); 95}); 96 97 $ ("span [data-treegrid-subs]"). on ("click", function () {98 var subIds = $ (this ). attr ("data-treegrid-subs "). split (','); 99 var explanded = $ (this ). data ("data-treegrid-expandend"); 100 explanded = undefined? True: explanded; 101 var id; 102 for (var I = 0; id = subIds [I]; I ++) {103 var tr = $ ("tr [data-treegrid-id = '" + id + "']"); 104 tr. toggle (! Explanded); 105} 106 $ (this) 107. data ("data-treegrid-expandend ",! Explanded) 108. removeClass ("glyphicon-chevron-right"). removeClass ("glyphicon-chevron-down") 109. addClass (explanded? "Glyphicon-chevron-right": "glyphicon-chevron-down"); 110}); 111} 112 113}) (Treegrid );