-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
table.js
3163 lines (2741 loc) · 102 KB
/
table.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
/**
* layui.table
* 表格组件
*/
layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
"use strict";
var $ = layui.$;
var lay = layui.lay;
var laytpl = layui.laytpl;
var laypage = layui.laypage;
var layer = layui.layer;
var form = layui.form;
var util = layui.util;
var hint = layui.hint();
var device = layui.device();
// api
var table = {
config: { // 全局配置项
checkName: 'LAY_CHECKED', // 是否选中状态的特定字段名
indexName: 'LAY_INDEX', // 初始下标索引名,用于恢复当前页表格排序
numbersName: 'LAY_NUM', // 序号
disabledName: 'LAY_DISABLED' // 禁用状态的特定字段名
},
cache: {}, // 数据缓存
index: layui.table ? (layui.table.index + 10000) : 0,
// 设置全局项
set: function(options){
var that = this;
that.config = $.extend({}, that.config, options);
return that;
},
// 事件
on: function(events, callback){
return layui.onevent.call(this, MOD_NAME, events, callback);
}
};
// 操作当前实例
var thisTable = function(){
var that = this;
var options = that.config;
var id = options.id || options.index;
return {
config: options,
reload: function(options, deep){
that.reload.call(that, options, deep);
},
reloadData: function(options, deep){
table.reloadData(id, options, deep);
},
setColsWidth: function(){
that.setColsWidth.call(that);
},
resize: function(){ // 重置表格尺寸/结构
that.resize.call(that);
}
}
};
// 获取当前实例
var getThisTable = function(id){
var that = thisTable.that[id];
if(!that) hint.error(id ? ('The table instance with ID \''+ id +'\' not found') : 'ID argument required');
return that || null;
};
// 获取当前实例配置项
var getThisTableConfig = function(id){
var config = thisTable.config[id];
if(!config) hint.error(id ? ('The table instance with ID \''+ id +'\' not found') : 'ID argument required');
return config || null;
};
// lay 函数可以处理 Selector,HTMLElement,JQuery 类型
// 无效的 CSS 选择器字符串,会抛出 SyntaxError 异常,此时直接返回 laytpl 模板字符串
var resolveTplStr = function(templet){
try{
return lay(templet).html();
}catch(err){
return templet;
}
}
// 解析自定义模板数据
var parseTempData = function(obj){
obj = obj || {};
var options = this.config || {};
var item3 = obj.item3; // 表头数据
var content = obj.content; // 原始内容
if (item3.type === 'numbers') content = obj.tplData[table.config.numbersName];
// 是否编码 HTML
var escaped = 'escape' in item3 ? item3.escape : options.escape;
if(escaped) content = util.escape(content);
// 获取模板
var templet = obj.text && item3.exportTemplet || (item3.templet || item3.toolbar);
// 获取模板内容
if(templet){
content = typeof templet === 'function'
? templet.call(item3, obj.tplData, obj.obj)
: laytpl(resolveTplStr(templet) || String(content)).render($.extend({
LAY_COL: item3
}, obj.tplData));
}
// 是否只返回文本
return obj.text ? $('<div>'+ content +'</div>').text() : content;
};
// 字符
var MOD_NAME = 'table';
var ELEM = '.layui-table';
var THIS = 'layui-this';
var SHOW = 'layui-show';
var HIDE = 'layui-hide';
var HIDE_V = 'layui-hide-v';
var DISABLED = 'layui-disabled';
var NONE = 'layui-none';
var ELEM_VIEW = 'layui-table-view';
var ELEM_TOOL = '.layui-table-tool';
var ELEM_BOX = '.layui-table-box';
var ELEM_INIT = '.layui-table-init';
var ELEM_HEADER = '.layui-table-header';
var ELEM_BODY = '.layui-table-body';
var ELEM_MAIN = '.layui-table-main';
var ELEM_FIXED = '.layui-table-fixed';
var ELEM_FIXL = '.layui-table-fixed-l';
var ELEM_FIXR = '.layui-table-fixed-r';
var ELEM_TOTAL = '.layui-table-total';
var ELEM_PAGE = '.layui-table-page';
var ELEM_PAGE_VIEW = '.layui-table-pageview';
var ELEM_SORT = '.layui-table-sort';
var ELEM_CHECKED = 'layui-table-checked';
var ELEM_EDIT = 'layui-table-edit';
var ELEM_HOVER = 'layui-table-hover';
var ELEM_GROUP = 'laytable-cell-group';
var ELEM_COL_SPECIAL = 'layui-table-col-special';
var ELEM_TOOL_PANEL = 'layui-table-tool-panel';
var ELEM_EXPAND = 'layui-table-expanded'
var DATA_MOVE_NAME = 'LAY_TABLE_MOVE_DICT';
// thead 区域模板
var TPL_HEADER = function(options){
var rowCols = '{{#var colspan = layui.type(item2.colspan2) === \'number\' ? item2.colspan2 : item2.colspan; if(colspan){}} colspan="{{=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 '';
}()
,'{{# var isSort = !(item2.colGroup) && item2.sort; }}'
,'<th data-field="{{= item2.field||i2 }}" data-key="{{=d.index}}-{{=i1}}-{{=i2}}" {{# if( item2.parentKey){ }}data-parentkey="{{= item2.parentKey }}"{{# } }} {{# if(item2.minWidth){ }}data-minwidth="{{=item2.minWidth}}"{{# } }} {{# if(item2.maxWidth){ }}data-maxwidth="{{=item2.maxWidth}}"{{# } }} {{# if(item2.style){ }}style="{{=item2.style}}"{{# } }} '+ rowCols +' {{# if(item2.unresize || item2.colGroup){ }}data-unresize="true"{{# } }} class="{{# if(item2.hide){ }}layui-hide{{# } }}{{# if(isSort){ }} layui-unselect{{# } }}{{# if(!item2.field){ }} layui-table-col-special{{# } }}"{{# if(item2.title){ }} title="{{ layui.$(\'<div>\' + item2.title + \'</div>\').text() }}"{{# } }}>'
,'<div class="layui-table-cell laytable-cell-'
,'{{# if(item2.colGroup){ }}'
,'group'
,'{{# } else { }}'
,'{{=d.index}}-{{=i1}}-{{=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" {{# if(item2[d.data.checkName]){ }}checked{{# }; }}>'
,'{{# } else { }}'
,'<span>{{-item2.title||""}}</span>'
,'{{# if(isSort){ }}'
,'<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc" title="升序"></i><i class="layui-edge layui-table-sort-desc" title="降序"></i></span>'
,'{{# } }}'
,'{{# } }}'
,'</div>'
,'</th>'
,(options.fixed ? '{{# }; }}' : '')
,'{{# }); }}'
,'</tr>'
,'{{# }); }}'
,'</thead>'
,'</table>'].join('');
};
// tbody 区域模板
var 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('');
// 主模板
var TPL_MAIN = [
,'{{# if(d.data.toolbar){ }}'
,'<div class="layui-table-tool">'
,'<div class="layui-table-tool-temp"></div>'
,'<div class="layui-table-tool-self"></div>'
,'</div>'
,'{{# } }}'
,'<div class="layui-table-box">'
,'{{# if(d.data.loading){ }}'
,'<div class="layui-table-init" style="background-color: #fff;">'
,'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>'
,'</div>'
,'{{# } }}'
,'{{# var left, right; }}'
,'<div class="layui-table-header">'
,TPL_HEADER()
,'</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 layui-hide">'
,'<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.totalRow){ }}'
,'<div class="layui-table-total">'
,'<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><tr><td><div class="layui-table-cell" style="visibility: hidden;">Total</div></td></tr></tbody>'
, '</table>'
,'</div>'
,'{{# } }}'
,'<div class="layui-table-column layui-table-page layui-hide">'
,'<div class="layui-inline layui-table-pageview" id="layui-table-page{{=d.index}}"></div>'
,'</div>'
].join('');
var _WIN = $(window);
var _DOC = $(document);
// constructor
var Class = function(options){
var that = this;
that.index = ++table.index;
that.config = $.extend({}, that.config, table.config, options);
that.render();
};
// 初始默认配置
Class.prototype.config = {
limit: 10, // 每页显示的数量
loading: true, // 请求数据时,是否显示 loading
escape: true, // 是否开启 HTML 编码功能,即转义 html 原文
cellMinWidth: 60, // 所有单元格默认最小宽度
cellMaxWidth: Number.MAX_VALUE, // 所有单元格默认最大宽度
editTrigger: 'click', // 单元格编辑的事件触发方式
defaultToolbar: ['filter', 'exports', 'print'], // 工具栏右侧图标
defaultContextmenu: true, // 显示默认上下文菜单
autoSort: true, // 是否前端自动排序。如果否,则需自主排序(通常为服务端处理好排序)
text: {
none: '无数据'
},
cols: []
};
// 表格渲染
Class.prototype.render = function(type){
var that = this;
var options = that.config;
options.elem = $(options.elem);
options.where = options.where || {};
// 初始化 id 属性 - 优先取 options > 元素 id > 自增索引
var id = options.id = 'id' in options ? options.id : (
options.elem.attr('id') || that.index
);
thisTable.that[id] = that; // 记录当前实例对象
thisTable.config[id] = options; // 记录当前实例配置项
//请求参数的自定义格式
options.request = $.extend({
pageName: 'page',
limitName: 'limit'
}, options.request)
// 响应数据的自定义格式
options.response = $.extend({
statusName: 'code', //规定数据状态的字段名称
statusCode: 0, //规定成功的状态码
msgName: 'msg', //规定状态信息的字段名称
dataName: 'data', //规定数据总数的字段名称
totalRowName: 'totalRow', //规定数据统计的字段名称
countName: 'count'
}, options.response);
//如果 page 传入 laypage 对象
if(options.page !== null && 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;
// 若元素未设 lay-filter 属性,则取实例 id 值
if(!options.elem.attr('lay-filter')){
options.elem.attr('lay-filter', options.id);
}
// 仅重载数据
if(type === 'reloadData'){
// 请求数据
return that.pullData(that.page, {
type: 'reloadData'
});
}
// 初始化索引
options.index = that.index;
that.key = options.id || options.index;
// 初始化一些其他参数
that.setInit();
// 高度铺满:full-差距值
if(options.height && /^full-.+$/.test(options.height)){
that.fullHeightGap = options.height.split('-')[1];
options.height = _WIN.height() - (parseFloat(that.fullHeightGap) || 0);
} else if (options.height && /^#\w+\S*-.+$/.test(options.height)) {
var parentDiv = options.height.split("-");
that.parentHeightGap = parentDiv.pop();
that.parentDiv = parentDiv.join("-");
options.height = $(that.parentDiv).height() - (parseFloat(that.parentHeightGap) || 0);
} else if (typeof options.height === "function"){
that.customHeightFunc = options.height;
options.height = that.customHeightFunc();
}
// 开始插入替代元素
var othis = options.elem;
var hasRender = othis.next('.' + ELEM_VIEW);
// 主容器
var reElem = that.elem = $('<div></div>');
// 添加 className
reElem.addClass(function(){
var arr = [
ELEM_VIEW,
ELEM_VIEW +'-'+ that.index,
'layui-form',
'layui-border-box'
];
if(options.className) arr.push(options.className);
return arr.join(' ');
}()).attr({
'lay-filter': 'LAY-TABLE-FORM-DF-'+ that.index,
'lay-id': options.id,
'style': function(){
var arr = [];
if(options.width) arr.push('width:'+ options.width + 'px;');
// if(options.height) arr.push('height:'+ options.height + 'px;');
return arr.join('')
}()
}).html(laytpl(TPL_MAIN, {
open: '{{', // 标签符前缀
close: '}}' // 标签符后缀
}).render({
data: options,
index: that.index //索引
}));
// 初始化样式
that.renderStyle();
// 生成替代元素
if(hasRender[0]){
that.resizeObserver && that.resizeObserver.unobserve(that.elem[0]);
hasRender.remove(); // 如果已经渲染,则 Rerender
}
othis.after(reElem);
// 各级容器
that.layTool = reElem.find(ELEM_TOOL);
that.layBox = reElem.find(ELEM_BOX);
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.layTotal = reElem.find(ELEM_TOTAL);
that.layPage = reElem.find(ELEM_PAGE);
// 初始化头部工具栏
that.renderToolbar();
// 初始化底部分页栏
that.renderPagebar();
// 让表格平铺
that.fullSize();
that.pullData(that.page); // 请求数据
that.events(); // 事件
};
// 根据列类型,定制化参数
Class.prototype.initOpts = function(item){
var that = this
var options = that.config;
var initWidth = {
checkbox: 50,
radio: 50,
space: 30,
numbers: 60
};
// 让 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 || initWidth[item.type];
}
};
//初始化一些参数
Class.prototype.setInit = function(type){
var that = this;
var options = that.config;
options.clientWidth = options.width || function(){ //获取容器宽度
//如果父元素宽度为0(一般为隐藏元素),则继续查找上层元素,直到找到真实宽度为止
var getWidth = function(parent){
var width, isNone;
parent = parent || options.elem.parent()
width = parent.width();
try {
isNone = parent.css('display') === 'none';
} catch(e){}
if(parent[0] && (!width || isNone)) return getWidth(parent.parent());
return width;
};
return getWidth();
}();
if(type === 'width') return options.clientWidth;
// 初始化高度配置,如果设置了最高高度,以最高高度形式为准
options.height = options.maxHeight || options.height;
// 初始化 css 参数
if(options.css && options.css.indexOf(ELEM_VIEW) === -1){
var css = options.css.split('}');
layui.each(css, function(index, value){
if(value){
css[index] = '.'+ ELEM_VIEW + '-'+ that.index + ' ' + value;
}
});
options.css = css.join('}');
}
// 封装对 col 的配置处理
var initChildCols = function (i1, item1, i2, item2) {
//如果列参数为空,则移除
if (!item2) {
item1.splice(i2, 1);
return;
}
item2.key = [options.index, i1, i2].join('-');
item2.colspan = item2.colspan || 0;
item2.rowspan = item2.rowspan || 0;
//根据列类型,定制化参数
that.initOpts(item2);
//设置列的父列索引
//如果是组合列,则捕获对应的子列
var indexChild = i1 + (parseInt(item2.rowspan) || 1);
if (indexChild < options.cols.length) { // 只要不是最后一层都会有子列
item2.colGroup = true;
var childIndex = 0;
layui.each(options.cols[indexChild], function (i22, item22) {
//如果子列已经被标注为{HAS_PARENT},或者子列累计 colspan 数等于父列定义的 colspan,则跳出当前子列循环
if (item22.HAS_PARENT || (childIndex >= 1 && childIndex == (item2.colspan || 1))) return;
item22.HAS_PARENT = true;
item22.parentKey = [options.index, i1, i2].join('-') // i1 + '-' + i2;
childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
initChildCols(indexChild, options.cols[indexChild], i22, item22);
});
} else {
item2.colGroup = false;
}
item2.hide = item2.hide && !item2.colGroup || false; // 初始化中中间节点的hide信息不做处理,否则会出错,如果需要必须将其子节点也都同步成hide
};
// 初始化列参数
layui.each(options.cols, function(i1, item1){
layui.each(item1, function(i2, item2){
if (i1) {
delete item2.HAS_PARENT; // 去掉临时的计数排除标识,避免有新字段插入的时候重新计算被跳过导致下标出错的问题
} else {
initChildCols(i1, item1, i2, item2); // 只解析顶层节点由递归完成解析
}
});
});
};
// 初始化样式
Class.prototype.renderStyle = function() {
var that = this;
var options = that.config;
var index = that.index;
var text = [];
// 单元格宽度
layui.each(options.cols, function(i1, item1) {
layui.each(item1, function(i2, item2) {
var key = [index, i1, i2].join('-');
var val = ['width: ', (item2.width || options.cellMinWidth), 'px'].join('');
text.push('.laytable-cell-'+ key +'{'+ val +'}');
});
});
// 自定义行样式
(function (lineStyle) {
if (!lineStyle) return;
var trClassName = '.layui-table-view-'+ index +' .layui-table-body .layui-table tr';
var rules = lineStyle.split(';');
var cellMaxHeight = 'none';
// 计算单元格最大高度
layui.each(rules, function(i, rule) {
rule = rule.split(':');
if (rule[0] === 'height') {
var val = parseFloat(rule[1]);
if (!isNaN(val)) cellMaxHeight = (val - 1) + 'px';
return true;
}
});
// 多行相关样式
layui.each([
'{'+ lineStyle +'}',
'.layui-table-cell{height: auto; max-height: '+ cellMaxHeight +'; white-space: normal; text-overflow: clip;}',
'> td:hover > .layui-table-cell{overflow: auto;}'
].concat(
device.ie ? [
'.layui-table-edit{height: '+ cellMaxHeight +';}',
'td[data-edit]:hover:after{height: '+ cellMaxHeight +';}'
] : []
), function(i, val) {
val && text.push(trClassName + ' ' + val);
});
})(options.lineStyle);
// 自定义 css 属性
if (options.css) text.push(options.css);
// 生成 style
lay.style({
target: that.elem[0],
text: text.join(''),
id: 'DF-table-'+ index
});
};
// 初始工具栏
Class.prototype.renderToolbar = function(){
var that = this
var options = that.config
// 添加工具栏左侧模板
var leftDefaultTemp = [
'<div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>',
'<div class="layui-inline" lay-event="update"><i class="layui-icon layui-icon-edit"></i></div>',
'<div class="layui-inline" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>'
].join('');
var elemToolTemp = that.layTool.find('.layui-table-tool-temp');
if(options.toolbar === 'default'){
elemToolTemp.html(leftDefaultTemp);
} else if(typeof options.toolbar === 'string'){
var toolbarHtml = $(options.toolbar).html() || '';
toolbarHtml && elemToolTemp.html(
laytpl(toolbarHtml).render(options)
);
}
// 添加工具栏右侧面板
var layout = {
filter: {
title: '筛选列',
layEvent: 'LAYTABLE_COLS',
icon: 'layui-icon-cols'
},
exports: {
title: '导出',
layEvent: 'LAYTABLE_EXPORT',
icon: 'layui-icon-export'
},
print: {
title: '打印',
layEvent: 'LAYTABLE_PRINT',
icon: 'layui-icon-print'
}
}, iconElem = [];
if(typeof options.defaultToolbar === 'object'){
layui.each(options.defaultToolbar, function(i, item){
var thisItem = typeof item === 'string' ? layout[item] : item;
if(thisItem){
iconElem.push('<div class="layui-inline" title="'+ thisItem.title +'" lay-event="'+ thisItem.layEvent +'">'
+'<i class="layui-icon '+ thisItem.icon +'"></i>'
+'</div>');
}
});
}
that.layTool.find('.layui-table-tool-self').html(iconElem.join(''));
};
// 分页栏
Class.prototype.renderPagebar = function(){
var that = this;
var options = that.config;
var layPagebar = that.layPagebar = $('<div class="layui-inline layui-table-pagebar"></div>');
// 开启分页栏自定义模板
if(options.pagebar){
var pagebarHtml = $(options.pagebar).html() || '';
pagebarHtml && layPagebar.append(laytpl(pagebarHtml).render(options));
that.layPage.append(layPagebar);
}
};
// 同步表头父列的相关值
Class.prototype.setParentCol = function(hide, parentKey){
var that = this;
var options = that.config;
var parentTh = that.layHeader.find('th[data-key="'+ parentKey +'"]'); // 获取父列元素
var parentColspan = parseInt(parentTh.attr('colspan')) || 0;
if(parentTh[0]){
var arrParentKey = parentKey.split('-');
var getThisCol = options.cols[arrParentKey[1]][arrParentKey[2]];
hide ? parentColspan-- : parentColspan++;
parentTh.attr('colspan', parentColspan);
parentTh[parentColspan ? 'removeClass' : 'addClass'](HIDE); // 如果子列显示,父列必然需要显示
getThisCol.colspan2 = parentColspan; // 更新实际的 colspan 数
getThisCol.hide = parentColspan < 1; // 同步 hide 参数
// 递归,继续往上查询是否有父列
var nextParentKey = parentTh.data('parentkey');
nextParentKey && that.setParentCol(hide, nextParentKey);
}
};
// 多级表头补丁
Class.prototype.setColsPatch = function(){
var that = this;
var options = that.config;
// 同步表头父列的相关值
layui.each(options.cols, function(i1, item1){
layui.each(item1, function(i2, item2){
if(item2.hide){
that.setParentCol(item2.hide, item2.parentKey);
}
});
});
};
// 设置组合表头的最大宽度
Class.prototype.setGroupWidth = function(th){
var that = this;
var options = that.config;
if(options.cols.length <= 1) return;
// 获取表头组合
var groups = that.layHeader.find((
// 根据当前活动的表头 parentkey 属性查找其组合表头
th ? ('th[data-key='+ th.data('parentkey') +']>') : ''
) + '.' + ELEM_GROUP); // 若无指向当前活动表头,则自下而上获取所有组合表头
groups.css('width', 0);
layui.each(groups.get().reverse(), function(){
var othis = $(this);
var key = othis.parent().data('key');
var maxWidth = 0;
that.layHeader.eq(0).find('th[data-parentkey='+ key +']').width(function(i, width){
var oTh = $(this);
if(oTh.hasClass(HIDE)) return;
width > 0 && (maxWidth += width);
});
// 给组合表头赋值最大宽度
if(maxWidth) othis.css('max-width', maxWidth - 1);
// 若当前活动的组合表头仍存在上级,则继续向上设置
if(th && othis.parent().data('parentkey')){
that.setGroupWidth(othis.parent());
}
});
groups.css('width', 'auto');
};
// 动态分配列宽
Class.prototype.setColsWidth = function(){
var that = this;
var options = that.config;
var colNums = 0; // 列个数
var autoColNums = 0; // 自动列宽的列个数
var autoWidth = 0; // 自动列分配的宽度
var countWidth = 0; // 所有列总宽度和
var cntrWidth = that.setInit('width');
// 统计列个数
that.eachCols(function(i, item){
item.hide || colNums++;
});
// 减去边框差和滚动条宽
cntrWidth = cntrWidth - function(){
return (options.skin === 'line' || options.skin === 'nob') ? 2 : colNums + 1;
}() - that.getScrollWidth(that.layMain[0]) - 1;
// 计算自动分配的宽度
var getAutoWidth = function(back){
// 遍历所有列
layui.each(options.cols, function(i1, item1){
layui.each(item1, function(i2, item2){
var width = 0;
var minWidth = item2.minWidth || options.cellMinWidth; // 最小宽度
var maxWidth = item2.maxWidth || options.cellMaxWidth; // 最大宽度
if(!item2){
item1.splice(i2, 1);
return;
}
if(item2.colGroup || item2.hide) return;
if(!back){
width = item2.width || 0;
if(/\d+%$/.test(width)){ // 列宽为百分比
width = Math.floor((parseFloat(width) / 100) * cntrWidth);
width < minWidth && (width = minWidth);
width > maxWidth && (width = maxWidth);
} else if(!width){ // 列宽未填写
item2.width = width = 0;
autoColNums++;
} else if(item2.type === 'normal'){
// 若 width 小于 minWidth, 则将 width 值自动设为 minWidth 的值
width < minWidth && (item2.width = width = minWidth);
// 若 width 大于 maxWidth, 则将 width 值自动设为 maxWidth 的值
width > maxWidth && (item2.width = width = maxWidth);
}
} else if(autoWidth && autoWidth < minWidth){
autoColNums--;
width = minWidth;
} else if(autoWidth && autoWidth > maxWidth){
autoColNums--;
width = maxWidth;
}
if(item2.hide) width = 0;
countWidth = countWidth + width;
});
});
// 如果未填充满,则将剩余宽度平分
(cntrWidth > countWidth && autoColNums > 0) && (
autoWidth = (cntrWidth - countWidth) / autoColNums
);
}
getAutoWidth();
getAutoWidth(true); // 重新检测分配的宽度是否低于最小列宽
// 记录自动列数
that.autoColNums = autoColNums = autoColNums > 0 ? autoColNums : 0;
// 设置列宽
that.eachCols(function(i3, item3){
var minWidth = item3.minWidth || options.cellMinWidth;
var maxWidth = item3.maxWidth || options.cellMaxWidth;
if(item3.colGroup || item3.hide) return;
// 给未分配宽的列平均分配宽
if(item3.width === 0){
that.cssRules(item3.key, function(item){
item.style.width = Math.floor(function(){
if(autoWidth < minWidth) return minWidth;
if(autoWidth > maxWidth) return maxWidth;
return autoWidth;
}()) + 'px';
});
}
// 给设定百分比的列分配列宽
else if(/\d+%$/.test(item3.width)){
that.cssRules(item3.key, function(item){
var width = Math.floor((parseFloat(item3.width) / 100) * cntrWidth);
width < minWidth && (width = minWidth);
width > maxWidth && (width = maxWidth);
item.style.width = width + 'px';
});
}
// 给拥有普通 width 值的列分配最新列宽
else {
that.cssRules(item3.key, function(item){
item.style.width = item3.width + 'px';
});
}
});
// 填补 Math.floor 造成的数差
var patchNums = that.layMain.width() - that.getScrollWidth(that.layMain[0])
- that.layMain.children('table').outerWidth();
if(that.autoColNums > 0 && patchNums >= -colNums && patchNums <= colNums){
var getEndTh = function(th){
var field;
th = th || that.layHeader.eq(0).find('thead > tr:first-child > th:last-child')
field = th.data('field');
if(!field && th.prev()[0]){
return getEndTh(th.prev())
}
return th;
};
var th = getEndTh();
var key = th.data('key');
that.cssRules(key, function(item){
var width = item.style.width || th.outerWidth();
item.style.width = (parseFloat(width) + patchNums) + 'px';
// 二次校验,如果仍然出现横向滚动条(通常是 1px 的误差导致)
if(that.layMain.height() - that.layMain.prop('clientHeight') > 0){
item.style.width = (parseFloat(item.style.width) - 1) + 'px';
}
});
}
that.setGroupWidth();
// 如果表格内容为空(无数据 或 请求异常)
if (that.layMain.find('tbody').is(":empty")) {
// 将表格宽度设置为跟表头一样的宽度,使之可以出现底部滚动条,以便滚动查看所有字段
var headerWidth = that.layHeader.first().children('table').width()
that.layMain.find('table').width(headerWidth);
} else {
that.layMain.find('table').width('auto');
}
that.loading(!0);
};
// 重置表格尺寸/结构
Class.prototype.resize = function(){
var that = this;
if (!that.layMain) return;
that.fullSize(); // 让表格铺满
that.setColsWidth(); // 自适应列宽
that.scrollPatch(); // 滚动条补丁
};
// 表格重载
Class.prototype.reload = function(options, deep, type){
var that = this;
options = options || {};
delete that.haveInit;
// 防止数组深度合并
layui.each(options, function(key, item){
if(layui.type(item) === 'array') delete that.config[key];
});
// 对参数进行深度或浅扩展
that.config = $.extend(deep, {}, that.config, options);
if (type !== 'reloadData') {
layui.each(that.config.cols, function (i1, item1) {
layui.each(item1, function (i2, item2) {
delete item2.colspan2;
})
})
delete that.config.HAS_SET_COLS_PATCH;
}
// 执行渲染
that.render(type);
};
// 异常提示
Class.prototype.errorView = function(html){
var that = this
,elemNone = that.layMain.find('.'+ NONE)
,layNone = $('<div class="'+ NONE +'">'+ (html || 'Error') +'</div>');
if(elemNone[0]){
that.layNone.remove();
elemNone.remove();
}
that.layFixed.addClass(HIDE);
that.layMain.find('tbody').html('');
that.layMain.append(that.layNone = layNone);
// 异常情况下对 page 和 total 的内容处理
that.layTotal.addClass(HIDE_V);
that.layPage.find(ELEM_PAGE_VIEW).addClass(HIDE_V);
table.cache[that.key] = []; //格式化缓存数据
that.syncCheckAll();
that.renderForm();
that.setColsWidth();
};
// 初始页码
Class.prototype.page = 1;
// 获得数据
Class.prototype.pullData = function(curr, opts){
var that = this;
var options = that.config;
// 同步表头父列的相关值
options.HAS_SET_COLS_PATCH || that.setColsPatch();
options.HAS_SET_COLS_PATCH = true;
var request = options.request;
var response = options.response;
var res;
var sort = function(){
if(typeof options.initSort === 'object'){
that.sort({
field: options.initSort.field,
type: options.initSort.type,
reloadType: opts.type
});
}
};
var done = function(res, origin){
that.setColsWidth();
typeof options.done === 'function' && options.done(