-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathindex.js
401 lines (379 loc) · 13.3 KB
/
index.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 需要查看相关DEMO的时候,搜索相关函数即可,比如:全选则搜索 checkAll,列表转树搜索 listConvert
layui.config({
base: 'layui_exts/',
}).extend({
authtree: 'authtree',
});
// 获取最大深度样例
function getMaxDept(dst){
layui.use(['jquery', 'layer', 'authtree'], function(){
var layer = layui.layer;
var authtree = layui.authtree;
layer.alert('树'+dst+'的最大深度为:'+authtree.getMaxDept(dst));
});
}
// 全选样例
function checkAll(dst){
layui.use(['jquery', 'layer', 'authtree'], function(){
var layer = layui.layer;
var authtree = layui.authtree;
authtree.checkAll(dst);
});
}
// 全不选样例
function uncheckAll(dst){
layui.use(['jquery', 'layer', 'authtree'], function(){
var layer = layui.layer;
var authtree = layui.authtree;
authtree.uncheckAll(dst);
});
}
// 显示全部
function showAll(dst){
layui.use(['jquery', 'layer', 'authtree'], function(){
var layer = layui.layer;
var authtree = layui.authtree;
authtree.showAll(dst);
});
}
// 隐藏全部
function closeAll(dst){
layui.use(['jquery', 'layer', 'authtree'], function(){
var layer = layui.layer;
var authtree = layui.authtree;
authtree.closeAll(dst);
});
}
// 获取节点状态
function getNodeStatus(dst){
layui.use(['jquery', 'layer', 'authtree', 'laytpl'], function(){
var layer = layui.layer;
var $ = layui.jquery;
var authtree = layui.authtree;
var laytpl = layui.laytpl;
// 获取所有节点
var all = authtree.getAll(dst);
// 获取所有已选中节点
var checked = authtree.getChecked(dst);
// 获取所有未选中节点
var notchecked = authtree.getNotChecked(dst);
// 获取选中的叶子节点
var leaf = authtree.getLeaf(dst);
// 获取最新选中
var lastChecked = authtree.getLastChecked(dst);
// 获取最新取消
var lastNotChecked = authtree.getLastNotChecked(dst);
var data = [
{func: 'getAll', desc: '获取所有节点', data: all},
{func: 'getChecked', desc: '获取所有已选中节点', data: checked},
{func: 'getNotChecked', desc: '获取所有未选中节点', data: notchecked},
{func: 'getLeaf', desc: '获取选中的叶子节点', data: leaf},
{func: 'getLastChecked', desc: '获取最新选中', data: lastChecked},
{func: 'getLastNotChecked', desc: '获取最新取消', data: lastNotChecked},
];
var string = laytpl($('#LAY-auth-tree-nodes').html()).render({
data: data,
});
layer.open({
title: '节点状态'
,content: string
,area: '800px'
,tipsMore: true
});
$('body').unbind('click').on('click', '.LAY-auth-tree-show-detail', function(){
layer.open({
type: 1,
title: $(this).data('title')+'-节点详情',
content: '['+$(this).data('content')+']',
tipsMore: true
});
});
});
}
// 显示到某层
function showDept(dst) {
layui.use(['layer', 'authtree', 'jquery'], function(){
var jquery = layui.jquery;
var layer = layui.layer;
var authtree = layui.authtree;
layer.prompt({title: '显示到某层'}, function(value, index, elem) {
authtree.showDept(dst, value);
layer.close(index);
});
});
}
// 关闭某层以后的所有层
function closeDept(dst) {
layui.use(['layer', 'authtree', 'jquery'], function(){
var jquery = layui.jquery;
var layer = layui.layer;
var authtree = layui.authtree;
layer.prompt({title: '关闭某层以后的所有层'}, function(value, index, elem) {
authtree.closeDept(dst, value);
layer.close(index);
});
});
}
// 树转下拉树
function treeConvertSelect(url) {
layui.use(['layer', 'authtree', 'jquery', 'form', 'code', 'laytpl'], function(){
var $ = layui.jquery;
var layer = layui.layer;
var authtree = layui.authtree;
var form = layui.form;
var laytpl = layui.laytpl;
layer.open({
title: '树转下拉树演示'
,content: '<div id="LAY-auth-tree-convert-select-dom"></div>'
,area: ['800px', '400px']
,tipsMore: true
,success: function() {
$.ajax({
url: url,
dataType: 'json',
success: function(res){
// 更多传入参数及其具体意义请查看文档
var selectList = authtree.treeConvertSelect(res.data.trees, {
childKey: 'list',
// checkedKey: ['glygl-tjgly'],
});
console.log(selectList);
// 渲染单选框
var string = laytpl($('#LAY-auth-tree-convert-select').html()).render({
// 为了 layFilter 的唯一性,使用模板渲染的方式传递
layFilter: 'LAY-auth-tree-convert-select-input',
list: selectList,
code: JSON.stringify(res, null, 2),
});
$('#LAY-auth-tree-convert-select-dom').html(string);
layui.code({
title: '返回的树状数据'
});
form.render('select');
// 使用form.on('select(LAY-FILTER)')监听选中
form.on('select(LAY-auth-tree-convert-select-input)', function(data){
console.log('选中信息', data);
});
},
error: function(xml, errstr, err) {
layer.alert(errstr+',获取样例数据失败,请检查是否部署在本地服务器中!');
}
});
}
});
});
}
function hideChooseSelect(url) {
layui.use(['layer', 'laytpl', 'authtree', 'jquery', 'form', 'code'], function(){
var $ = layui.jquery;
var layer = layui.layer;
var authtree = layui.authtree;
var form = layui.form;
var laytpl = layui.laytpl;
$.ajax({
url: url,
dataType: 'json',
success: function(res){
var trees = res.data.trees;
var content = laytpl($('#LAY-auth-tree-hide-choose').html()).render({
// 为了 渲染ID 的唯一性,使用模板渲染的方式传递
hideChooseDomId: 'LAY-auth-tree-hide-choose-dom',
});
// 弹框展示
layer.open({
title: '隐藏左侧操作演示'
,content: content
,area: ['800px', '400px']
,tipsMore: true
,success: function() {
layui.code({
});
// 如果页面中多个树共存,需要注意 layfilter 需要不一样,否则触发事件会混乱
authtree.render('#LAY-auth-tree-hide-choose-dom', trees, {
layfilter: 'LAY-auth-tree-hide-choose-input',
openall: true,
theme: 'auth-skin-default',
hidechoose: true,// 关键配置
autowidth: true,
});
}
});
},
error: function(xml, errstr, err) {
layer.alert(errstr+',获取样例数据失败,请检查是否部署在本地服务器中!');
}
});
});
}
// 转换列表
function listConvert(url) {
layui.use(['layer', 'laytpl', 'authtree', 'jquery', 'form', 'code'], function(){
var $ = layui.jquery;
var layer = layui.layer;
var authtree = layui.authtree;
var form = layui.form;
var laytpl = layui.laytpl;
$.ajax({
url: url,
dataType: 'json',
success: function(res){
// 支持自定义递归字段、数组权限判断等
// 深坑注意:如果API返回的数据是字符串,那么 startPid 的数据类型也需要是字符串
var trees = authtree.listConvert(res.data.list, {
primaryKey: 'alias'
,startPid: '0'
,parentKey: 'palias'
,nameKey: 'name'
,valueKey: 'alias'
,checkedKey: res.data.checkedAlias
,disabledKey: res.data.disabledAlias
});
var content = laytpl($('#LAY-auth-tree-list-convert').html()).render({
// 为了 渲染ID 的唯一性,使用模板渲染的方式传递
layId: 'LAY-auth-list-convert-index',
code: JSON.stringify(res, null, 2),
codeAns: JSON.stringify(trees, null, 2)
});
// 弹框展示
layer.open({
title: '列表转树演示'
,content: content
,area: ['800px', '400px']
,tipsMore: true
,success: function() {
layui.code({
});
// 如果页面中多个树共存,需要注意 layfilter 需要不一样,否则触发事件会混乱
authtree.render('#LAY-auth-list-convert-index', trees, {
inputname: 'authids[]',
layfilter: 'LAY-auth-list-convert-input',
theme: 'auth-skin-default',
// openall: true,
autowidth: true,
});
}
});
},
error: function(xml, errstr, err) {
layer.alert(errstr+',获取样例数据失败,请检查是否部署在本地服务器中!');
}
});
});
}
/**
* 加群交流弹窗
* @return {[type]} [description]
*/
function groupAdd() {
layui.use(['laytpl', 'layer', 'jquery'], function(){
var laytpl = layui.laytpl;
var layer = layui.layer;
var $ = layui.jquery;
var content = laytpl($('#LAY-auth-tree-group-add').html()).render({});
layer.open({
title: "加群交流"
,area: ['300px', '450px']
,content: content
});
});
}
layui.use(['jquery', 'authtree', 'form', 'layer'], function(){
var $ = layui.jquery;
var authtree = layui.authtree;
var form = layui.form;
var layer = layui.layer;
// 初始化
$.ajax({
url: 'tree.json',
dataType: 'json',
success: function(data){
// 渲染时传入渲染目标ID,树形结构数据(具体结构看样例,checked表示默认选中),以及input表单的名字
authtree.render('#LAY-auth-tree-index', data.data.trees, {
inputname: 'ids[]'
,layfilter: 'lay-check-auth'
// ,dblshow: true
// ,dbltimeout: 180
// ,autoclose: false
// ,autochecked: false
// ,openchecked: false
// ,'collapseLeafNode': false
// ,openall: true
// ,hidechoose: true
// ,checkType: 'radio'
// ,checkedKey: 'checked'
// ,disabledKey: 'disabled'
// ,checkSkin: 'primary'
,'theme': 'auth-skin-default'
// ,'themePath': 'themes/'
// ,collapseLastDepthNode: false
,autowidth: true
,formFilter: 'authtree-submit-form' // 注意!!!如果不与其他插件render冲突,这个选填
});
// PS:使用 form.on() 会引起了事件冒泡延迟的BUG,需要 setTimeout(),并且无法监听全选/全不选
// PS:如果开启双击展开配置,form.on()会记录两次点击事件,authtree.on()不会
/* form.on('checkbox(lay-check-auth)', function(data){
// 注意这里:需要等待事件冒泡完成,不然获取叶子节点不准确。
setTimeout(function(){
console.log('监听 form 触发事件数据', data);
// 获取选中的叶子节点
var leaf = authtree.getLeaf('#LAY-auth-tree-index');
console.log('leaf', leaf);
// 获取最新选中
var lastChecked = authtree.getLastChecked('#LAY-auth-tree-index');
console.log('lastChecked', lastChecked);
// 获取最新取消
var lastNotChecked = authtree.getLastNotChecked('#LAY-auth-tree-index');
console.log('lastNotChecked', lastNotChecked);
}, 100);
});
*/ // 使用 authtree.on() 不会有冒泡延迟
authtree.on('change(lay-check-auth)', function(data) {
console.log('监听 authtree 触发事件数据', data);
// 获取所有节点
var all = authtree.getAll('#LAY-auth-tree-index');
// 获取所有已选中节点
var checked = authtree.getChecked('#LAY-auth-tree-index');
// 获取所有未选中节点
var notchecked = authtree.getNotChecked('#LAY-auth-tree-index');
// 获取选中的叶子节点
var leaf = authtree.getLeaf('#LAY-auth-tree-index');
// 获取最新选中
var lastChecked = authtree.getLastChecked('#LAY-auth-tree-index');
// 获取最新取消
var lastNotChecked = authtree.getLastNotChecked('#LAY-auth-tree-index');
console.log(
'all', all,"\n",
'checked', checked,"\n",
'notchecked', notchecked,"\n",
'leaf', leaf,"\n",
'lastChecked', lastChecked,"\n",
'lastNotChecked', lastNotChecked,"\n"
);
});
authtree.on('deptChange(lay-check-auth)', function(data) {
console.log('监听到显示层数改变',data);
});
authtree.on('dblclick(lay-check-auth)', function(data) {
console.log('监听到双击事件',data);
});
},
error: function(xml, errstr, err) {
layer.alert(errstr+',获取样例数据失败,请检查是否部署在本地服务器中!');
}
});
// 表单提交样例
form.on('submit(LAY-auth-tree-submit)', function(obj){
var authids = authtree.getChecked('#LAY-auth-tree-index');
console.log('Choosed authids is', authids);
// obj.field.authids = authids;
$.ajax({
url: 'tree.json',
dataType: 'json',
data: obj.field,
success: function(res){
layer.alert('提交成功!');
}
});
return false;
});
});