assignRole.js
6.09 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;
}
})