layui树(tree)获取所有选中的节点id

本文介绍了在layui框架中如何操作tree组件,详细解析了如何获取所有被选中的节点id,对于使用layui进行前端开发的人员来说,这是一个实用的技巧。
<%--html--%>
<div id="roleTree"></div>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn" id="sub" lay-demo="getChecked" lay-submit lay- filter="addGrant">立即提交</button>
    </div>
</div>
<script>

    //渲染tree
    layui.use(['form', 'tree'], function () {
        var tree = layui.tree
            , form = layui.form;
        var inst1 = tree.render({
                elem: '#roleTree'  //绑定元素
                ,
                id: 'tree1'
                ,
                data: [{
                    "id": 1,
                    "title": "公司管理",
                    "children": [{
                        "id": 4,
                        "title": "人员列表",
                        "children": [{
                            "id": 5,
                            "title": "添加人员",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }, {"id": 6, "title": "修改人员", "children": null, "spread": true, "checked": true}, {
                            "id": 7,
                            "title": "删除人员",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }],
                        "spread": true,
                        "checked": null
                    }],
                    "spread": true,
                    "checked": null
                }, {
                    "id": 8,
                    "title": "用户管理",
                    "children": [{
                        "id": 9,
                        "title": "用户列表",
                        "children": [{
                            "id": 11,
                            "title": "添加用户",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }, {"id": 12, "title": "删除用户", "children": null, "spread": true, "checked": true}, {
                            "id": 13,
                            "title": "用户赋角色",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }],
                        "spread": true,
                        "checked": null
                    }, {
                        "id": 10,
                        "title": "角色列表",
                        "children": [{
                            "id": 14,
                            "title": "添加角色",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }, {"id": 15, "title": "删除角色", "children": null, "spread": true, "checked": true}, {
                            "id": 16,
                            "title": "角色赋权",
                            "children": null,
                            "spread": true,
                            "checked": true
                        }],
                        "spread": true,
                        "checked": null
                    }],
                    "spread": true,
                    "checked": null
                }]
                ,
                showCheckbox: true
            })
        ;
        // 监听点击按钮
        $("#sub").click(function(){
            //获取所有选中节点id数组
            var checkedData = tree.getChecked('tree1'); //获取选中节点的数据
            var nodeIds = new Array();
            nodeIds = getCheckedId(checkedData);
            console.log(nodeIds);
        });
    });


    //获取所有选中的节点id
    function getCheckedId(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            //item 没有children属性
            if (item.children != null) {
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            }
        });
        return id;
    }

</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值