组织树结构在React中

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

Organizational Tree Structure in react

问题

我是新来的React开发者,对这个愚蠢的问题请原谅。

我想创建一个组织层次结构,如附件所示。

我从JSON文件中获取所有必要的信息。

我的JSX文件如下:

请帮助我解决以下两个问题:

1)如何在JSON文件中提供图像地址。
2)我已经成功创建了根/父卡(即Chetan Lal),如何创建它的子元素?我尝试了以下代码,但无法创建子元素。

import React, { useState } from 'react';
import Records from '../OrgDetail.json';

export default function Chart() {
  // ...(这里是一些样式定义)

  const myFunction = (y) => {
    // ...(这里是一些JavaScript函数)
  }

  return (
    <>
      {Records && Records.map(record => {
        return (
          <div key={record.key} className="container" style={center}>
            <img src={record.image} style={imagestyle} alt={record.name} />
            <a style={linkstyle} href="/" alt=""><b>...</b></a>
            <span style={namestyle}><b>{record.name}</b></span><br></br>
            <p style={designstyle}>{record.designation}</p>
            <button style={buttonstyle}>{record.child.length} &darr;</button>
          </div>
        )
      })}

      {Records.child && Records.child.map(record => {
        return (
          <div className="display:flex" key={record.child.key} style={center}>
            <img src={record.child.image} style={imagestyle} alt={record.name} />
            <a style={linkstyle} href="/" alt=""><b>...</b></a>
            <span style={namestyle}><b>{record.child.name}</b></span><br></br>
            <p style={designstyle}>{record.child.designation}</p>
            <button style={buttonstyle}>{record.child.child.length} &darr;</button>
          </div>
        )
      })}
    </>
  )
}

JSON文件:

[{
  "key": "p1",
  "name": "Chetan Lal",
  "designation": "Managing Director",
  "image": "D:/React/RL_org/my-app/src/Images/Chetan-Lal-edit.jpg",
  "about": "This is just test",
  "child": [
    // ...(更多的JSON数据)
  ]
}]

希望这能帮助你解决你的问题。如果有其他问题,请随时提出。

英文:

I am new to react. so pardon me for this silly question.
I want to create an organizational hierarchy as shown in the attachment.
组织树结构在React中

I am pulling all the necessary information from the JSon file.

My jsx file looks like this:

Please help me with two:

  1. how to give image address in the json file.

  2. i have successfully created root/parent card(i.e chetan lal), how can i create it's child elements? I tried with following code but unable to create child elements.

    import React, { useState } from &#39;react&#39;;
    import Records from &#39;../OrgDetail.json&#39;;
    export default function Chart() {
    const center = {
    top: &#39;60px&#39;,
    backgroundColor: &#39;rgb(246,246,246)&#39;,
    fontSize: &#39;12px&#39;,
    width: &quot;250px&quot;,
    height: &quot;85px&quot;,
    border: &quot;1px solid grey&quot;,
    borderRadius: &#39;25px&#39;,
    margin: &#39;auto&#39;,
    display: &#39;flex&#39;,
    justifyContent: &#39;center&#39;,
    position: &#39;relative&#39;
    }
    const linkstyle = {
    left: &#39;220px&#39;,
    position: &#39;absolute&#39;,
    textDecoration: &#39;none&#39;,
    color: &#39;darkgrey&#39;,
    color: &#39;darkgrey&#39;
    }
    const imagestyle = {
    border: &#39;2px solid grey&#39;,
    borderRadius: &#39;50%&#39;,
    height: &#39;80px&#39;,
    width: &#39;80px&#39;,
    position: &#39;absolute&#39;,
    top: &#39;-50px&#39;,
    }
    const namestyle = {
    position: &#39;absolute&#39;,
    top: &#39;30px&#39;
    }
    const designstyle = {
    position: &#39;absolute&#39;,
    top: &#39;45px&#39;
    }
    const buttonstyle = {
    position: &#39;absolute&#39;,
    top: &#39;70px&#39;,
    borderColor: &#39;grey&#39;
    }
    const myFunction = (y) =&gt; {
    console.log(y);
    var x = document.getElementById(y);
    console.log(x);
    if (x.style.display === &quot;none&quot;) {
    x.style.display = &quot;flex&quot;;
    } else {
    x.style.display = &quot;none&quot;;
    }
    }
    return (
    &lt;&gt;
    {Records &amp;&amp; Records.map(record =&gt; {
    return (
    &lt;div key={record.key} className=&quot;container&quot; style={center}&gt;
    &lt;img src={record.image} style={imagestyle} alt={record.name} /&gt;
    &lt;a style={linkstyle} href=&quot;/&quot; alt=&quot;&quot;&gt;&lt;b&gt;...&lt;/b&gt;&lt;/a&gt;
    &lt;span style={namestyle}&gt;&lt;b&gt;{record.name}&lt;/b&gt;&lt;/span&gt;&lt;br&gt;&lt;/br&gt;
    &lt;p style={designstyle}&gt;{record.designation}&lt;/p&gt;
    &lt;button style={buttonstyle} &gt;{record.child.length} &amp;darr;&lt;/button&gt;
    &lt;/div&gt;
    )
    })}
    {Records.child &amp;&amp; Records.child.map(record =&gt; {
    return (
    &lt;div className=&quot;display:flex&quot; key={record.child.key} style={center}&gt;
    &lt;img src={record.child.image} style={imagestyle} alt={record.name} /&gt;
    &lt;a style={linkstyle} href=&quot;/&quot; alt=&quot;&quot;&gt;&lt;b&gt;...&lt;/b&gt;&lt;/a&gt;
    &lt;span style={namestyle}&gt;&lt;b&gt;{record.child.name}&lt;/b&gt;&lt;/span&gt;&lt;br&gt;&lt;/br&gt;
    &lt;p style={designstyle}&gt;{record.child.designation}&lt;/p&gt;
    &lt;button style={buttonstyle} &gt;{record.child.child.length} &amp;darr;&lt;/button&gt;
    )
    &lt;/div&gt;
    )
    })}
    &lt;/&gt;
    )
    }
    

