首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应: JSON树与递归的帮助

反应: JSON树与递归的帮助
EN

Stack Overflow用户
提问于 2017-08-21 06:25:57
回答 1查看 2.2K关注 0票数 1

JSON后端程序为我提供了它的多个父子,所以我必须放置动态循环来显示父子。

JSON

代码语言:javascript
复制
"data": [
  {
    "id": 25,
    "slug": "mobiles",
    "parent_id": null,
    "name": "Mobiles"
  },
  {
    "id": 26,
    "slug": "mobile-phones-accessories",
    "parent_id": 25,
    "name": "Mobile Phones accessories"
  },
  {
    "id": 27,
    "slug": "computer-laptop",
    "parent_id": null,
    "name": "Computer & Laptop"
  },
  {
    "id": 28,
    "slug": "laptops",
    "parent_id": 27,
    "name": "Laptops"
  },
  {
    "id": 29,
    "slug": "mobile-phones",
    "parent_id": 26,
    "name": "Mobiles Phone"
  }
]

我的函数(请忽略这一点。这只是一次尝试,但我有一个孩子的父母)

代码语言:javascript
复制
renderCategoriesHtml() {
  const { categories } = this.props;

  if (!categories) return false;

  const nullCat = [];
  categories.map((obj) => {
    if (obj.parent_id == null) {
      nullCat.push(obj);
    }
  });

  return nullCat.map(
    (parentCat, i) => (
      <div className="form-group" key={i}>
        <div className="checkbox" key={i}>
          <label>
            <Field
              name={`categories.${parentCat.id}`}
              component="input"
              type="checkbox"
            />
            {parentCat.slug}
          </label>
        </div>
        {
          categories.map(
            (childCat, j) => (
              parentCat.id == childCat.parent_id ?
                <div className="checkbox ml-20" key={j}>
                  <label>
                    <Field
                      name={`categories.${childCat.id}`}
                      component="input"
                      type="checkbox"
                    />
                    {childCat.slug}
                  </label>
                </div>
                : ''
            )
          )
        }
      </div>
    )
  );
}

我想要这个(我想要的动态html )

代码语言:javascript
复制
<ul>
  <li>mobiles</li>
  <ul>
      <li>mobile-phones-accessories</li>
      <ul>
          <li>mobile-phones</li>
      </ul>
  </ul>            
  <li>computer-laptop</li>
  <ul>
      <li>laptops</li>
  </ul>    
</ul>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-21 07:22:57

试试这个:

代码语言:javascript
复制
class TreeRender extends React.Component {
  state = {
    data: JSON.parse('[{"id": 25,"slug": "mobiles","parent_id": null,"name": "Mobiles"},{"id": 26,"slug": "mobile-phones-accessories","parent_id": 25,"name": "Mobile Phones accessories"},{"id": 27,"slug": "computer-laptop","parent_id": null,"name": "Computer & Laptop"},{"id": 28,"slug": "laptops","parent_id": 27,"name": "Laptops"},{"id": 29,"slug": "mobile-phones","parent_id": 26,"name": "Mobiles Phone"}]')
  }
  getCurrent = (node) => this.state.data.filter(cNode => cNode.parent_id == node).map(cNode => (
    <ul key={`node_${cNode.id}`}>
      <li>{cNode.name}</li>
      {this.getCurrent(cNode.id)}
    </ul>
  ))

  render() {
    return (
      <div>
        {this.getCurrent(null)}
      </div>
    );
  }
}

小提琴

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45790499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档