Created
April 28, 2021 14:49
-
-
Save m-yahya/c9b9e8f6be42c825d3eb8f656f36d856 to your computer and use it in GitHub Desktop.
Dynamic Nested UI and Tree View Tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const createList = (items) => { | |
| switch ($.type(items)) { | |
| case "object": | |
| console.log(items); | |
| break; | |
| case "string": | |
| console.log(items); | |
| break; | |
| case "array": | |
| console.log(items); | |
| break; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // evaluate expressions | |
| const createList = (items) => { | |
| switch ($.type(items)) { | |
| case "object": | |
| getItems(items); | |
| break; | |
| case "string": | |
| markupArray.push(`<li> <span> ${items} </span> </li>`); | |
| break; | |
| case "array": | |
| console.log(items); | |
| break; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // evaluate expressions | |
| const createList = (items) => { | |
| // code removed for brevity | |
| case "array": | |
| items.forEach((item) => { | |
| createList(item); | |
| }); | |
| break; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // evaluate expressions | |
| const createList = (items) => { | |
| switch ($.type(items)) { | |
| case "object": | |
| getItems(items); | |
| break; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const data = { | |
| Parent: "Childa", | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const data = { | |
| Parent: ["Child 1", "Child 2", {Child3:'Sub Child'}], | |
| }; | |
| // OR | |
| const data = { | |
| Parent: { | |
| Child1: "Child 1", | |
| Child2: {Child21: "Child 21", Child22: "Child 22"}, | |
| Child3: [ | |
| "Child 31", | |
| "Child 32", | |
| { Child33: "Sub Child" }, | |
| { Child4: ["Child 41", "Child 42", { Child43: "Sub Child" }] }, | |
| ], | |
| }, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // add a parent with some details | |
| const data = { | |
| Parent: { | |
| name: "Jan Doe", | |
| age: "50", | |
| img: "father.png", | |
| }, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const data = { | |
| Parent: { | |
| img: "father.png", | |
| name: "Jan Doe", | |
| age: "50", | |
| children: [ | |
| { | |
| child: { | |
| img: "child_1.png", | |
| name: "child 1", | |
| age: "25", | |
| }, | |
| }, | |
| ], | |
| }, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get details | |
| const getDetails = (details) => { | |
| // iterate over the detail items of object | |
| for (const detail in details) { | |
| // fetch the value of each item | |
| markupArray.push(`<span> ${details[detail]} </span>`); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get details | |
| const getDetails = (details) => { | |
| // iterate over the detail items of object | |
| for (const detail in details) { | |
| // fetch the value of each item | |
| if (detail == "img") { | |
| markupArray.push( | |
| `<img src="./img/${details[detail]}" alt="${details[detail]}">` | |
| ); | |
| } else { | |
| markupArray.push(`<span> ${details[detail]} </span>`); | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get details | |
| const getDetails = (details) => { | |
| // iterate over the detail items of object | |
| for (const detail in details) { | |
| // fetch the value of each item | |
| if (detail == "img") { | |
| markupArray.push( | |
| `<img src="./img/${details[detail]}" alt="${details[detail]}">` | |
| ); | |
| } else if (detail == "children") { | |
| markupArray.push("<ul>"); | |
| details[detail].forEach((element) => { | |
| getItems(element); | |
| }); | |
| markupArray.push("</ul>"); | |
| } else { | |
| markupArray.push(`<span> ${details[detail]} </span>`); | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get items in the object | |
| const getItems = (items) => { | |
| for (const item in items) { | |
| // push li tags for parent | |
| // with nested opening ul tag for children | |
| markupArray.push(`<li> <a> ${item} </a> <ul>`); | |
| // evaluate expression for children | |
| createList(items[item]); | |
| // push closing tage | |
| markupArray.push("</ul></li>"); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get items in the object | |
| const getItems = (items) => { | |
| for (const item in items) { | |
| markupArray.push(`<li> ${item}`); | |
| markupArray.push("</li>"); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // get items in the object | |
| const getItems = (items) => { | |
| for (const item in items) { | |
| markupArray.push(`<li> ${item}`); | |
| // fetch the parent object | |
| let details = items[item]; | |
| getDetails(details); | |
| // push the closing tag for parent | |
| markupArray.push("</li>"); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ul> | |
| <li>Parent | |
| <ul> | |
| <li> <span> Child </span> </li> | |
| </ul> | |
| </li> | |
| </ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // call the function on page load | |
| window.onload = () => { | |
| createList(data); | |
| markupArray.push("</ul>"); | |
| $("#list").html(markupArray.join("")); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment