如何在Antd Design中创建一个选项组?

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

How to create an Option Group in Antd Design?

问题

以下是代码的翻译部分:

我正在尝试使用从我的数据库返回的数据来实现一个带有分类的输入框。

[
  {
    "_id": "63e59f91bd2a21368188ff4b",
    "title": "Uncategorized",
    "slug": "uncategorized",
    "categoryType": "blog",
    "createdAt": "2023-02-10T01:36:17.704Z",
    "updatedAt": "2023-02-10T01:36:17.704Z"
  },
  {
    "_id": "63e5984028745af5bad2c015",
    "parentCategory": {
      "_id": "63e5974a786719dd4bb2d37b",
      "title": "Projects"
    },
    "title": "YTDownloader",
    "slug": "ytdownloader",
    "categoryType": "blog",
    "createdAt": "2023-02-10T01:05:04.919Z",
    "updatedAt": "2023-02-10T01:05:04.919Z"
  },
  {
    "_id": "63e597c3786719dd4bb2d387",
    "parentCategory": {
      "_id": "63e5974a786719dd4bb2d37b",
      "title": "Projects"
    },
    "title": "Song Finder",
    "slug": "song-finder",
    "categoryType": "blog",
    "createdAt": "2023-02-10T01:02:59.742Z",
    "updatedAt": "2023-02-10T01:02:59.742Z"
  }
]

我正在尝试创建示例,就像文档中提供的示例一样,因为我的分类几乎都是“父级”或“子级”,我不想让它们无序。

到目前为止,这是我一直在尝试的,但没有成功:

<Select
  placeholder="选择分类"
  defaultValue={category}
  onChange={(e) => {
    setObjectData({
      ...objectData,
      category: e,
    })
  }}
  value={category}
  options={[
    categories.map((c, i) => [
      {
        label: c.parentCategory ? c.parentCategory.title : c.title,
      },
    ]),
  ]}
/>

这几乎没有返回任何内容,甚至没有错误。我期望的是以下内容:

<Select
  defaultValue={category}
  onChange={(e) => {
    setObjectData({
      ...objectData,
      category: e,
    })
  }}
  value={category}
  options={[
    {
      label: 'Projects',
      options: [
        {
          label: 'YTDownloader',
          value: '63e5984028745af5bad2c015',
        },
        {
          label: 'Song Finder',
          value: '63e597c3786719dd4bb2d387',
        },
      ],
    },
    {
      label: 'Uncategorized',
      value: '63e59f91bd2a21368188ff4b',
    }
  ]}
/>

有人以前是否做过类似的事情?如果您能帮助我解决这个问题,将不胜感激,这个问题已经让我头痛了2个小时,哈哈。

英文:

I'm trying to implement a categories input with this data returned from my DB

[
  {
    _id: &#39;63e59f91bd2a21368188ff4b&#39;,
    title: &#39;Uncategorized&#39;,
    slug: &#39;uncategorized&#39;,
    categoryType: &#39;blog&#39;,
    createdAt: &#39;2023-02-10T01:36:17.704Z&#39;,
    updatedAt: &#39;2023-02-10T01:36:17.704Z&#39;,
  },
  {
    _id: &#39;63e5984028745af5bad2c015&#39;,
    parentCategory: {
      _id: &#39;63e5974a786719dd4bb2d37b&#39;,
      title: &#39;Projects&#39;,
    },
    title: &#39;YTDownloader&#39;,
    slug: &#39;ytdownloader&#39;,
    categoryType: &#39;blog&#39;,
    createdAt: &#39;2023-02-10T01:05:04.919Z&#39;,
    updatedAt: &#39;2023-02-10T01:05:04.919Z&#39;,
  },
  {
    _id: &#39;63e597c3786719dd4bb2d387&#39;,
    parentCategory: {
      _id: &#39;63e5974a786719dd4bb2d37b&#39;,
      title: &#39;Projects&#39;,
    },
    title: &#39;Song Finder&#39;,
    slug: &#39;song-finder&#39;,
    categoryType: &#39;blog&#39;,
    createdAt: &#39;2023-02-10T01:02:59.742Z&#39;,
    updatedAt: &#39;2023-02-10T01:02:59.742Z&#39;,
  },
]

What I'm trying is to create the example given in the documentation since my categories are pretty much 'parents' or 'childrens' and don't want to have them unorganized.

So far this is what I've been trying but to not success:

