英文:
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: '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',
},
]
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:
<Select
placeholder="Select category"
defaultValue={category}
onChange={(e) => {
setObjectData({
...objectData,
category: e,
})
}}
value={category}
options={[
categories.map((c, i) => [
{
label: c.parentCategory ? c.parentCategory.title : c.title,
},
]),
]}
/>
This returns literally nothing, not even an error. What I was expecting is the following:
<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'
],
},
]}
/>
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) => [
{
label: c.parentCategory ? c.parentCategory.title : c.title,
value: c._Id
},
]),
答案2
得分: 0
我最终做了这个:
<Select
placeholder="选择类别"
defaultValue={category}
onChange={(e) => {
setObjectData({
...objectData,
category: e,
})
}}
value={category}
>
{categories
.filter((c) => c.parentCategory === undefined)
.map((category, index) => (
<Select.OptGroup key={category._id} label={category.title}>
{categories
.filter((c) => c.parentCategory?._id === category._id)
.map((childC, index) => (
<Select.Option key={childC._id} value={childC._id}>
{childC.title}
</Select.Option>
))}
</Select.OptGroup>
)}
</Select>
我差不多得做两次循环,而不是一次,但嘿!这样能完成任务,我想?
英文:
I ended up doing this:
<Select
placeholder="Select category"
defaultValue={category}
onChange={(e) => {
setObjectData({
...objectData,
category: e,
})
}}
value={category}
>
{categories
.filter((c) => c.parentCategory === undefined)
.map((category, index) => (
<Select.OptGroup key={category._id} label={category.title}>
{categories
.filter((c) => c.parentCategory?._id === category._id)
.map((childC, index) => (
<Select.Option key={childC._id} value={childC._id}>
{childC.title}
</Select.Option>
))}
</Select.OptGroup>
))}
</Select>
I pretty much had to do two loops instead of one but hey!. This gets the job done, I guess?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论