如何使用JavaScript展示我的具有父子关系的数据?

huangapple go评论112阅读模式
英文:

How can I show my data with a parent child relationship with javascript?

问题

Sure, here's the translated code part:

  1. const data = [
  2. { id: 1, text: 'hello', parent: null },
  3. { id: 2, text: 'hello2', parent: 1 },
  4. { id: 3, text: 'hello3', parent: 2 },
  5. { id: 4, text: 'where are u?', parent: null },
  6. ];
  7. function convertToTree(data: any) {
  8. const tree: any = {};
  9. data.forEach((item: any) => {
  10. const parentId = item.parent;
  11. if (!parentId) {
  12. tree[item.id] = { ...item, children: [] };
  13. } else {
  14. if (!tree[parentId]) {
  15. tree[parentId] = { children: [] };
  16. }
  17. tree[parentId].children.push({ ...item, children: [] });
  18. }
  19. });
  20. return tree;
  21. }
  22. function TreeNode({ data }: any) {
  23. const [isVisible, setIsVisible] = useState(false);
  24. const handleClick = () => {
  25. setIsVisible(!isVisible);
  26. };
  27. return (
  28. <div>
  29. <p onClick={handleClick}>{data.text}</p>
  30. {isVisible && data.children && data.children.length > 0 && (
  31. <ul>
  32. {data.children.map((child: any) => (
  33. <li key={child.id}>
  34. <TreeNode data={child} />
  35. </li>
  36. ))}
  37. </ul>
  38. )}
  39. </div>
  40. );
  41. }

I hope this helps you with your JavaScript-React code! If you have any questions or need further assistance, please feel free to ask.

英文:
  1. const data = [
  2. { id: 1, text: &#39;hello&#39;, parent: null },
  3. { id: 2, text: &#39;hello2&#39;, parent: 1 },
  4. { id: 3, text: &#39;hello3&#39;, parent: 2 },
  5. { id: 4, text: &#39;where are u?&#39;, parent: null },
  6. ];
  1. function convertToTree(data: any) {
  2. const tree: any = {};
  3. data.forEach((item: any) =&gt; {
  4. const parentId = item.parent;
  5. if (!parentId) {
  6. tree[item.id] = { ...item, children: [] };
  7. } else {
  8. if (!tree[parentId]) {
  9. tree[parentId] = { children: [] };
  10. }
  11. tree[parentId].children.push({ ...item, children: [] });
  12. }
  13. });
  14. return tree;
  15. }
  16. function TreeNode({ data }: any) {
  17. const [isVisible, setIsVisible] = useState(false);
  18. const handleClick = () =&gt; {
  19. setIsVisible(!isVisible);
  20. };
  21. return (
  22. &lt;div&gt;
  23. &lt;p onClick={handleClick}&gt;{data.text}&lt;/p&gt;
  24. {isVisible &amp;&amp; data.children &amp;&amp; data.children.length &gt; 0 &amp;&amp; (
  25. &lt;ul&gt;
  26. {data.children.map((child: any) =&gt; (
  27. &lt;li key={child.id}&gt;
  28. &lt;TreeNode data={child} /&gt;
  29. &lt;/li&gt;
  30. ))}
  31. &lt;/ul&gt;
  32. )}
  33. &lt;/div&gt;
  34. );
  35. }

I want to show this data as perent-child depending on the parent data in the data using javascript-react.
rule1- when I click on the parent data I want to see the child data.
rule2- since every child can also be a parent, if a child has child data, I want to access its own child data when I click on the child.

But in the code I wrote, only when I click on the parent data, only the children come. As it appears in the data, the child data also has childi, but I cannot see them.

thank you for your help

答案1

得分: 1

这是我现在有的,基本上是将节点递归添加到树中然后打印出来的方法:

  1. class TodoApp extends React.Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. items: [
  6. { id: 1, text: 'hello', parent: null },
  7. { id: 2, text: 'hello2', parent: 1 },
  8. { id: 3, text: 'hello3', parent: 2 },
  9. { id: 4, text: 'where are u?', parent: null },
  10. ]
  11. }
  12. }
  13. transformToTree(data) {
  14. const tree = {};
  15. // 具有父节点但不在树中的元素
  16. const invisibleParents = {};
  17. for(let node of data.items) {
  18. if (!node.parent) {
  19. // 树的第一个节点
  20. tree[node.id] = { ...node, children: [] };
  21. if (node.id in invisibleParents) {
  22. tree[node.id].children.push(...invisibleParents[node.id]);
  23. delete invisibleParents[node.id];
  24. }
  25. continue;
  26. }
  27. if (node.parent in tree) {
  28. // 直接添加
  29. tree[node.parent].children.push(node);
  30. } else {
  31. if (node.parent in invisibleParents)
  32. invisibleParents[node.parent].push(node);
  33. else
  34. invisibleParents[node.parent] = [node];
  35. }
  36. }
  37. return tree;
  38. }
  39. render() {
  40. const treeData = this.transformToTree(this.state);
  41. return (
  42. treeData.map(x =>
  43. {
  44. return (<SmallComponent item={x} />)
  45. })
  46. )
  47. }
  48. }
  49. class SmallComponent extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. this.state = { ...props };
  53. }
  54. render() {
  55. const item = this.state.item;
  56. return (
  57. <div>
  58. <p>
  59. {item.text}
  60. {item.children.length &&
  61. <ul>
  62. <li>
  63. {item.children.map(x =>
  64. (
  65. <SmallComponent item={x} />
  66. ))}
  67. </li>
  68. </ul>}
  69. </p>
  70. </div>
  71. )
  72. }
  73. }
  74. ReactDOM.render(<TodoApp />, document.querySelector("#app"))
