Introduction and use of the treeview plug-in in bootstrap

零下一度
Release: 2018-05-14 14:56:46
Original
4093 people have browsed it

The project needs to implement permission management and use the front-end framework bootstrap, so I directly selected the treeview extension plug-in of bootstrap. First picture:

When the parent node is selected, all child nodes under the parent node are also selected, see code

1 , HTML code

TreeView Checkable

Copy after login

2, Json data

function getTvStateData() { var defaultData = [ { text: 'Parent 1', href: '#parent1', tags: ['4'], state: { checked: true }, nodes: [ { text: 'Child 1', href: '#child1', tags: ['2'], nodes: [ { text: 'Grandchild 1', href: '#grandchild1', tags: ['0'] }, { text: 'Grandchild 2', href: '#grandchild2', tags: ['0'] } ] }, { text: 'Child 2', href: '#child2', tags: ['0'] } ] }, { text: 'Parent 2', href: '#parent2', tags: ['0'], nodes: [ { text: 'Child 1', href: '#child1', tags: ['2'], nodes: [ { text: 'Grandchild 1', href: '#grandchild1', tags: ['0'] }, { text: 'Grandchild 2', href: '#grandchild2', tags: ['0'] } ] }, { text: 'Child 2', href: '#child2', tags: ['0'] } ] }, { text: 'Parent 3', href: '#parent3' }, { text: 'Parent 4', href: '#parent4', tags: ['0'] }, { text: 'Parent 5', href: '#parent5', tags: ['0'] } ]; return defaultData; }
Copy after login

3, JS data binding, loading TreeView

$(function() { var $checkableTree = $('#treeview-checkable') .treeview({ data: getTvStateData(), //数据 showIcon: false, showCheckbox: true, levels: 1, onNodeChecked: function(event, node) { //选中节点 var selectNodes = getChildNodeIdArr(node); //获取所有子节点 if (selectNodes) { //子节点不为空,则选中所有子节点 $('#treeview-checkable').treeview('checkNode', [selectNodes, { silent: true }]); } var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId); setParentNodeCheck(node); }, onNodeUnchecked: function(event, node) { //取消选中节点 var selectNodes = getChildNodeIdArr(node); //获取所有子节点 if (selectNodes) { //子节点不为空,则取消选中所有子节点 $('#treeview-checkable').treeview('uncheckNode', [selectNodes, { silent: true }]); } }, onNodeExpanded: function(event, data) { if (data.nodes === undefined || data.nodes === null) { } else if (data.tags[0] === "2") { alert("Tags 2"); } else { alert("1111"); } } }); });
Copy after login

4, select/cancel when the parent node is selected/unselected Select all child nodes, and when all child nodes are selected, select the parent node

function getChildNodeIdArr(node) { var ts = []; if (node.nodes) { for (x in node.nodes) { ts.push(node.nodes[x].nodeId); if (node.nodes[x].nodes) { var getNodeDieDai = getChildNodeIdArr(node.nodes[x]); for (j in getNodeDieDai) { ts.push(getNodeDieDai[j]); } } } } else { ts.push(node.nodeId); } return ts; } function setParentNodeCheck(node) { var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId); if (parentNode.nodes) { var checkedCount = 0; for (x in parentNode.nodes) { if (parentNode.nodes[x].state.checked) { checkedCount ++; } else { break; } } if (checkedCount === parentNode.nodes.length) { $("#treeview-checkable").treeview("checkNode", parentNode.nodeId); setParentNodeCheck(parentNode); } } }
Copy after login

The above is the detailed content of Introduction and use of the treeview plug-in in bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!