&lt;Select
  placeholder=&quot;Select category&quot;
  defaultValue={category}
  onChange={(e) =&gt; {
    setObjectData({
      ...objectData,
      category: e,
    })
  }}
  value={category}
  options={[
    categories.map((c, i) =&gt; [
      {
        label: c.parentCategory ? c.parentCategory.title : c.title,
      },
    ]),
  ]}
/&gt;

This returns literally nothing, not even an error. What I was expecting is the following:

  &lt;Select
    defaultValue={category}
    onChange={(e) =&gt; {
      setObjectData({
        ...objectData,
        category: e,
      })
    }}
    value={category}
    options={[
      {
        label: &#39;Projects&#39;,
        options: [
          {
            label: &#39;YTDownloader&#39;,
            value: &#39;63e5984028745af5bad2c015&#39;,
          },
          {
            label: &#39;Song Finder&#39;,
            value: &#39;63e597c3786719dd4bb2d387&#39;,
          },
        ],
      },
      {
        label: &#39;Uncategorized&#39;,
        value: &#39;63e59f91bd2a21368188ff4b&#39;
        ],
      },
    ]}
  /&gt;

Has anyone done something like this before? It will be great if you guys can help me solve this little issue that's been giving a headache for the last 2 hours, LOL

答案1

得分: 0

My guess would be you have only passed objects to options that only have a label and no value. This is speculation, but it's possible nothing is rendered in the dropdown if there is no corresponding value to each label.

The label, which is what the user sees for each option, isn't necessarily its underlying value.

Keep in mind the value is ultimately the thing that will be passed to onChange, so to only have a label with no value could explain why it just does nothing -- because a valid option must have a value.

Map each option such that its value represents that option uniquely:

    categories.map((c, i) => [
      {
        label: c.parentCategory ? c.parentCategory.title : c.title,
        value: c._Id
      },
    ]),
英文:

My guess would be you have only passed objects to options that only have a label and no value. This is speculation, but it's possible nothing is rendered in the dropdown if there is no corresponding value to each label.

The label, which is what the user sees for each option, isn't necessarily its underlying value.

Keep in mind the value is ultimately the thing that will be passed to onChange, so to only have a label with no value could explain why it just does nothing -- because a valid option must have a value.

Map each option such that its value represents that option uniquely:

    categories.map((c, i) =&gt; [
      {
        label: c.parentCategory ? c.parentCategory.title : c.title,
        value: c._Id
      },
    ]),

答案2

得分: 0

我最终做了这个:

&lt;Select
  placeholder=&quot;选择类别&quot;
  defaultValue={category}
  onChange={(e) =&gt; {
    setObjectData({
      ...objectData,
      category: e,
    })
  }}
  value={category}
&gt;
  {categories
    .filter((c) =&gt; c.parentCategory === undefined)
    .map((category, index) =&gt; (
      &lt;Select.OptGroup key={category._id} label={category.title}&gt;
        {categories
          .filter((c) =&gt; c.parentCategory?._id === category._id)
          .map((childC, index) =&gt; (
            &lt;Select.Option key={childC._id} value={childC._id}&gt;
              {childC.title}
            &lt;/Select.Option&gt;
          ))}
      &lt;/Select.OptGroup&gt;
    )}
&lt;/Select&gt;

我差不多得做两次循环,而不是一次,但嘿!这样能完成任务,我想?

英文:

I ended up doing this:

&lt;Select
  placeholder=&quot;Select category&quot;
  defaultValue={category}
  onChange={(e) =&gt; {
    setObjectData({
      ...objectData,
      category: e,
    })
  }}
  value={category}
&gt;
  {categories
    .filter((c) =&gt; c.parentCategory === undefined)
    .map((category, index) =&gt; (
      &lt;Select.OptGroup key={category._id} label={category.title}&gt;
        {categories
          .filter((c) =&gt; c.parentCategory?._id === category._id)
          .map((childC, index) =&gt; (
            &lt;Select.Option key={childC._id} value={childC._id}&gt;
              {childC.title}
            &lt;/Select.Option&gt;
          ))}
      &lt;/Select.OptGroup&gt;
    ))}
&lt;/Select&gt;

I pretty much had to do two loops instead of one but hey!. This gets the job done, I guess?

huangapple
  • 本文由 发表于 2023年2月10日 10:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406405.html
匿名

发表评论

匿名网友

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

确定