英文:

This is what I have, basically your method of recursively adding nodes to the trees and then printing it

  1. class TodoApp extends React.Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. items: [
  6. { id: 1, text: &#39;hello&#39;, parent: null },
  7. { id: 2, text: &#39;hello2&#39;, parent: 1 },
  8. { id: 3, text: &#39;hello3&#39;, parent: 2 },
  9. { id: 4, text: &#39;where are u?&#39;, parent: null },
  10. ]
  11. }
  12. }
  13. transformToTree(data) {
  14. const tree = {};
  15. //elems that have a parent but not in tree
  16. const invisibleParents = {};
  17. for(let node in data.items)
  18. {
  19. if (!node.parent)
  20. {
  21. //first node of tree
  22. tree[node.id] = {...node, children: []};
  23. if (node.id in invisibleParents)
  24. {
  25. tree[node.id].children.push(...invisibleParents[node.id]);
  26. delete invisibleParents[node.id];
  27. }
  28. continue;
  29. }
  30. if (node.parent in tree)
  31. {
  32. //directly add
  33. tree[node.parent].children.push(node);
  34. }
  35. else
  36. {
  37. if (node.parent in invisibleParents)
  38. invisibleParents[node.parent].push(node);
  39. else
  40. invisibleParents[node.parent] = [node];
  41. }
  42. }
  43. return tree;
  44. }
  45. render() {
  46. const treeData = transformData(this.state.data);
  47. return (
  48. treeData.map(x =&gt;
  49. {
  50. return (&lt;SmallComponent item={x} /&gt;)
  51. })
  52. )
  53. }
  54. }
  55. class SmallComponent extends React.Component
  56. {
  57. constructor(props)
  58. {
  59. super(props);
  60. this.state = {...props};
  61. }
  62. render() {
  63. item = this.state.item;
  64. return (
  65. &lt;div&gt;
  66. &lt;p&gt;
  67. {item.text}
  68. {item.children.length &amp;&amp;
  69. &lt;ul&gt;
  70. &lt;li&gt;
  71. {item.children.map(x =&gt;
  72. (
  73. &lt;SmallComponent item={x} /&gt;
  74. ))}
  75. &lt;/li&gt;
  76. &lt;/ul&gt;}
  77. &lt;/p&gt;
  78. &lt;/div&gt;
  79. )
  80. }
  81. }
  82. ReactDOM.render(&lt;TodoApp /&gt;, document.querySelector(&quot;#app&quot;))

huangapple
  • 本文由 发表于 2023年7月4日 20:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定