JSon file:

[{
&quot;key&quot;: &quot;p1&quot;,
&quot;name&quot;: &quot;Chetan Lal&quot;,
&quot;designation&quot;: &quot;Managing Director&quot;,
&quot;image&quot;: &quot;D:/React/RL_org/my-app/src/Images/Chetan-Lal-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test&quot;,
&quot;child&quot;: [
{
&quot;key&quot;: &quot;c1&quot;,
&quot;name&quot;: &quot;Shagufta Merchant&quot;,
&quot;designation&quot;: &quot;VP-Digital Operations &amp; HR&quot;,
&quot;image&quot;: &quot;../Images/Shagufta-Merchant-edit - Copy.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;,
&quot;child&quot;: [
{
&quot;key&quot;: &quot;gc1&quot;,
&quot;name&quot;: &quot;Manjiri Nadkarni&quot;,
&quot;designation&quot;: &quot;Manager - NA Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;,
&quot;child&quot;: [
{
&quot;key&quot;: &quot;ggc1&quot;,
&quot;name&quot;: &quot;Anurag Bhagrav&quot;,
&quot;designation&quot;: &quot;Team Leader - Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
},
{
&quot;key&quot;: &quot;ggc2&quot;,
&quot;name&quot;: &quot;Janhavi Chaudhari&quot;,
&quot;designation&quot;: &quot;Team Leader - Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
},
{
&quot;key&quot;: &quot;ggc3&quot;,
&quot;name&quot;: &quot;Anurag Shetty&quot;,
&quot;designation&quot;: &quot;Team Leader - Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
}
]
},
{
&quot;key&quot;: &quot;gc2&quot;,
&quot;name&quot;: &quot;Shailendra More&quot;,
&quot;designation&quot;: &quot;Manager - NA Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;,
&quot;child&quot;: [
{
&quot;key&quot;: &quot;ggc1&quot;,
&quot;name&quot;: &quot;AjayKumar Padhy&quot;,
&quot;designation&quot;: &quot;Team Leader - SEO &amp; Auto&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
},
{
&quot;key&quot;: &quot;ggc2&quot;,
&quot;name&quot;: &quot;Jignesh Shah&quot;,
&quot;designation&quot;: &quot;Asist. Manager&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
}
]
},
{
&quot;key&quot;: &quot;gc3&quot;,
&quot;name&quot;: &quot;Vaishakhi Phadke&quot;,
&quot;designation&quot;: &quot;Asst. Manager - NA Media Ops&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;,
&quot;child&quot;: [
{
&quot;key&quot;: &quot;ggc1&quot;,
&quot;name&quot;: &quot;Hassel Rathod&quot;,
&quot;designation&quot;: &quot;Team Manger - National&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
}
]
},
{
&quot;key&quot;: &quot;gc4&quot;,
&quot;name&quot;: &quot;Mitesh More&quot;,
&quot;designation&quot;: &quot;Training Team Manager&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
},
{
&quot;key&quot;: &quot;gc5&quot;,
&quot;name&quot;: &quot;Shrutika Jadhav&quot;,
&quot;designation&quot;: &quot;Manager - HR&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
},
{
&quot;key&quot;: &quot;gc6&quot;,
&quot;name&quot;: &quot;Tejinder Ossan&quot;,
&quot;designation&quot;: &quot;Manager - Facility&quot;,
&quot;image&quot;: &quot;placeholder.jpg&quot;,
&quot;about&quot;: &quot;This is just test2&quot;
}
]
},
{
&quot;key&quot;: &quot;c2&quot;,
&quot;name&quot;: &quot;Bhavesh Chauhan&quot;,
&quot;designation&quot;: &quot;VP-Marketing Operations &amp; IT&quot;,
&quot;image&quot;: &quot;../Images/Bhavesh-Chauhan-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test3&quot;
},
{
&quot;key&quot;: &quot;c3&quot;,
&quot;name&quot;: &quot;Sanket Naik&quot;,
&quot;designation&quot;: &quot;VP-Marketing Operations &amp; Graphics&quot;,
&quot;image&quot;: &quot;../Images/Sanket-Naik-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test4&quot;
},
{
&quot;key&quot;: &quot;c4&quot;,
&quot;name&quot;: &quot;Ankanksha Narula&quot;,
&quot;designation&quot;: &quot;Sr. Manager - Finance&quot;,
&quot;image&quot;: &quot;../Images/Akansha-Narula-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test5&quot;
},
{
&quot;key&quot;: &quot;c5&quot;,
&quot;name&quot;: &quot;Diju Kurup&quot;,
&quot;designation&quot;: &quot;Manager - Marketing Operations&quot;,
&quot;image&quot;: &quot;../Images/Diju-Kurup-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test6&quot;
},
{
&quot;key&quot;: &quot;c6&quot;,
&quot;name&quot;: &quot;Arya Andrade&quot;,
&quot;designation&quot;: &quot;Manager - Facility&quot;,
&quot;image&quot;: &quot;../Images/Arya-Andrade-edit.jpg&quot;,
&quot;about&quot;: &quot;This is just test7&quot;
}
]
}]

