Thursday 15 September 2011

javascript - jQuery - Parse DOM and create a tree structure (composite pattern) -


I am parsing the DOM, and I want to remove the hierarchy $ ('[data-node]') , in a JavaScript object tree structure. I have failed to find a standard jQuery method of doing this using JQuery.find () to think back a flat list. For example, I would like to parse it:

  & lt; Div data-node = "root" & gt; & Lt; Div class = "negligible-presentation" & gt; & Lt; Div & gt; & Lt; Div data-node = "interesting item" & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div & gt; & Lt; Div data-node = "another interesting entom" & gt; & Lt; Div data-node = "useful item" & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt;  

and thus create a structure in JavaScript:

  [{"node": "root", "kids": [{"node": "Interesting itam", "children": []}, {"node": "another interesting item", "children": [{"node": "useful item", "children": []}]}] }]  

Not sure why you needed it, but doing something like that

  $ Fn.tree = function () {var arr = [], itself = this; (Function Renafferesteran (L, ARR) {var is AL = self.is (L), children = []; if (IEL) Aryuposh ({"node": Ladeata ('node'), "children": children}) ; L. Baal (). Each (function () {runForrestRun ($ (this), is? Children: arr);}}}} (this.first (), arr)); Return arr; }  


No comments:

Post a Comment