1. 程式人生 > >bootstrap-treeview中關於勾選父節點全選子節點,以及勾選一個子節點自動勾選父節點

bootstrap-treeview中關於勾選父節點全選子節點,以及勾選一個子節點自動勾選父節點

$("#flowOperTypeSingle_tree").treeview({
data : orgTree,
enableLinks : true,
showBorder : false,
showCheckbox:true,
levels:1,
onNodeChecked : function(event, data) {
//點選選中
selected(data);
},
onNodeUnchecked : function(event, node){
//點選取消
unSelected(node);
    }

});

function selected(data){
if(data.fatherId == undefined){
for (var i = 0; i < data.nodes.length; i++) {
$("#flowOperTypeSingle_tree").treeview('checkNode', [ data.nodes[i], {silent: true}]);
}
}else{
var parentNode = $('#flowOperTypeSingle_tree').treeview('getParent', data.nodeId);
$("#flowOperTypeSingle_tree").treeview('checkNode', [ parentNode, {silent: true}]);
}
}

function unSelected(node){
if(node.fatherId == undefined){
for (var i = 0; i < node.nodes.length; i++) {
$("#flowOperTypeSingle_tree").treeview('uncheckNode', [ node.nodes[i], {silent: true}]);
}
}else{
var parentNode = $('#flowOperTypeSingle_tree').treeview('getParent', node.nodeId);
var num;
for (var i = 0; i < parentNode.nodes.length; i++) {
if(parentNode.nodes[i].state.checked == true){
num=1;
}
}
if(num!=1){
$("#flowOperTypeSingle_tree").treeview('uncheckNode', [ parentNode, {silent: true}]);
}
console.log(parentNode);
}
}

每個人的程式碼寫的都不同,根據業務不同需要進行修改.(有不對的地方的請指教)