assignModule.js
7.35 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
layui.config({
base: "/js/"
}).use(['form', 'vue', 'ztree', 'layer', 'jquery', 'utils', 'table'], function () {
var layer = (top == undefined || top.layer === undefined) ? layui.layer : top.layer,
$ = layui.jquery;
var table = layui.table;
var id = $.getUrlParam("id"); //待分配的id
var type = $.getUrlParam("type"); //待分配的类型
var menuType = $.getUrlParam("menuType"); //待分配菜单的类型
var menudata = new Array(); //菜单数据
//菜单列表
var menucon = {}; //table的参数,如搜索key,点击tree的id
var mainList = function (options) {
if (options != undefined) {
$.extend(menucon, options);
}
table.reload('mainList', {
url: '/base/SysModule/LoadMenus'
, method: 'POST'
, where: menucon
, done: function (res, curr, count) {
//如果是异步请求数据方式,res即为你接口返回的信息。
//如果是直接赋值的方式,res即为:{data: [], count: 99} data为当前页数据、count为数据总长度
var url = "/base/SysModule/LoadMenusForUser";
if (type.indexOf("Role") != -1) {
url = "/base/SysModule/LoadMenusForRole";
}
$.ajax(url, {
async: true
, type: "post"
, data: {
firstId: id
, moduleId: options.moduleId
}
, dataType: "json"
, success: function (result) {
//循环所有数据,找出对应关系,设置checkbox选中状态
for (var i = 0; i < res.data.length; i++) {
//获取当前所有菜单的ID数组
menudata[i] = res.data[i].Id;
for (var j = 0; j < result.length; j++) {
if (res.data[i].Id != result[j].Id) 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');
}
}
});
}
});
}
//模块列表
var ztree = function () {
var url = '/base/UserSession/GetModules';
var zTreeObj;
var setting = {
view: { selectedMulti: true },
check: {
enable: true,
chkStyle: "checkbox",
chkboxType: { "Y": "", "N": "" } //去掉勾选时级联
},
data: {
key: {
name: 'Name',
title: 'Name'
},
simpleData: {
enable: true,
idKey: 'Id',
pIdKey: 'ParentId',
rootPId: 'null'
}
},
callback: {
onClick: function (event, treeId, treeNode) {
mainList({ moduleId: treeNode.Id });
},
onCheck: function (event, treeId, treeNode) {
var url = "/base/SysRelevance/Assign";
if (!treeNode.checked) {
url = "/base/SysRelevance/UnAssign";
}
$.post(url, { type: type, firstId: id, secIds: [treeNode.Id] }
, function (data) {
layer.msg(data.Message);
}
, "json");
}
}
};
var load = function () {
$.getJSON(url, function (json) {
zTreeObj = $.fn.zTree.init($("#tree"), setting);
zTreeObj.addNodes(null, json);
//如果该用户已经分配模块了,则设置相应的状态
var url = "/base/SysModule/LoadForUser";
if (type.indexOf("Role") != -1) {
url = "/base/SysModule/LoadForRole";
}
$.getJSON(url, { firstId: id }
, function (data) {
$.each(data,
function (i) {
var that = this;
var node = zTreeObj.getNodeByParam("Id", that.Id, null);
zTreeObj.checkNode(node, true, false);
});
});
zTreeObj.expandAll(true);
});
};
load();
return {
reload: load
}
}();
$("#tree").height($("div.layui-table-view").height());
//分配及取消分配
table.on('checkbox(list)', function (obj) {
var editdata = GetObjData(obj);
var url = "/base/SysRelevance/Assign";
if (!obj.checked) {
url = "/base/SysRelevance/UnAssign";
}
$.post(url, { type: menuType, firstId: id, secIds: editdata }
, function (data) {
layer.msg(data.Message);
}
, "json");
});
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;
}
//监听页面主按钮操作 end
})