ReactJs在optgroup中显示数据

时间:2019-11-01 01:58:58

标签: javascript reactjs ant-design-pro

this.props.datacat.map(singleData=>{
  return Object.keys(singleData).map((key,i) =>{
     if(key==='name'){
        return <OptGroup label  = {singleData[key]}>
     }else{
        return <Option key= {key} >{singleData[key]}</Option> 
     }
        return </OptGroup>
  })
})

(this.props.datacat)的数据

[
  {
    "4967": "Others.",
    "4968": "Sports & Beachwear > Others.",
    "4969": "Lingerie & Nightwear > Others.",
    "4971": "Pants & Shorts > Others.",
    "name": "Women Clothes"
  },
  {
    "4798": "Supplements > Others.",
    "4802": "Men's Grooming > Others.",
    "4952": "Others.",
    "4953": "Medical Supplies > Others.",
    "4954": "Personal Pleasure > Others.",
    "4955": "Personal Care > Others.",
    "4956": "Pedicure & Manicure > Others.",
    "6647": "Lips > Lip Tint"
    "name": "Health & Beauty"
  }
]

在上面的代码中,如果键是 name ,则试图显示数据,然后创建 optgroup 标签,否则它将显示选项值中的所有数据。之后,选项值ID <Option key= {key} >{singleData[key]}</Option> ,应该在选项值内,并且要显示的文本是该值。任何人都知道该怎么做:(?

1 个答案:

答案 0 :(得分:1)

this.props.datacat的每个条目创建一个OptGroup,然后用该条目的所有选项填充它,但键为name的选项除外:

this.props.datacat.map((singleData) => (
  <OptGroup label={singleData["name"]}>
    {Object.keys(singleData)
      .filter(key => key !== "name")
      .map(key => (
        <Option key={key}>{singleData[key]}</Option>
      ))}
  </OptGroup>
));

Demo