bootstrap-Treeview实现级联勾选

实例化Tree

1
2
3
4
5
6
$('#searchTree').treeview({  
showCheckbox:true,
data:treeData,
onNodeChecked:nodeChecked ,
onNodeUnchecked:nodeUnchecked
});

核心方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var nodeCheckedSilent = false;  
function nodeChecked (event, node){
if(nodeCheckedSilent){
return;
}
nodeCheckedSilent = true;
checkAllParent(node);
checkAllSon(node);
nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked (event, node){
if(nodeUncheckedSilent)
return;
nodeUncheckedSilent = true;
uncheckAllParent(node);
uncheckAllSon(node);
nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
$('#searchTree').treeview('checkNode',node.nodeId,{silent:true});
var parentNode = $('#searchTree').treeview('getParent',node.nodeId);
if(!("nodeId" in parentNode)){
return;
}else{
checkAllParent(parentNode);
}
}
//取消全部父节点
function uncheckAllParent(node){
$('#searchTree').treeview('uncheckNode',node.nodeId,{silent:true});
var siblings = $('#searchTree').treeview('getSiblings', node.nodeId);
var parentNode = $('#searchTree').treeview('getParent',node.nodeId);
if(!("nodeId" in parentNode)) {
return;
}
var isAllUnchecked = true; //是否全部没选中
for(var i in siblings){
if(siblings[i].state.checked){
isAllUnchecked=false;
break;
}
}
if(isAllUnchecked){
uncheckAllParent(parentNode);
}

}

//级联选中所有子节点
function checkAllSon(node){
$('#searchTree').treeview('checkNode',node.nodeId,{silent:true});
if(node.nodes!=null&&node.nodes.length>0){
for(var i in node.nodes){
checkAllSon(node.nodes[i]);
}
}
}
//级联取消所有子节点
function uncheckAllSon(node){
$('#searchTree').treeview('uncheckNode',node.nodeId,{silent:true});
if(node.nodes!=null&&node.nodes.length>0){
for(var i in node.nodes){
uncheckAllSon(node.nodes[i]);
}
}
}