assignRole.js 6.09 KB
layui.config({
    base: "/js/"
}).use(['form', 'I18nPage', 'vue', 'ztree', 'layer', 'jquery', 'table', 'droptree', 'hhweb', 'utils'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        I18nPage = layui.I18nPage,
        table = layui.table,
        hhweb = layui.hhweb;

    var id = $.getUrlParam("id");      //待分配的id
    var menudata = new Array(); //菜单数据

    var cols_arr = I18nPage.GetI18nTable(
        {
            TableNo: "base-SysRoleAssign",
        }
    );

    var config = {};

    var mainList = {
        Render: function (options) {
            if (options != undefined) {
                $.extend(config, options);
            }

            mainList.Table = table.render({
                elem: '#mainList'
                , url: '/base/SysRole/Load'
                , method: "post"
                , page: true //开启分页
                , cols: hhweb.ColumnSetting('mainList', cols_arr)
                , id: 'mainList'
                , limit: 20
                , limits: [20, 50, 100, 200, 500, 1000]
                , toolbar: false
                , height: 'full-15'
                , cellMinWidth: 80
                , size: 'sm'
                , where: config
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
                , done: function (res) {
                    $.ajax("/base/SysRole/LoadForUser", {
                        async: true
                        , type: "post"
                        , data: { userId: id }
                        , dataType: 'json'
                        , success: function (result) {
                            if (result.Code == 500) return;
                            var roles = result.Result;
                            //循环所有数据,找出对应关系,设置checkbox选中状态
                            for (var i = 0; i < res.data.length; i++) {
                                //获取当前所有菜单的ID数组
                                menudata[i] = res.data[i].Id;

                                for (var j = 0; j < roles.length; j++) {
                                    if (res.data[i].Id != roles[j]) continue;

                                    //这里才是真正的有效勾选
                                    res.data[i]["LAY_CHECKED"] = true;
                                    //找到对应数据改变勾选样式,呈现出选中效果
                                    var index = res.data[i]['LAY_TABLE_INDEX'];
                                    $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                                    $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                                }
                            }

                            //如果构成全选
                            var checkStatus = table.checkStatus('mainList');
                            if (checkStatus.isAll) {
                                $('.layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true);
                                $('.layui-table-header th[data-field="0"] input[type="checkbox"]').next().addClass('layui-form-checked');
                            }
                        }
                    });

                    //分配及取消分配
                    table.on('checkbox(mainList)', function (obj) {
                        if (obj.checked == true) {
                            obj.tr.addClass('layui-table-click');
                        } else {
                            obj.tr.removeClass('layui-table-click');
                        }

                        var editdata = GetObjData(obj);
                        var url = "/base/SysRelevance/Assign";
                        if (!obj.checked) {
                            url = "/base/SysRelevance/UnAssign";
                        }
                        $.post(url, { type: "UserRole", firstId: id, secIds: editdata }
                            , function (data) {
                                layer.msg(data.Message);
                            }
                            , "json");
                    });
                }
            });

            return mainList.Table;
        },
        Load: function (options) {
            if (options != undefined) {
                $.extend(config, options);
            }
            if (mainList.Table == undefined) {
                mainList.Table = this.Render(options);
                return;
            }
            table.reload('mainList', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: config
            });
        }
    };

    mainList.Load();

    function GetObjData(obj) {
        var editdata;
        if (obj.type == "one") {
            if (obj.data.Id == undefined) {
                if (obj.checked) {
                    editdata = menudata
                }
                else {
                    var checkStatus = table.checkStatus('mainList');
                    var data = checkStatus.data; //获取选中行的数据
                    if (data.length == 0) {
                        editdata = menudata
                    }
                    else {
                        var tmpary = new Array();
                        var datacopy = new Array();
                        for (var i = 0; i < data.length; i++) {
                            datacopy[i] = data[i].Id;
                        }
                        for (var i = 0; i < menudata.length; i++) {
                            if (datacopy.indexOf(menudata[i]) < 0) {
                                tmpary[tmpary.length] = menudata[i];
                            }
                        }
                        editdata = tmpary;
                    }
                }
            }
            else {
                editdata = obj.data.Id;
            }
        }
        else {
            editdata = menudata
        }

        return editdata;
    }
})