wzp
2021-07-19 e65183d31755a0e5fae4bf428435d2e0fd6afdc5
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
@{
    ViewBag.Title = "Assign";
    Layout = "~/Views/Shared/_LayoutList.cshtml";
}
 
<style type="text/css">
    .div-collapse {
        float: left;
        height: 18px;
        line-height: 18px;
        padding-left: 5px;
    }
 
    .layui-table-cell .layui-form-checkbox[lay-skin=primary] {
        top: -1px;
        padding-left: 25px;  /* 覆盖layui padding */
    }
</style>
<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["treeGrid", "form", "okUtils"], function () {
        let treeGrid = layui.treeGrid;
        let form = layui.form;
        let okUtils = layui.okUtils;
        let $ = layui.$;
 
        treeGrid.render({
            elem: "#tableId",
            url: "/Permissions/Module/ModuleButtonList?roleId=@ViewBag.RoleId",
            page: false,
            toolbar: "#toolbarTpl",
            size: "sm",
            cellMinWidth: 100,
            treeId: 'Id',   //树形id字段名称
            treeUpId: 'ParentId',   //树形父id字段名称
            treeShowName: 'FullName',   //以树形式显示的字段
            cols: [[
                { field: "Id", title: "ID", width: 80, sort: true },
                { field: "FullName", title: "菜单名称", width: 200 ,type:'checkbox_txt' },
                { field: "ModuleButtonHtml", title: "按钮权限" },
                { field: "Icon", title: "菜单图标", width: 80, templet: '<span>{{showIcon(d.Icon)}}<span>' },
                { field: "SortCode", title: "排序码", width: 80 },
                { title: "操作", width: 120, align: "center", fixed: "right", templet: "#operationTpl"}
            ]]
        });
 
        //关闭页面
        function CloseWin() {
            parent.location.reload(); // 父页面刷新
            parent.layer.close(parent.layer.getFrameIndex(window.name));//先得到当前iframe层的索引 再执行关闭
        }
 
        //折叠展开
        $("#btnFold").on('click', function () {
            $(".div-collapse.root .layui-tree-head").click();
        });
 
        //保存设置
        $("#btnSave").on('click', function () {
            var _roleId = @ViewBag.RoleId;
            var cbxs = $(".layui-table").find('tbody input[type="checkbox"]');
            var _list = [];
            cbxs.each(function (index, item) {
                if (item.checked == true) {
                    var _row = { RoleId: 0, ModuleId: 0, ButtonId: 0 };
                    var _name = item.name;//chx_2
                    if (_name.indexOf('cbx_') > -1) {
                        _name = _name.replace('cbx_', '');
                    } else {
                        _name = item.getAttribute('tag');
                        if (_name.indexOf('cbx_') > -1) {
                            _name = _name.replace('cbx_', '');
                        }
                    }
                    _row.RoleId = _roleId;
                    _row.ModuleId = _name;
                    _row.ButtonId = item.value == "on" ? null : item.value;
                    _list.push(_row);
                }
            });
            okUtils.ajax("/Permissions/RoleAuthorize/InsertBatch", "post", { "list": _list, roleId: _roleId }, true).done(function (response) {
                okUtils.tableSuccessMsg(response.message);
                //没开启分页,没确定按钮,手动刷新
                setTimeout(CloseWin, 1500);
            }).fail(function (error) {
                console.log(error)
            });
        });
 
        treeGrid.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            if (obj.event === 'selectAll') {  //全选
                $("input[name='cbx_" + data.Id + "']").prop("checked", true);
            } else if (obj.event === 'cancleSelectAll') {//反选
                $("input[name='cbx_" + data.Id + "']").prop("checked", false);
            }
            form.render('checkbox');
        });
 
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class='layui-btn layui-btn-sm' id='btnFold'><i class='ok-icon'>&#xe6d9;</i>全部折叠/展开</button>
    <button class='layui-btn layui-btn-sm' id='btnSave'><i class='ok-icon'>&#xe68a;</i>保存设置</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    {{# if(d.ModuleButtonHtml!=""){}}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="selectAll">全选</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="cancleSelectAll">反选</a>
    {{# } }}
</script>