bootstrap-treeview级联问题

本文介绍如何利用Bootstrap和bootstrap-treeview插件实现一个可全选/全不选的树形结构,包括引入相关文件、HTML结构设置、JavaScript交互逻辑,并展示了具体的实现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、引入bootstrap、bootstrap-treeview相关文件;

二、HTML代码

<div>
    <div id="tree" style="color: #999; font-size: 14px;"></div>
    <div>
        <button type="button" class="btn btn-success" id="btn-check-all">Check All</button>
        <button type="button" class="btn btn-danger" id="btn-uncheck-all">Uncheck All</button>
    </div>
    <div id="output"></div>
</div>

 

三、JS代码

var defaultData = [
                        {
                            "id": 0,
                            "icon": "talent-icon-folder",
                            "text": "计算机科学",
                            "nodes": [
                                {
                                    "id": 1,
                                    "icon": "talent-icon-folder",
                                    "text": "软件工程",
                                    "nodes": [
                                        {
                                            "id": 2,
                                            "icon": "talent-icon-file",
                                            "text": "图形学"
                                        },
                                        {
                                            "id": 3,
                                            "icon": "talent-icon-file",
                                            "text": "大数据运算"
                                        }
                                    ]
                                },
                                {
                                    "id": 4,
                                    "icon": "talent-icon-file",
                                    "text": "微机原理"
                                }
                            ]
                        },
                        {
                            "id": 5,
                            "icon": "talent-icon-folder",
                            "text": "土木工程",
                            "state": {
                                "checked": true
                            }
                        },
                        {
                            "id": 6,
                            "icon": "talent-icon-folder",
                            "text": "市场营销"
                        }
                    ];
                    var $checkableTree = $('#tree').treeview({
                        data: defaultData,
                        levels: 3,
                        showIcon: true,
                        showCheckbox: true,
                        showBorder: false,
                        /*multiSelect: true,*/
		                selectedColor: 'back',
		                selectedBackColor: 'white',
                        expandIcon: 'talent-icon-towards-the-right',
                        collapseIcon: 'talent-icon-down',
                        checkedIcon: 'talent-icon-check',
                        uncheckedIcon: 'talent-icon-checkbox',
                        onNodeChecked: function(event, node) {
                            console.log(node.id);
                            $('#output').prepend('<p>' + node.text + ' was checked</p>');
                            // parent-有子必有父
                            function doCheckedNode(node) {
                                // 初始化
                                var thisDiv = $('#tree');
                                var parentNode = thisDiv.treeview('getParent', node);
                                if(parentNode && 0 <= parentNode.nodeId) {
                                    console.log(parentNode);
                                    // 选中
                                    thisDiv.treeview('checkNode', [ parentNode, { silent: true } ]);
                                    // 递归
                                    doCheckedNode(parentNode);
                                }
                            }
                            doCheckedNode(node);
                        },
                        onNodeUnchecked: function (event, node) {
                            $('#output').prepend('<p>' + node.text + ' was unchecked</p>');
                            // child-无父无子
                            function doUnCheckedNode(node) {
                                // 初始化
                                var thisDiv = $('#tree');
                                if(node && node.nodes && 0 < node.nodes.length) {
                                    var childNodes = node.nodes;
                                    for(var i = 0, len = childNodes.length; i < len; i++) {
                                        // 取消选中
                                        thisDiv.treeview('uncheckNode', [ childNodes[i].nodeId, { silent: true } ]);
                                        // 递归
                                        doUnCheckedNode(childNodes[i]);
                                    }
                                }
                            }
                            doUnCheckedNode(node);
                        }
                    });
                    $('#btn-check-all').on('click', function (e) {
                        $checkableTree.treeview('checkAll', { silent: $('#chk-check-silent').is(':checked') });
                    });
                    $('#btn-uncheck-all').on('click', function (e) {
                        $checkableTree.treeview('uncheckAll', { silent: $('#chk-check-silent').is(':checked') });
                    });

 

四、效果



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值