javascript - 如何在 Cascader Ant Design ReactJs 中显示子项?
问题描述
我在如何在 console.log 下方显示 categorySubResponses 和 categorySubChildResponses 的数据时遇到问题
像这种级联的梦取自虚拟数据
现在没有孩子
我用蚂蚁设计https://ant.design/components/cascader/
我把代码codesanbox.io
和链接
https://codesandbox.io/embed/q-56619059-so-8dk0z
解决方案
您需要为每个数组创建值和标签.map()
categorySubResponses
。categorySubResponses
const options = this.state.allCategory.map(category => ({
value: category.id,
label: category.name,
children: category.categorySubResponses && category.categorySubResponses.map(child => ({
value: child.id,
label: child.name,
children: child.categorySubChildResponses && child.categorySubChildResponses.map(innerChild => ({
value: innerChild.id,
label: innerChild.name
})) || []
})) || []
}));
代码清理
createSubChildren = (children = []) => {
return (
(children &&
children.map(child => ({
value: child.id,
label: child.name
}))) ||
[]
);
};
createChildren = (children = []) => {
return (
children &&
children.map(child => ({
value: child.id,
label: child.name,
children: this.createSubChildren(child.categorySubChildResponses)
}))
) || [];
};
const options = this.state.allCategory.map(category => ({
value: category.id,
label: category.name,
children: this.createChildren(category.categorySubResponses)
}));
推荐阅读
- reactjs - CompressorJS - 它可以将PNG转换为JPG吗?
- java - Android Studio:微调器根本不显示项目。[XML/JAVA]
- android - 我的 webview 应用程序关于链接的问题?
- vagrant - 流浪者 | Homestead - vagrant up 命令连接超时
- json.net - Google OpenID 发现文档的更改有多敏感?
- java - 尝试将 CSV 文件转换为 JSON 格式以便将其保存在 mongodb (spring) 中
- unicode - 浏览器中不显示 unicode 字符
- python - 将 Apply 函数结果添加到原始 df
- javascript - JS 类属性“未定义”,即使它似乎已定义
- r - 数一数护士在接触病人之前洗手的次数:是 X 在 Y 之前,group_by(ID)?