angularjs - 树视图复选框充当单选按钮
问题描述
示例图:
我希望这组特定的节点充当单选按钮(只应选中 1 个)。我知道我可以通过硬编码条件来处理这个问题,但我想通过更改其 json 列“组”来使其在未来可扩展(添加更多复选框)。
样本数据:
[
{ id:"1", text: "Items", expanded: true, List: [
{ id:"2",text: "book" ,group: 1},//group for radiobutton actions
{ id:"3",text: "chair",group: 1 },
{ id:"4",text: "table",group: 1 },
{ id:"5",text: "mat", group: 0 },
{ id:"6",text: "decor", group: 0}
] }
]
我找到了这个关于引用组属性的jsfiddle 示例。
解决方案
这是最好的(我认为)方法:
1. 声明要分组的 ID 数组:
var group = ["2","3","4"]; //in my example above
2. 在check
事件中:
if (group.indexOf(dataItem.ID) > -1) { //if the ID you clicked exists in the group
group.splice(group.indexOf(dataItem.ID), 1); //remove the ID from the group
for (var i = 0, j = treeview.length; i < j; i++) {
for (var x = 0, y = treeview[i].List.length; x < y; x++) {
if (group.indexOf(treeview[i].List[x].ID) > -1) {
treeview[i].List[x].set("checked", false); //uncheck the members of the group
}
}
}
}
推荐阅读
- python - 将二进制文件拆分为两个文件
- indexing - 预处理器输出中的 C 表索引
- react-redux - 如何应用 Redux 创建点击计数器
- python - 如何将 pylint 检查仅限于当前 repo 定义的对象?
- machine-learning - 如何将未来预测用作时间序列 LSTM 模型中的输入变量?
- kdb - 取消组合给定表中的多个列
- react-native - React Native 中的排序渲染
- json - Powershell Invoke-WebRequest XML 数据
- azure - 在 Azure DevOps 如何使用 REST API 触发发布?
- javascript - 在哪里安全地存储用于来自 Monzo bank api 的 api 请求的访问令牌