-
Notifications
You must be signed in to change notification settings - Fork 13
/
treeGrid.js
2680 lines (2620 loc) · 106 KB
/
treeGrid.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@Name:treeGrid树状表格
@Author:yuanzhao
*/
layui.config({
}).extend({
}).define(['laytpl', 'laypage', 'layer', 'form'], function(exports){
"use strict";
var $ = layui.$
,laytpl = layui.laytpl
,laypage = layui.laypage
,layer = layui.layer
,form = layui.form
,hint = layui.hint()
,device = layui.device()
//外部接口
,table = {
config: {//全局配置项,表格级别
indexName: 'lay_table_index' //下标索引名
,cols:{//节点级别的附加字段
isCheckName: 'lay_is_checked' //选中状态(true,false)
,isRadio:'lay_is_radio'//单选状态(true,false)
,isOpen:'lay_is_open'//是否展开节点
,isShow:'lay_is_show'//是否显示节点
,level:'lay_level'//节点的层级关系(不需要设置)
,children:'children'//存放下级的变量
,cheDisabled:'lay_che_disabled'//禁止多选(true,false)
,radDisabled:'lay_rad_disabled'//禁止单选(true,false)
,iconOpen:'lay_icon_open'//打开的图标
,iconClose:'lay_icon_close'//关闭的图标
,icon:'lay_icon'//叶子节点图标
}
,initWidth:{//默认列宽度定义
checkbox: 48
,space: 15
,numbers: 40
,radio:48
}
}
/**
* 缓存数据
*
* 结构图示
* cache{} 缓存(对象)
* key['data']{} 全部数据缓存(对象)
* key[list][] 列表数据对象(数组)
* key[map]{} 列表数据Map对象(Map)
* key[treeList][] 树状结构的对象(数组)
* key['cla']{} 全部已初始化过的Calss对象类(注意渲染是异步执行)
* key['claIds'][] 全部已经吊用过初始化方法的表格类
* key[claObjs]{key[tableId]} 全部已经初始化好的cla对象
*
*/
,cache: {
tableId:{
data:{
list:[]//列表数据
,map:{}//列表数据以idField或唯一值作为key的Map数据
,treeList:[]//树状数据
,upIds:[]//父节点集合 在一次外部请求前严格按照第一次的顺序
}
}
,cla:{
claIds:{
tableId:true
}
,claObjs:{
tableId:{}
}
}
,selectcode:{//数据字典缓存
demokey:[
{
key:{value:''}
}
]
}
}
,index: layui.table ? (layui.table.index + 10000) : 0
/**
* 设置全局项
* @param options
* @return {table}
*/
,set: function(options){
var that = this;
that.config = $.extend({}, that.config, options);
return that;
}
/**
* 事件监听
* @param events
* @param callback
* @return {*}
*/
,on: function(events, callback){
return layui.onevent.call(this, MOD_NAME, events, callback);
}
,getClass:function (tableId) {
return table.cache.cla.claObjs[tableId];;
}
,pushClass:function (tableId,that) {
table.cache.cla.claObjs[tableId]=that;
}
,isCalss:function (tableId) {
var ids=this.cache.cla.claIds||{};
return ids.hasOwnProperty(tableId)||false;
}
,isClassYes:function (tableId) {
var ids=this.cache.cla.claIds||{};
return ids[tableId]||false;
}
,pushClassIds:function (tableId,is) {
this.cache.cla.claIds[tableId]=is;
}
,setObj:function (tableId,key,o) {
if(!this.obj[tableId])this.obj[tableId]={};
this.obj[tableId][key]=o;
}
,getObj:function (tableId, key) {
return this.obj[tableId]?this.obj[tableId][key]:null;
}
/**
* 获取列表数据
*/
,getDataList:function (tableId) {
if(table.cache[tableId]){
return table.cache[tableId].data.list;
}
return [];
}
/**
* 设置列表数据
*/
,setDataList:function (tableId, list) {
if(!table.cache[tableId])table.cache[tableId]={};
if(!table.cache[tableId].data)table.cache[tableId].data={};
if(!table.cache[tableId].data.list)table.cache[tableId].data.list=[];
table.cache[tableId].data.list=list;
}
/**
* 获取列表数据
*/
,getDataMap:function (tableId) {
if(table.cache[tableId]){
return table.cache[tableId].data.map;
}
return {};
}
/**
* 设置列表数据
*/
,setDataMap:function (tableId, map) {
if(!table.cache[tableId])table.cache[tableId]={};
if(!table.cache[tableId].data)table.cache[tableId].data={};
if(!table.cache[tableId].data.map)table.cache[tableId].data.map={};
table.cache[tableId].data.map=map;
}
/**
* 获取树状数据
*/
,getDataTreeList:function (tableId) {
if(table.cache[tableId]){
return table.cache[tableId].data.treeList;
}
return [];
}
/**
* 设置树状数据
*/
,setDataTreeList:function (tableId, treeList) {
if(!table.cache[tableId])table.cache[tableId]={};
if(!table.cache[tableId].data)table.cache[tableId].data={};
if(!table.cache[tableId].data.treeList)table.cache[tableId].data.treeList={};
table.cache[tableId].data.treeList=treeList;
}
/**
* 获取根节点数据
*/
,getDataRootList:function (tableId) {
if(table.cache[tableId]){
return table.cache[tableId].data.upIds||[];
}
return [];
}
/**
* 设置根节点数据
*/
,setDataRootList:function (tableId, rootList) {
if(!table.cache[tableId])table.cache[tableId]={};
if(!table.cache[tableId].data)table.cache[tableId].data={};
if(!table.cache[tableId].data.upIds)table.cache[tableId].data.upIds=[];
table.cache[tableId].data.upIds=rootList;
}
/**
* 初始化
* @param filter
* @param settings
* @return {table}
*/
,init:function (filter, settings) {
settings = settings || {};
var that = this
,elemTable = filter ? $('table[lay-filter="'+ filter +'"]') : $(ELEM + '[lay-data]')
,errorTips = 'Table element property lay-data configuration item has a syntax error: ';
//遍历数据表格
elemTable.each(function(){
var othis = $(this), tableData = othis.attr('lay-data');
try{
tableData = new Function('return '+ tableData)();
} catch(e){
hint.error(errorTips + tableData)
}
var cols = [], options = $.extend({
elem: this
,cols: []
,data: []
,skin: othis.attr('lay-skin') //风格
,size: othis.attr('lay-size') //尺寸
,even: typeof othis.attr('lay-even') === 'string' //偶数行背景
}, table.config, settings, tableData);
filter && othis.hide();
//获取表头数据
othis.find('thead>tr').each(function(i){
options.cols[i] = [];
$(this).children().each(function(ii){
var th = $(this), itemData = th.attr('lay-data');
try{
itemData = new Function('return '+ itemData)();
} catch(e){
return hint.error(errorTips + itemData)
}
var row = $.extend({
title: th.text()
,colspan: th.attr('colspan') || 0 //列单元格
,rowspan: th.attr('rowspan') || 0 //行单元格
}, itemData);
if(row.colspan < 2) cols.push(row);
options.cols[i].push(row);
});
});
//获取表体数据
othis.find('tbody>tr').each(function(i1){
var tr = $(this), row = {};
//如果定义了字段名
tr.children('td').each(function(i2, item2){
var td = $(this)
,field = td.data('field');
if(field){
return row[field] = td.html();
}
});
//如果未定义字段名
layui.each(cols, function(i3, item3){
var td = tr.children('td').eq(i3);
row[item3.field] = td.html();
});
options.data[i1] = row;
});
table.render(options);
});
return that;
}
/**
* 渲染入口方法(核心入口)
*/
,render:function (options) {
table.pushClassIds(options.id);
var inst = new Class(options);
return thisTable.call(inst);
}
/**
* 对应的表格加载完成后执行
* @param tableId
* @param fn
*/
,ready:function (tableId,fn) {
var is=false;
var myDate=new Date();
function isReady() {
if(tableId){
var that=table.getClass(tableId);
if(that&&that.hasOwnProperty('layBody')){
fn(that);
is=true;
}else{
var myDate2=new Date();
var i=myDate2.getTime()-myDate.getTime();
if(i<=(1000*10)&&!is){//大于10秒退出
setTimeout(isReady,50);
}
}
}
}
if(tableId&&fn){
setTimeout(isReady,50);
}
}
/**
* 获取表格选中记录
* @param tableId
* @return {{data: Array, isAll: boolean}}
*/
,checkStatus:function (tableId) {
var nums = 0
,invalidNum = 0
,arr = []
,data = table.getDataList(tableId) || [];
//计算全选个数
layui.each(data, function(i, item){
if(item.constructor === Array){
invalidNum++; //无效数据,或已删除的
return;
}
if(item[table.config.cols.isCheckName]){
nums++;
arr.push(table.clearCacheKey(item));
}
});
return {
data: arr //选中的数据
,isAll: data.length ? (nums === (data.length - invalidNum)) : false //是否全选
};
}
/**
* 设置表格复选状态
* @param tableId
* @param value 此值存在时为设置操作
* @returns {*}
*/
,setCheckStatus:function(tableId, fildName, ids){
var retObj=null;
var that=table.getClass(tableId)
,invalidNum = 0
,arr = []
,data = table.getDataList(tableId) || []
,childs = that.layBody.find('input[name="layTableCheckbox"]')//复选框
;
if(fildName&&ids){//设置选中
var idsarr=ids.split(',');
idsarr.forEach(function (o) {
var temo=null;
data.forEach(function (e) {
var b1=e[fildName]+"";
var b2=o+"";
if(b1==b2){
temo=e;
return;
};
});
if(temo){
var v=temo[table.config.indexName];
that.layBody.find('input[name="layTableCheckbox"][value="'+v+'"]').prop("checked",true);
that.setCheckData(v, true);
}
});
that.syncCheckAll();
that.renderForm('checkbox');
}
return retObj;
}
/**
* 表格单选状态
* @param tableId
* @param value 此值存在时为设置操作
* @returns {*}
*/
,radioStatus:function (tableId) {
var retObj=null;
var nums = 0
,invalidNum = 0
,arr = []
,data = table.getDataList(tableId) || [];
var v=$("input[name='"+TABLE_RADIO_ID+tableId+"']:checked").val();
v=parseInt(v);
data.forEach(function (e) {
if(e[table.config.indexName]==v){
retObj=e;
};
});
return table.clearCacheKey(retObj);
}
/**
* 设置表格单选状态
* @param tableId
* @param value 此值存在时为设置操作
* @returns {*}
*/
,setRadioStatus:function (tableId,fildName,value) {
var retObj=null;
var nums = 0
,invalidNum = 0
,arr = []
,data = table.getDataList(tableId) || [];
if(fildName&&value){//设置选中
data.forEach(function (e) {
var b1=e[fildName]+"";
var b2=value+"";
if(b1==b2){
retObj=e;
return;
};
});
if(retObj){
var v=retObj[table.config.indexName];
$("input:radio[name='"+TABLE_RADIO_ID+tableId+"'][value='"+v+"']").prop("checked",true);
form.render('radio');
}
}
return retObj;
}
/**
* 清除临时Key
* @param data
* @return {*}
*/
,clearCacheKey:function (data) {
data = $.extend({}, data);
delete data[table.config.cols.isCheckName];
delete data[table.config.indexName];
return data;
}
/**
* 刷新数据
* @param id
* @param options
* @return {*}
*/
,query:function (tableId, options) {
var that= table.getClass(tableId);
that.renderTdCss();
if(that.config.data && that.config.data.constructor === Array) delete that.config.data;
that.config = $.extend({}, that.config, options);
that.pullData(that.page, that.loading());
}
/**
* 此方法为整体重新渲染(重量级刷新方法)
* @param id
* @param options
*/
,reload:function (tableId, options) {
var config = thisTable.config[tableId];
options = options || {};
if(!config) return hint.error('The ID option was not found in the table instance');
if(options.data && options.data.constructor === Array) delete config.data;
config.cols=options.cols;
return table.render($.extend(true, {}, config, options));
}
/**
* 添加一行或多行数据
* @param tableId 表格id
* @param index 在第几个位置插入(从0开始)
* @param data 数据
* @returns {*}
*/
,addRow:function (tableId, index, data) {
var that=table.getClass(tableId)
,options=that.config
,uo = []//父级节点
,treeList=table.getDataTreeList(tableId)
,list = table.getDataList(tableId) || [];
that.resetData(data);
//插入到父节点后面
list.splice(index,0,data);//更新缓存
table.kit.restNumbers(list);//重置下标
table.setDataMap(tableId,that.resetDataMap(list));//处理map
if(options.isTree){//处理层级
//1、处理父级 2、处理treeObj 3、层级
var uo=that.treeFindUpData(data);
if(uo) {
var clist=uo.children;
uo.children.push(data);
data[table.config.cols.level]=uo[table.config.cols.level]+1;
}else{
data[table.config.cols.level]=1;
treeList.push(data);
}
}
//生成html
var tds=that.renderTr(data,data[table.config.indexName]);
var trs='<tr data-index="'+ data[table.config.indexName] +'"'+that.renderTrUpids(data)+'>'+ tds.join('') + '</tr>';
if(index==0){//在第一个位置插入
var tbody=that.layBody.find('table tbody');
$(tbody).prepend(trs);
that.layBody.find(".layui-none").remove();
}else{
var o=that.layBody.find('[data-index='+(index-1)+']');//父节点dom树
$(o).after(trs);
}
that.renderForm();
if(options.isPage)that.renderPage(that.config.page.count+1);//分页渲染
that.restNumbers();
that.events();
if(options.isTree) {//展开节点
that.treeNodeOpen(uo, true);
that.renderTreeConvertShowName(uo);
}
}
/**
* 删除一行或多行数据
* (如果是树状则删除自己和子节点)
* @param tableId
* @param data(1、数组;2、对象)
*/
,delRow:function (tableId, data) {
//1、页面清除 2、缓存清除
var that=table.getClass(tableId)
,options=that.config
,list=table.getDataList(tableId);
var sonList=[];//需要删除的数据
var delIds={};//需要删除的数据map
var delDatas=[];
var upDelDatas=[];//全部待删除节点的父节点(处理折叠)
if(!that||!data)return;
if(table.kit.isArray(data)){//是数组,删除多个
delDatas=data;
}else{
delDatas[0]=data;
}
delDatas.forEach(function(temo) {//记录全部父节点
var uo=that.treeFindUpData(temo);
if(uo){
upDelDatas.push(uo);
}
});
sonList=options.isTree?table.treeFindSonList(that.config.id,delDatas):delDatas;
//页面元素处理
sonList.forEach(function (temo) {
var index=temo[table.config.indexName];
delIds[index]=index;//设置代删除的id集合
var tr = that.layBody.find('tr[data-index="'+ index +'"]');
tr.remove();
});
//数据处理
that.restNumbers();
var newList=[];//重构一个新的数组
for (var i=0,len=list.length;i<len;i++) {
var isP=true;
var temo1=null;
sonList.forEach(function (temo) {
if (temo[table.config.indexName] === list[i][table.config.indexName]) {
isP = false;
}
});
if(isP){
newList.push(list[i]);
}
}
table.kit.restNumbers(newList);
//that.resetDatas(newList);//更新缓存数据
//处理父级节点
upDelDatas.forEach(function(temo) {
that.renderTreeConvertShowName(temo);
});
if(options.isPage)that.renderPage(that.config.page.count-Object.keys(delIds).length);//分页渲染
//处理list
table.setDataList(tableId,newList);
//处理map
table.setDataMap(tableId,that.resetDataMap(newList));
//处理树结构
table.setDataTreeList(tableId,that.resetDataTreeList(newList,table.getDataRootList(tableId)));
that.events();
}
/**
* 更新指定的记录
* @param obj
* @param index
*/
,updateRow:function (tableId,obj) {
var that=table.getClass(tableId);
if(!that||!obj)return;
var id=obj[that.config.idField];
//更新缓存数据
var maps=table.getDataMap(tableId);
var thisobj=maps[id];
if(thisobj){
$.extend(thisobj, obj);
}else{
return;
}
//更新页面
var oi=thisobj[table.config.indexName];
var tds=that.renderTr(thisobj,oi);
var tr=that.layBody.find('tr[data-index='+oi+']');
$(tr).html(tds);
}
,treeNodeOpen:function (tableId,o, isOpen) {
var that=table.getClass(tableId);
if(!that||!o)return;
that.treeNodeOpen(o,isOpen);
}
/**
* 折叠或展开全部(默认为展开全部节点)
* @param tableId
* @param isOpen 展开还是折叠(默认值为true 展开)
*/
,treeOpenAll:function (tableId,isOpen) {
var that=table.getClass(tableId);
if(!that)return;
if(isOpen===undefined){isOpen=true;}
var list=table.getDataList(tableId);
if(!list)return;
list.forEach(function (temo) {
that.treeNodeOpen(temo,isOpen);
});
}
/**
* 获取全部需要子节点对象集合
* @param data(数组或对象)
*/
,treeFindSonList:function (tableId,data) {
var that=table.getClass(tableId);
if(!that||!data)return [];
var delDatas=[];
var sonList=[];//需要删除的数据
var delIds={};//需要删除的数据map
if(table.kit.isArray(data)){//是数组,删除多个
delDatas=data;
}else{
delDatas[0]=data;
}
delDatas.forEach(function (temo) {
if(temo.children.length>0){
var temSonList=that.treeFindSonData(temo);
temSonList.forEach(function (temii) {
if(!delIds[temii[table.config.indexName]]){
sonList.push(temii);
delIds[temii[table.config.indexName]]=temii[table.config.indexName];
}
});
}
sonList.push(temo);
delIds[temo[table.config.indexName]]=temo[table.config.indexName];
});
return sonList;
}
,treeFindUpDatas:function (tableId, o) {
var that=table.getClass(tableId);
if(!that||!o)return [];
return that.treeFindUpDatas(o);
}
,treeFindUpData:function (tableId, o) {
var that=table.getClass(tableId);
if(!that||!o)return [];
return that.treeFindUpData(o);
}
/**
* 获取全部需要子节点id集合
* @param data(数组或对象)
*/
,treeFindSonIds:function (tableId,data) {
var delIds=[];
var sonList=table.treeFindSonList(tableId,data);
sonList.forEach(function (temo) {
delIds.push([table.config.indexName]);
});
return delIds;
}
/**
* 获取全部的id字段集合
* @param tableId
* @param data
* @returns {Array}
*/
,treeFindSonIdFields:function (tableId,data) {
var idField=[];
var that=table.getClass(tableId);
var sonList=table.treeFindSonList(tableId,data);
sonList.forEach(function (temo) {
idField.push(temo[that.config.idField]);
});
return idField;
}
,treeIconRender:function (tableId, o) {
var that=table.getClass(tableId);
if(!that||!o)return [];
return that.treeIconRender(o,false);
}
/**
* 工具方法对象
*/
,kit:{
isArray:function (o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
,isNumber:function (val){
var regPos = /^\d+(\.\d+)?$/; //非负浮点数
var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
if(regPos.test(val) || regNeg.test(val)){
return true;
}else{
return false;
}
}
,restNumbers:function (list) {
if(!list)return;
var i=0;
list.forEach(function (o) {
o[table.config.indexName]=i;
i++;
});
}
}
}
//操作当前实例
,thisTable = function(){
var that = this
,options = that.config
,id = options.id;
id && (thisTable.config[id] = options);
return {
reload: function(options){
that.reload.call(that, options);
}
,config: options
}
}
//字符常量
,MOD_NAME = 'treeGrid', ELEM = '.layui-table', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled', NONE = 'layui-none'
,ELEM_VIEW = 'layui-table-view', ELEM_HEADER = '.layui-table-header', ELEM_BODY = '.layui-table-body', ELEM_MAIN = '.layui-table-main', ELEM_FIXED = '.layui-table-fixed', ELEM_FIXL = '.layui-table-fixed-l', ELEM_FIXR = '.layui-table-fixed-r', ELEM_TOOL = '.layui-table-tool', ELEM_PAGE = '.layui-table-page', ELEM_SORT = '.layui-table-sort', ELEM_EDIT = 'layui-table-edit', ELEM_HOVER = 'layui-table-hover'
,TABLE_RADIO_ID='table_radio_'
,ELEM_FILTER='.layui-table-filter'
,TREE_ID='treeId',TREE_UPID='treeUpId',TREE_SHOW_NAME='treeShowName',TREE_KEY_MAP='tree_key_map'
//thead区域模板
,TPL_HEADER = function(options){
var rowCols = '{{#if(item2.colspan){}} colspan="{{item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{item2.rowspan}}"{{#}}}';
options = options || {};
return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
,'<thead>'
,'{{# layui.each(d.data.cols, function(i1, item1){ }}'
,'<tr>'
,'{{# layui.each(item1, function(i2, item2){ }}'
,'{{# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}'
,'{{# if(item2.fixed === "right"){ right = true; } }}'
,function(){
if(options.fixed && options.fixed !== 'right'){
return '{{# if(item2.fixed && item2.fixed !== "right"){ }}';
}
if(options.fixed === 'right'){
return '{{# if(item2.fixed === "right"){ }}';
}
return '';
}()
,'<th data-field="{{ item2.field||i2 }}" {{# if(item2.minWidth){ }}data-minwidth="{{item2.minWidth}}"{{# } }} '+ rowCols +' {{# if(item2.unresize){ }}data-unresize="true"{{# } }}>'
,'<div class="layui-table-cell laytable-cell-'
,'{{# if(item2.colspan > 1){ }}'
,'group'
,'{{# } else { }}'
,'{{d.index}}-{{item2.field || i2}}'
,'{{# if(item2.type !== "normal"){ }}'
,' laytable-cell-{{ item2.type }}'
,'{{# } }}'
,'{{# } }}'
,'" {{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
,'{{# if(item2.type === "checkbox"){ }}' //复选框
/*,'<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose">'*/
,'{{# } else { }}'
,'<span>{{item2.title||""}}</span>'
,'{{# if(!(item2.colspan > 1) && item2.sort){ }}'
,'<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc"></i><i class="layui-edge layui-table-sort-desc"></i></span>'
,'{{# } }}'
,'{{# } }}'
,'</div>'
,'</th>'
,(options.fixed ? '{{# }; }}' : '')
,'{{# }); }}'
,'</tr>'
,'{{# }); }}'
,'</thead>'
,'</table>'].join('');
}
/**
* 行内过滤区域
*/
,TPL_FILTER = function(options){
}
//tbody区域模板
,TPL_BODY = ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
,'<tbody></tbody>'
,'</table>'].join('')
//主模板
,TPL_MAIN = ['<div class="layui-form layui-border-box {{d.VIEW_CLASS}}" lay-filter="LAY-table-{{d.index}}" style="{{# if(d.data.width){ }}width:{{d.data.width}}px;{{# } }} {{# if(d.data.height){ }}height:{{d.data.height}}px;{{# } }}">'
,'{{# if(d.data.toolbar){ }}'
,'<div class="layui-table-tool"></div>'
,'{{# } }}'
,'<div class="layui-table-box">'
,'{{# var left, right; }}'
,'<div class="layui-table-header">'
,TPL_HEADER()
,'</div>'
,'<div class="layui-table-filter">'
,TPL_FILTER()
,'</div>'
,'<div class="layui-table-body layui-table-main">'
,TPL_BODY
,'</div>'
,'{{# if(left){ }}'
,'<div class="layui-table-fixed layui-table-fixed-l">'
,'<div class="layui-table-header">'
,TPL_HEADER({fixed: true})
,'</div>'
,'<div class="layui-table-body">'
,TPL_BODY
,'</div>'
,'</div>'
,'{{# }; }}'
,'{{# if(right){ }}'
,'<div class="layui-table-fixed layui-table-fixed-r">'
,'<div class="layui-table-header">'
,TPL_HEADER({fixed: 'right'})
,'<div class="layui-table-mend"></div>'
,'</div>'
,'<div class="layui-table-body">'
,TPL_BODY
,'</div>'
,'</div>'
,'{{# }; }}'
,'</div>'
,'{{# if(d.data.isPage){ }}'
,'<div class="layui-table-page">'
,'<div id="layui-table-page{{d.index}}"></div>'
,'</div>'
,'{{# } }}'
/*,'<style>'
,'{{# layui.each(d.data.cols, function(i1, item1){'
,'layui.each(item1, function(i2, item2){ }}'
,'.laytable-cell-{{d.index}}-{{item2.field||i2}}{ '
,'{{# if(item2.width){ }}'
,'width: {{item2.width}}px;'
,'{{# } }}'
,' }'
,'{{# });'
,'}); }}'
,'</style>'*/
,'</div>'].join('')
,_WIN = $(window)
,_DOC = $(document)
//构造器
,Class = function(options){
var that = this;
that.index = ++table.index;
that.config = $.extend({}, that.config, table.config, options);
that.configFirst = $.extend({}, that.config, table.config, options);
that.render();
table.pushClass(options.id,that);
};
/**
* 表格行为属性(默认配置)
*/
Class.prototype.config = {
limit: 10 //每页显示的数量
,loading: true //请求数据时,是否显示loading
,cellMinWidth: 60 //所有单元格默认最小宽度
,heightRemove:[]//非固定高度情况下,不参与计算的高度
,text: {
none: '无数据'
}
,isFilter:false//是否开启行内过滤
,method:'post'//默认以post方式请求后台
,radDisabledNum:0//禁止单选的记录数
,cheDisabledNum:0//禁止多选的记录数
//树相关图表
,branch: ["",""] //父节点
,leaf: '' //叶节点
,iconOpen:true//默认开启树节点图标
,isOpenDefault:true//默认展开还是折叠节点
,parseData:null//加载数据后的回调方法
,onClickRow:null//行单击事件
,onDblClickRow:null//行双击事件
,onBeforeCheck:null//复选前事件
,isTree:true//默认为树表格
,isPage:false//不分页
,height:'100%'//默认高度100%
};
Class.prototype.configFirst={};//页面定义时原始参数
//表格渲染
Class.prototype.render = function(){
var that = this
,options = that.config;
options.elem = $(options.elem);
options.where = options.where || {};
options.id = options.id || options.elem.attr('id');
that.test();
//请求参数的自定义格式
options.request = $.extend({
pageName: 'page'
,limitName: 'limit'
}, options.request)
//响应数据的自定义格式
options.response = $.extend({
statusName: 'code'
,statusCode: 0
,msgName: 'msg'
,dataName: 'data'
,countName: 'count'
}, options.response);
//如果 page 传入 laypage 对象
if(typeof options.page === 'object'){
options.limit = options.page.limit || options.limit;
options.limits = options.page.limits || options.limits;
that.page = options.page.curr = options.page.curr || 1;
delete options.page.elem;
delete options.page.jump;
}
if(!options.elem[0]) return that;
that.columnWidthInit();//列宽度计算
//开始插入替代元素
var othis = options.elem
,hasRender = othis.next('.' + ELEM_VIEW)
//主容器
,reElem = that.elem = $(laytpl(TPL_MAIN).render({
VIEW_CLASS: ELEM_VIEW
,data: options
,index: that.index //索引
}));
options.index = that.index;
//生成替代元素
hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
othis.after(reElem);
that.renderTdCss();
//各级容器
that.layHeader = reElem.find(ELEM_HEADER); //表头
that.layMain = reElem.find(ELEM_MAIN);//内容区域
that.layBody = reElem.find(ELEM_BODY);//内容区域
that.layFixed = reElem.find(ELEM_FIXED);//浮动区域
that.layFixLeft = reElem.find(ELEM_FIXL);//左浮动
that.layFixRight = reElem.find(ELEM_FIXR);//有浮动
that.layTool = reElem.find(ELEM_TOOL);//工具栏区域
that.layPage = reElem.find(ELEM_PAGE);//分页区域
that.layFilter=reElem.find(ELEM_FILTER);//行内过滤条件区域
that.layTool.html(
laytpl($(options.toolbar).html()||'').render(options)
);
if(options.height){
that.tableHeight();//表格高度计算
that.resizeHeight();//高度控制
that.renderCss();
}
//如果多级表头,则填补表头高度
if(options.cols.length > 1){
var th = that.layFixed.find(ELEM_HEADER).find('th');
th.height(that.layHeader.height() - 1 - parseFloat(th.css('padding-top')) - parseFloat(th.css('padding-bottom')));
}
//渲染过滤区域
if(options.isFilter){
that.layFilter.html(
that.renderFilter()
);
}
that.layHeader.find(".layui-table thead tr").append('<th data-field="_swth" style="display: none"><div class="layui-table-cell" align="center"></div></th>');
//请求数据
that.pullData(that.page,that.loading());
that.test();
};
//根据列类型,定制化参数
Class.prototype.initOpts = function(item){
var that = this,
options = that.config;
//让 type 参数兼容旧版本
if(item.checkbox) item.type = "checkbox";
if(item.space) item.type = "space";
if(!item.type) item.type = "normal";
if(item.type !== "normal"){
item.unresize = true;
item.width = item.width || table.config.initWidth[item.type];
}
if(options.isFilter){//开启行内过滤
if(item.isFilter!=false){
item.isFilter=true;
}
}
};
/**
* 表格获取父容器高度