首页 > 解决方案 > 树视图复选框充当单选按钮

问题描述

示例图:

在此处输入图像描述

我希望这组特定的节点充当单选按钮(只应选中 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 示例。

标签: angularjscheckboxkendo-uiradio-buttontreeview

解决方案


这是最好的(我认为)方法:

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
               }
           }
       }
   }

推荐阅读