-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableTree.js
More file actions
623 lines (549 loc) · 16.1 KB
/
Copy pathTableTree.js
File metadata and controls
623 lines (549 loc) · 16.1 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
version 1.1.0
depend on jquery(测试用1.8.3)
TableTree
CheckBoxTableTree
data : [{
'id': 'id2',
'pid': '0',
'name': '总行2',
'isParent': false,
'attr': {
'code': '123',
'type': '总行',
'status': '激活'
},
'children': []
}]
option : {
['ajaxURL' : string]//发送ajax获取下一层数据的url,若为undefined/null 则不发送ajax请求
'thKey' : array //table中column所对应的key,按照数组中key顺序显示列
'data' : array //需要显示的数据数组,data数据结构如上显示
'tableArea' : object//table的jQuery对象
}
*/
/*
*@description 生成树形结构的table(仅tbody内的tr部分)
*@param {object} 生成树所需要的参数
*/
function TableTree(option){
this.ajaxURL = option.ajaxURL;
this.setting = {
doneFlag : ['t', 'f', 'n'],
foldFlag : ['t', 'f', 'n'],
foldClass : ['tt-fold', 'tt-unfold'],
txtClass : 'tt-txt',
pdLeftBase : 16,
};
//this.thName = option.thName;
this.thKey = option.thKey;
this.initData = option.data;
this.dataCache = {};
this.selectedId = '';
//this.tableArea = option.tableArea;
this.tableEl = option.tableArea;
//改为外部调用(由于继承方式原因)
//this.init.call(this, option.data);
}
TableTree.prototype = {
constructor : TableTree,
/*
*@description 初始化TableTree
*@return {object} 对象本身
*/
init : function(){
var data = this.initData;
if(!data || data.constructor != Array){
return this;
}
this.addDataCache(data, 0);
//var table = this.initTable();
//this.tableEl = table;
this.tableEl.find('tbody').append(this.getTrHtml(data));
//this.tableArea.append(table);
this.showRoot();
this.bindEvent();
return this;
},
/*
*@description 绑定相应事件
*@return {undefined}
*/
bindEvent : function(){
this.bindFoldClick();
},
/*
*@description 将数据加工后以id:object的形式存入this.dataCache
*@param data {array} 树形结构的原始数据
*@param n {number} data中第一层数据在所有数据中的层数
*@return {object} 对象本身
*/
addDataCache : function(data, n){
var i = 0,
_dataCache = this.dataCache,
_data,
_doneFlag = this.setting.doneFlag,
_foldFlag = this.setting.foldFlag;
i = data.length;
while(i){
_data = data[--i];
var hasChildren = !!_data.children.length;
_data.isParent = _data.isParent || (this.ajaxURL || hasChildren ? true : false);
_data.isDone = _data.isParent ? (hasChildren ? _doneFlag[0] : _doneFlag[1]) : _doneFlag[2];
_data.isFold = _data.isParent ? _foldFlag[0] : _foldFlag[2];
_data.layer = n;
_dataCache[_data.id] = _data;
if(hasChildren){
_data.isDone = _doneFlag[0];
this.addDataCache.call(this, _data.children, n + 1);
}
}
return this;
},
/*
*然而并没有什么×用
*/
initTable : function(){
var html = ['<table><thead><tr>'],
_thName = this.thName;
for(var i = 0, len = _thName.length; i < len; i++){
html.push('<td>' + _thName[i] + '</td>');
}
html.push('</tr></thead><tbody></tbody></table>');
return $(html.join(''));
},
/*
*@description 通过data生成html字符串
*@param data {array} 加工后的数据,根据数据中的属性生成tr
*@return {string} 生成的html
*/
getTrHtml : function(data){
if(!data || data.constructor != Array){
return '';
}
var html = [],
_setting = this.setting,
paddingLeft = _setting.pdLeftBase * data[0].layer,
_thKey = this.thKey,
_foldClassTrue = _setting.foldClass[0],
_txtClass = _setting.txtClass;
for(var j = 0, len = data.length; j < len; j++){
var row = data[j],
cData = row.attr,
str = '<tr data-id="' + row.id + '"><td style="padding-left:'
+ paddingLeft + 'px;"><span class="fold-btn '
+ (row.isParent ? _foldClassTrue : '') + '"></span><div class="'
+ _txtClass + '">' + (cData.hasOwnProperty(_thKey[0]) ? cData[_thKey[0]] : row[_thKey[0]]) + '</div></td>';
for(var i = 1, cLen = _thKey.length; i < cLen; i++){
str += '<td>' + ((cData.hasOwnProperty(_thKey[i]) ? cData[_thKey[i]] : row[_thKey[i]]) || '') + '</td>';
}
str += '</tr>';
html.push(str);
if(row.children.length){
html.push(this.getTrHtml.call(this, row.children));
}
}
return html.join('');
},
/*
*@description 获取下一层数据的ajax成功返回数据后执行的操作
* 1.将data加工然后存入dataCache
* 2.生成新数据的html并展示在页面上
*@param data {array} 新增的数据
*@return {object} 对象本身
*/
ajaxCallback : function(data){
var _dataCache = this.dataCache,
_selectedId = this.selectedId,
_selectedRow = this.dataCache[_selectedId],
_layer = _selectedRow.layer + 1;
this.addDataCache(data, _layer);
//将新增数据push入相应的父级children中
for(var i = 0, len = data.length, row; i < len; i++){
row = data[i];
_dataCache[row.pid].children.push(row);
}
this.tableEl.find('tr[data-id=' + _selectedId + ']').after(this.getTrHtml.call(this, data));
this.toggle(_selectedId);
return this;
},
/*
*@description 绑定展开/收起及节点事件
*@return {undefined}
*/
bindFoldClick : function(){
var _this = this;
_this.tableEl.on('click', 'tbody .fold-btn', function(){
var _span = $(this),
id = _span.parent().parent().attr('data-id'),
obj = _this.dataCache[id],
_isDone = obj.isDone,
_doneFlag = _this.setting.doneFlag;
_this.selectedId = id;
if(_isDone === _doneFlag[1] && _this.ajaxURL){
$.ajax({
type : 'POST',
url : _this.ajaxURL,
data : {pid : id},
dataType : 'JSON',
success : function(result){
if(result.code === '0' && result.data.length){
_this.ajaxCallback(result.data);
obj.isDone = _doneFlag[0];
}else{
_this.setNotParent(id);
}
}
});
}else if(_isDone === _doneFlag[0]){
_this.toggle(id);
}
});
},
/*
*@description 将节点设置为非父节点
*@param pid {string} 指定节点的id
*@return {object} 对象本身
*/
setNotParent : function(id){
var row = this.dataCache[id];
_setting = this.setting,
_foldClass = _setting.foldClass,
_doneNone = _setting.doneFlag[2],
_foldNone = _setting.foldFlag[2];
this.tableEl.find('tr[data-id=' + id + '] .fold-btn').removeClass(_foldClass[0] + ' ' + _foldClass[1]);
row.isDone = _doneNone;
row.isFold = _foldNone;
row.isParent = false;
},
searchById : function(id){
var _thisEl = this.tableEl.find('tr[data-id=' + id + ']');
this.unfoldParent(id);
var top = _thisEl.addClass('hover').offset().top - 300;
$(document.body).scrollTop(Math.max(0, top));
return this;
},
unfoldParent : function(id){
if(this.dataCache[id]) {
var _foldFlagTrue = this.setting.foldFlag[0],
pid = this.dataCache[id].pid,
parent = this.dataCache[pid];
if (parent && parent.isFold === _foldFlagTrue) {
this.fold(pid, false);
this.unfoldParent(parent.pid);
}
}
return this;
},
/*
*@description 展开/收起指定id的节点
*@param pid {string} 指定节点的id
*@return {object} 对象本身
*/
toggle : function(pid){
var _foldFlagTrue = this.setting.foldFlag[0],
isFold = this.dataCache[pid].isFold !== _foldFlagTrue;
this.fold(pid, isFold);
return this;
},
/*
*@description 展开/收起指定id的节点
*@param pid {string} 指定节点的id
*@param flag {boolean} 指示展开还是收起{true : 展开, false : 收起}
*@return {object} 对象本身
*/
fold : function(pid, flag){
var _tableEl = this.tableEl,
_thisRow = this.dataCache[pid],
_foldFlag = this.setting.foldFlag,
_foldClass = this.setting.foldClass,
children = _thisRow.children,
len = children.length,
cRow, cId, cEl,
display, aClass, rClass , isFold;
if(flag){
display = 'none';
aClass = _foldClass[0];
rClass = _foldClass[1];
isFold = _foldFlag[0];
}else{
display = 'table-row';
aClass = _foldClass[1];
rClass = _foldClass[0];
isFold = _foldFlag[1];
}
_thisRow.isFold = isFold;
_tableEl.find('tr[data-id=' + pid + ']').find('.fold-btn').removeClass(rClass).addClass(aClass);
while(len){
cRow = children[--len],
cId = cRow.id,
cEl = _tableEl.find('tr[data-id=' + cId + ']');
cEl.css('display', display);
if(cRow.isParent && flag && cRow.children.length){
this.fold.call(this, cId, true);
}
}
return this;
},
/*
*@description 设置根节点为可见(display:table-row),默认所有tr为不可见(display:none)
*@return {object} 对象本身
*/
showRoot : function(){
var _tableEl = this.tableEl,
_dataCache = this.dataCache,
row;
for(var key in _dataCache){
row = _dataCache[key];
if(row.layer === 0){
_tableEl.find('tr[data-id=' + row.id + ']').css('display', 'table-row');
}
}
return this;
},
/*
*@description 仅移除指定节点(不移除其子节点)
*@param {string} 指定节点的id
*@return {object} 对象本身
*/
removeSingle : function(id){
var _dataCache = this.dataCache,
_tr = _dataCache[id],
_parent = _dataCache[_tr.pid],
_sibling = _parent ? _parent.children : [];
this.tableEl.find('tr[data-id=' + id + ']').remove();
//delete from dataCache
delete _dataCache[id];
//delete from parent.children
for(var i = 0, len = _sibling.length, childId; i < len; i++){
childId = _sibling[i].id;
if(childId === id){
_sibling.splice(i, 1);
break;
}
}
if(!_sibling.length){
this.setNotParent(_tr.pid);
}
return this;
},
/*
*@description 移除指定节点(包括其子节点)
*@param {string} 指定节点的id
*@return {object} 对象本身
*/
remove : function(id){
var _dataCache = this.dataCache,
_tr = _dataCache[id],
_children = _tr.children;
this.removeSingle(id);
for(var i = 0, len = _children.length, child; i < len; i++){
child = _children[i];
this.remove.call(this, child.id);
}
return this;
}
};
/*
*@description 生成带checkbox的TableTree(继承于TableTree)
*@param {object} 生成树所需要的参数
*/
var CheckBoxTableTree = function(option){
TableTree.call(this, option);
this.setting.checkedFlag = ['t', 'f'];
};
CheckBoxTableTree.pt = CheckBoxTableTree.prototype = function(o){
function F(){}
F.prototype = o;
return new F();
}(TableTree.prototype);
CheckBoxTableTree.pt.constructor = CheckBoxTableTree;
/*
*@description 绑定相应事件
*@return {undefined}
*/
CheckBoxTableTree.pt.bindEvent = function(){
this.bindFoldClick();
this.bindCheckboxClick();
};
/*
*@description 通过data生成html字符串
*@param data {array} 加工后的数据,根据数据中的属性生成tr
*@return {string} 生成的html
*/
CheckBoxTableTree.pt.getTrHtml = function(data){
if(!data || data.constructor != Array){
return '';
}
var html = [],
_setting = this.setting,
paddingLeft = _setting.pdLeftBase * data[0].layer,
_thKey = this.thKey,
_foldClassTrue = _setting.foldClass[0],
_txtClass = _setting.txtClass;
for(var j = 0, len = data.length; j < len; j++){
var row = data[j],
cData = row.attr,
str = '<tr data-id="' + row.id + '"><td style="padding-left:'
+ paddingLeft + 'px;"><span class="fold-btn '
+ (row.isParent ? _foldClassTrue : '') + '"></span><input type="checkbox"/><div class="'
+ _txtClass + '">' + (cData.hasOwnProperty(_thKey[0]) ? cData[_thKey[0]] : row[_thKey[0]]) + '</div></td>';
for(var i = 1, cLen = _thKey.length; i < cLen; i++){
str += '<td>' + ((cData.hasOwnProperty(_thKey[i]) ? cData[_thKey[i]] : row[_thKey[i]]) || '') + '</td>';
}
str += '</tr>';
html.push(str);
if(row.children.length){
html.push(this.getTrHtml.call(this, row.children));
}
}
return html.join('');
};
/*
*@description 将数据加工后以id:object的形式存入this.dataCache
*@param data {array} 树形结构的原始数据
*@param n {number} data中第一层数据在所有数据中的层数
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.addDataCache = function(data, n){
var i = 0,
_dataCache = this.dataCache,
_setting = this.setting,
_data,
_doneFlag = _setting.doneFlag,
_foldFlag = _setting.foldFlag,
_checkedFlag = _setting.checkedFlag;
i = data.length;
while(i){
_data = data[--i];
var hasChildren = !!_data.children.length;
_data.isParent = _data.isParent || (this.ajaxURL || hasChildren ? true : false);
_data.isDone = _data.isParent ? (hasChildren ? _doneFlag[0] : _doneFlag[1]) : _doneFlag[2];
_data.isFold = _data.isParent ? _foldFlag[0] : _foldFlag[2];
_data.isChecked = _checkedFlag[1];
_data.layer = n;
_dataCache[_data.id] = _data;
if(hasChildren){
_data.isDone = _doneFlag[0];
this.addDataCache.call(this, _data.children, n + 1);
}
}
return this;
};
/*
*@description 绑定checkbox点击事件
*@return {undefined}
*/
CheckBoxTableTree.pt.bindCheckboxClick = function(){
var _this = this;
_this.tableEl.on('click', 'tbody input[type=checkbox]', function(){
var id = $(this).parent().parent().attr('data-id'),
flag = this.checked;
_this.select(id, flag);
});
};
/*
*@description 仅[取消]选中指定节点的checkbox(不包含其父/子节点)
*@param id {string} 指定节点的id
*@param flag {boolean} 指示选中还是取消 {true : 选中, false : 取消}
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.selectSingle = function(id, flag){
var _tableEl = this.tableEl,
_dataCache = this.dataCache,
_checkedFlag = this.setting.checkedFlag;
_dataCache[id].isChecked = _checkedFlag[flag ? 0 : 1];
_tableEl.find('tr[data-id=' + id + ']').find('input[type=checkbox]')[0].checked = flag;
return this;
};
/*
*@description [取消]选中所有节点的checkbox
*@param flag {boolean} 指示选中还是取消 {true : 选中, false : 取消}
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.selectAll = function(flag){
var _dataCache = this.dataCache,
_tableEl = this.tableEl,
_checkedFlag = this.setting.checkedFlag[flag ? 0 : 1];
for(key in _dataCache){
var row = _dataCache[key];
_tableEl.find('tr[data-id=' + row.id + ']').find('input[type=checkbox]')[0].checked = flag;
row.isChecked = _checkedFlag;
}
return this;
};
/*
*@description [取消]选中指定节点的checkbox(包含其父/子节点)
*@param id {string} 指定节点的id
*@param flag {boolean} 指示选中还是取消 {true : 选中, false : 取消}
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.select = function(id, flag){
this.selectParent(id, flag);
this.selectChildren(id, flag);
return this;
};
/*
*@description [取消]选中指定节点的父节点checkbox
*@param id {string} 指定节点的id
*@param flag {boolean} 指示选中还是取消 {true : 选中, false : 取消}
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.selectParent = function(id, flag){
var _dataCache = this.dataCache,
tr = _dataCache[id],
_parent = _dataCache[tr.pid],
_checkedFlagTrue = this.setting.checkedFlag[0],
_sibling;
this.selectSingle(tr.id, flag);
if(_parent){
if(!flag){
_sibling = _parent.children;
for(var i = 0, len = _sibling.length; i < len; i++){
if(_sibling[i].isChecked == _checkedFlagTrue){
return this;
}
}
}
this.selectParent.call(this, _parent.id, flag);
}
return this;
};
/*
*@description [取消]选中指定节点的子节点checkbox
*@param id {string} 指定节点的id
*@param flag {boolean} 指示选中还是取消 {true : 选中, false : 取消}
*@return {object} 对象本身
*/
CheckBoxTableTree.pt.selectChildren = function(id, flag){
var _dataCache = this.dataCache,
tr = _dataCache[id],
_children = tr.children;
for(var i = 0, len = _children.length, row; i < len; i++){
row = _children[i];
this.selectSingle(row.id, flag);
row.children.length && this.selectChildren.call(this, row.id, flag);
}
return this;
};
/*
*@description 获取选中节点的id数组
*@param sign {number} 指示只取叶子节点还是全部节点{0/undefined : 全部节点, 1 : 仅叶子节点}
*@return {array} 选中节点的数组
*/
CheckBoxTableTree.pt.getSelected = function(sign){
var sign = sign || 0,
idArray = [],
_dataCache = this.dataCache,
_checkedFlagTrue = this.setting.checkedFlag[0],
row;
for(key in _dataCache){
row = _dataCache[key];
if(sign === 1 && row.children.length){
continue;
}
row.isChecked === _checkedFlagTrue
&& idArray.push(key);
}
return idArray;
};