答案1

得分: 1

  1. 将图像放置在您的public/static目录中,并根据数据设置路径:
"image": "/images/placekitten.jpg",
  1. 要呈现数据的层次结构,您可能希望使用一个递归组件,该组件呈现当前元素,然后为每个子元素再次呈现自身。这会产生一个递归结构,适用于数据的任何深度。

可能看起来像这样:

export function Node({ item }) {
  return (
    {/* 渲染当前项目的名称等信息 */}
    <div className="node">
      <div class="card">
        <img src={item.image} />
        <div className="name">{item.name}</div>
        <div className="designation">{item.designation}</div>
      </div>

      {item.child?.length && (
        <div className="children">
          {item.child.map((item) => (
            {/* 使用相同的组件呈现子项目 */}
            <Node key={item.key} item={item} />
          ))}
        </div>
      )}
    </div>
  );
}

这是一个使用上述组件和您的数据的CodeSandbox上的实时演示实现,生成下面的屏幕截图中的结果。

(当然,您可以根据需要进行修改,以仅显示当前选择或其他内容。这仅供说明目的的示例方法。)

组织树结构在React中

英文:
  1. Place the images in your public/static directory and set the paths in the data accordingly:
&quot;image&quot;: &quot;/images/placekitten.jpg&quot;,
  1. To render the hierarchy from data you may want to use a recursive component that renders the current element and then renders itself for each child. This produces a recursive structure to whatever depth your data is.

Might looks something like this:

export function Node({ item }) {
  return (
    {/* render the current item&#39;s name, etc. */}
    &lt;div className=&quot;node&quot;&gt;
      &lt;div class=&quot;card&quot;&gt;
        &lt;img src={item.image} /&gt;
        &lt;div className=&quot;name&quot;&gt;{item.name}&lt;/div&gt;
        &lt;div className=&quot;designation&quot;&gt;{item.designation}&lt;/div&gt;
      &lt;/div&gt;

      {item.child?.length &amp;&amp; (
        &lt;div className=&quot;children&quot;&gt;
          {item.child.map((item) =&gt; (
            {/* render the children using this same component */}
            &lt;Node key={item.key} item={item} /&gt;
          ))}
        &lt;/div&gt;
      )}
    &lt;/div&gt;
  );
}

Here's a live demo implementation on codesandbox using the component above with your data, producing the result in the screenshot below.

(You could of course modify it to only show the current selection or whatever. This is intended only as an example of the approach for illustrative purposes.)

组织树结构在React中

huangapple
  • 本文由 发表于 2023年3月4日 00:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629569.html
匿名

发表评论

匿名网友

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

确定