【发布时间】:2019-08-22 11:29:00
【问题描述】:
对于应用程序的设置页面,我实现了一个具有启用(绿色)或禁用(红色)状态的滑块。并且父级的设置是根据其子级的值计算的。
//Getting the switches configuration inside componnetDidMount something like this
var obj = [
{
parent_header_name: "parent1",
children_info: [
{
child_switch_name: "child1",
isEnabled: true
},
{
child_switch_name: "child2",
isEnabled: false
}
]
},
{
parent_header_name: "parent2",
children_info: [
{
child_switch_name: "child3",
isEnabled: true
}
]
},
{
parent_header_name: "parent3",
children_info: [
{
child_switch_name: "child4",
isEnabled: false
}
]
}
];
现在基于这个值,我需要形成一个父母和孩子的分组,如下所示:
Label(the value should be parent_header_name) : Parent Switch Component
Label for children(children_switch_name) : Child Switch Component
此外,在更改单个子开关切换时,我需要获取该开关的信息,如下所示:
例如,将 parent1 的 child1 更改为禁用
[
{
parent_header_name: "parent1",
children_info: [
{
child_switch_name: "child1",
isEnabled: false
}
]
}
];
如果 parent1 启用,我需要获取其所有子项值
[
{
parent_header_name: "parent1",
children_info: [
{
child_switch_name: "child1",
isEnabled: true
},
{
child_switch_name: "child2",
isEnabled: true
}
]
}
]
当父母开关被切换时(当父母被启用时,孩子将被启用,当被禁用时孩子将被禁用;),我需要获取该父母的全部信息
另外,我需要避免切换到“部分”状态,只应启用或禁用父级。 “部分”只是代表性的
为此,我将 react-multi-toggle 用于此切换开关。
我尝试过这样的事情:https://codesandbox.io/s/parent-child-switches-gxfx6
【问题讨论】:
-
请澄清:您的问题到底是什么?您的示例似乎在我的浏览器中运行
-
@Taxel 我已经硬编码了开关配置,我的意思是我没有动态生成开关。现在基于上面的对象数组,我需要将父开关和子开关分组,并在单个切换时获取该开关的信息。
-
还是一头雾水。我会考虑用更多代码以明确直接的方法重写问题。
-
@iwaduarte 会有很多代码。所以尽量避免它。如果您看到 Settings.js 渲染方法,则每个组始终有一个父级和两个子级。但是我有这个用例,我可以跨组为父级创建任意数量的子级,因此要根据上述对象数组动态渲染它,我需要帮助。希望你能得到我:)
标签: javascript reactjs setstate