reactjs - 我想在反应 js 中创建检查嵌套数据的复选框
问题描述
我的数据在嵌套数组对象中。我想像树视图一样选中/取消选中节点。IE。when any child node is selected then the parent node is checked itself.
这是我的嵌套 JSON。从这个对象,我从这个数据创建一个树视图:
const nodes = [
{
value: "/app",
label: "app",
children: [
{
value: "/app/Http",
label: "Http",
children: [
{
value: "/app/Http/Controllers",
label: "Controllers",
children: [
{
value: "/app/Http/Controllers/WelcomeController.js",
label: "WelcomeController.js",
},
],
},
{
value: "/app/Http/routes.js",
label: "routes.js",
},
],
},
{
value: "/app/Providers",
label: "Providers",
children: [
{
value: "/app/Http/Providers/EventServiceProvider.js",
label: "EventServiceProvider.js",
},
],
},
],
},
{
value: "/config",
label: "config",
children: [
{
value: "/config/app.js",
label: "app.js",
},
{
value: "/config/database.js",
label: "database.js",
},
],
},
{
value: "/public",
label: "public",
children: [
{
value: "/public/assets/",
label: "assets",
children: [
{
value: "/public/assets/style.css",
label: "style.css",
},
],
},
{
value: "/public/index.html",
label: "index.html",
},
],
},
{
value: "/.env",
label: ".env",
},
{
value: "/.gitignore",
label: ".gitignore",
},
{
value: "/README.md",
label: "README.md",
},
];
我正在使用此功能在检查孩子时检查父母。
checkChange(targetNode: any, event) {
/// debugger;
const targetNodeId = targetNode.id;
this.findIndexNestedforCheckbox(targetNode, targetNodeId);
let newTableData = [...this.state.tableData];
this.setState({ tableData: newTableData, isActionFooter: true });
}
findIndexNestedforCheckbox(data, index) {
if (data.id === index) data.isChecked = "Yes";
let result;
const i = (data.children || []).findIndex((child) => {
child.isChecked = "Yes";
return (result = this.findIndexNestedforCheckbox(child, index));
});
if (result) return [i, ...result];
}
解决方案
推荐阅读
- database - 警告:无法打开统计文件“pg_stat_tmp/global.stat”:过时的文件句柄
- autodesk-forge - 在 Autodesk Forge 中创建新面板
- android - 我的 Android 工作室可绘制资源文件夹以非常拥挤的方式导入
- angular - 当 aot=true(生产)时 Angular 10 吨构建错误
- javascript - Javascript - 从嵌套数组中删除对象
- flutter - 如何使用 JSON 数据制作表单
- leaflet - 将鼠标悬停在传单中的任何特征上时触发多个特征的事件
- javascript - 如何使用 Selenium Python 在新选项卡中打开 JavaScript 链接?
- apache-spark - hadoop yarn UI中的“队列百分比”指的是什么
- python - 如何将所有 mp4 文件名存储在数组中