-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathel-data-table.vue
More file actions
1366 lines (1306 loc) · 37.4 KB
/
el-data-table.vue
File metadata and controls
1366 lines (1306 loc) · 37.4 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
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
<template>
<div class="el-data-table">
<template v-if="showNoData">
<!--@slot 获取数据为空时的内容-->
<slot name="no-data"></slot>
</template>
<template v-else>
<!-- 搜索字段 -->
<!-- @submit.native.prevent -->
<!-- 阻止表单提交的默认行为 -->
<!-- https://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2 -->
<el-form-renderer
v-if="hasSearchForm"
ref="searchForm"
:content="_searchForm"
inline
class="search-form-container"
@submit.native.prevent
>
<slot
v-for="slot in searchLocatedSlotKeys"
:slot="slot.replace('search:', 'id:')"
:name="slot"
/>
<!--@slot 额外的搜索内容, 当searchForm不满足需求时可以使用-->
<slot name="search"></slot>
<el-form-item>
<!--https://github.com/ElemeFE/element/pull/5920-->
<el-button
native-type="submit"
type="primary"
:size="buttonSize"
@click="search"
>查询</el-button
>
<el-button :size="buttonSize" @click="resetSearch">重置</el-button>
</el-form-item>
</el-form-renderer>
<el-form v-if="hasHeader">
<el-form-item class="header-button-container">
<el-button
v-if="hasNew"
type="primary"
:size="buttonSize"
@click="onDefaultNew"
>{{ newText }}</el-button
>
<template v-for="(btn, i) in headerButtons">
<self-loading-button
v-if="'show' in btn ? btn.show(selected) : true"
:key="i"
:disabled="'disabled' in btn ? btn.disabled(selected) : false"
:click="btn.atClick"
:params="selected"
:callback="getList"
:size="buttonSize"
v-bind="btn"
>
{{
typeof btn.text === 'function' ? btn.text(selected) : btn.text
}}
</self-loading-button>
</template>
<el-button
v-if="hasSelect && hasDelete"
type="danger"
:size="buttonSize"
:disabled="selected.length === 0 || (single && selected.length > 1)"
@click="onDefaultDelete(single ? selected[0] : selected)"
>{{ deleteText }}</el-button
>
<!--@slot 额外的header内容, 当headerButtons不满足需求时可以使用,传入selected -->
<slot name="header" :selected="selected" />
<!--@collapse 自定义折叠按钮, 默认的样式文案不满足时可以使用。传入当前折叠状态 isSearchCollapse: Boolean -->
<slot name="collapse" :isSearchCollapse="isSearchCollapse">
<el-button
v-if="canSearchCollapse"
type="default"
:size="buttonSize"
:icon="`el-icon-arrow-${isSearchCollapse ? 'down' : 'up'}`"
@click="isSearchCollapse = !isSearchCollapse"
>{{ isSearchCollapse ? '展开' : '折叠' }}搜索</el-button
>
</slot>
</el-form-item>
</el-form>
<el-table
ref="table"
v-loading="loading"
v-bind="tableAttrs"
:data="data"
:row-class-name="rowClassName"
v-on="tableEventHandlersInner"
@selection-change="selectStrategy.onSelectionChange"
@select="selectStrategy.onSelect"
@select-all="selectStrategy.onSelectAll($event, selectable)"
>
<!--TODO 不用jsx写, 感觉template逻辑有点不清晰了-->
<template v-if="isTree">
<!--有多选-->
<template v-if="hasSelect">
<el-data-table-column
key="selection-key"
v-bind="{align: columnsAlign, ...columns[0]}"
/>
<el-data-table-column
key="tree-ctrl"
v-bind="{align: columnsAlign, ...columns[1]}"
>
<template slot-scope="scope">
<span
v-for="space in scope.row._level"
:key="space"
class="ms-tree-space"
/>
<span
v-if="iconShow(scope.$index, scope.row)"
class="tree-ctrl"
@click="toggleExpanded(scope.$index)"
>
<i
:class="`el-icon-${scope.row._expanded ? 'minus' : 'plus'}`"
/>
</span>
{{ scope.row[columns[1].prop] }}
</template>
</el-data-table-column>
<el-data-table-column
v-for="col in columns.filter((c, i) => i !== 0 && i !== 1)"
:key="col.prop"
v-bind="{align: columnsAlign, ...col}"
/>
</template>
<!--无选择-->
<template v-else>
<!--展开这列, 丢失 el-data-table-column属性-->
<el-data-table-column
key="tree-ctrl"
v-bind="{align: columnsAlign, ...columns[0]}"
>
<template slot-scope="scope">
<span
v-for="space in scope.row._level"
:key="space"
class="ms-tree-space"
/>
<span
v-if="iconShow(scope.$index, scope.row)"
class="tree-ctrl"
@click="toggleExpanded(scope.$index)"
>
<i
:class="`el-icon-${scope.row._expanded ? 'minus' : 'plus'}`"
/>
</span>
{{ scope.row[columns[0].prop] }}
</template>
</el-data-table-column>
<el-data-table-column
v-for="col in columns.filter((c, i) => i !== 0)"
:key="col.prop"
v-bind="{align: columnsAlign, ...col}"
/>
</template>
</template>
<!--非树-->
<template v-else>
<el-data-table-column
v-for="col in columns"
:key="col.prop"
v-bind="{align: columnsAlign, ...col}"
/>
</template>
<!--默认操作列-->
<el-data-table-column
v-if="hasOperation"
label="操作"
v-bind="{align: columnsAlign, ...operationAttrs}"
>
<template slot-scope="scope">
<self-loading-button
v-if="isTree && hasNew"
type="primary"
:size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'"
@click="onDefaultNew(scope.row)"
>
{{ newText }}
</self-loading-button>
<self-loading-button
v-if="hasEdit"
type="primary"
:size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'"
@click="onDefaultEdit(scope.row)"
>
{{ editText }}
</self-loading-button>
<self-loading-button
v-if="hasView"
type="primary"
:size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'"
@click="onDefaultView(scope.row)"
>
{{ viewText }}
</self-loading-button>
<template v-for="(btn, i) in extraButtons">
<self-loading-button
v-if="'show' in btn ? btn.show(scope.row) : true"
:key="i"
:is-text="operationButtonType === 'text'"
v-bind="btn"
:click="btn.atClick"
:params="scope.row"
:callback="getList"
:disabled="'disabled' in btn ? btn.disabled(scope.row) : false"
>
{{
typeof btn.text === 'function'
? btn.text(scope.row)
: btn.text
}}
</self-loading-button>
</template>
<self-loading-button
v-if="!hasSelect && hasDelete && canDelete(scope.row)"
type="danger"
:size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'"
@click="onDefaultDelete(scope.row)"
>
{{ deleteText }}
</self-loading-button>
<!--@slot 自定义操作列, 当extraButtons不满足需求时可以使用。传入 row -->
<slot name="operation" :row="scope.row" />
</template>
</el-data-table-column>
<!--@slot 默认slot,同 el-table -->
<slot />
</el-table>
<el-pagination
v-if="hasPagination"
:current-page="page"
:page-sizes="paginationSizes"
:page-size="size"
:total="total"
style="text-align: right; padding: 10px 0;"
:layout="paginationLayout"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
<the-dialog
ref="dialog"
:new-title="dialogNewTitle"
:edit-title="dialogEditTitle"
:view-title="dialogViewTitle"
:form="dialogForm || form"
:form-attrs="formAttrs"
:dialog-attrs="dialogAttrs"
:button-size="buttonSize"
@confirm="onConfirm"
>
<template v-slot="scope">
<!-- @slot 表单作用域插槽。当编辑、查看时传入row;新增时row=null -->
<slot name="form" :row="scope.row" />
</template>
</the-dialog>
</template>
</div>
</template>
<script>
import _get from 'lodash.get'
import _values from 'lodash.values'
import _kebabcase from 'lodash.kebabcase'
import _isEmpty from 'lodash.isempty'
import SelfLoadingButton from './components/self-loading-button.vue'
import TheDialog, {dialogModes} from './components/the-dialog.vue'
import ElDataTableColumn from './components/el-data-table-column'
import * as queryUtil from './utils/query'
import getSelectStrategy from './utils/select-strategy'
import getLocatedSlotKeys from './utils/extract-keys'
import transformSearchImmediatelyItem from './utils/search-immediately-item'
import {isFalsey, removeEmptyKeys} from './utils/utils'
const defaultFirstPage = 1
const noPaginationDataPath = 'payload'
export default {
name: 'ElDataTable',
components: {
SelfLoadingButton,
TheDialog,
ElDataTableColumn
},
props: {
/**
* 请求url, 如果为空, 则不会发送请求; 改变url, 则table会重新发送请求
*/
url: {
type: String,
default: ''
},
/**
* 主键,默认值 id,
* 修改/删除时会用到,请求会根据定义的属性值获取主键,即row[this.id]
*/
id: {
type: String,
default: 'id'
},
/**
* 分页请求的第一页的值(有的接口0是第一页)
*/
firstPage: {
type: Number,
default: defaultFirstPage
},
/**
* 渲染组件的分页数据在接口返回的数据中的路径, 嵌套对象使用.表示即可
*/
dataPath: {
type: String,
default: 'payload.content'
},
/**
* 分页数据的总数在接口返回的数据中的路径, 嵌套对象使用.表示即可
*/
totalPath: {
type: String,
default: 'payload.totalElements'
},
/**
* 请求的时候如果接口需要的页码的查询 key 不同的时候可以指定
*/
pageKey: {
type: String,
default: 'page'
},
/**
* 请求的时候如果接口需要的分页数量的查询 key 不同的时候可以指定
*/
pageSizeKey: {
type: String,
default: 'size'
},
/**
* 处理请求返回的数据
* @param raw axios 返回的原始数据
* @return 函数应返回 {total, data}
*/
onResponse: {
type: Function,
default: undefined
},
/**
* 列属性设置, 详情见element-ui官网
* @link https://element.eleme.cn/2.4/#/zh-CN/component/table#table-column-attributes
*/
columns: {
type: Array,
default() {
return []
}
},
/**
* 查询字段渲染, 配置参考el-form-renderer
* @link https://femessage.github.io/el-form-renderer/
*/
searchForm: {
type: Array,
default() {
return []
}
},
/**
* 是否开启搜索栏折叠功能
*/
canSearchCollapse: {
type: Boolean,
default: false
},
/**
* 点击查询按钮, 查询前执行的函数,参数form表单数据,需要返回Promise
*/
beforeSearch: {
type: Function,
default() {}
},
/**
* 单选, 适用场景: 不可以批量删除
*/
single: {
type: Boolean,
default: false
},
/**
* 切换页面时,已勾选项不会丢失
*/
persistSelection: {
type: Boolean,
default: false
},
/**
* 是否有操作列
*/
hasOperation: {
type: Boolean,
default: true
},
/**
* 操作列的自定义按钮, 渲染的是element-ui的button, 支持包括style在内的以下属性:
* {type: '', text: '', atClick: row => Promise.resolve(), show: row => return true时显示, disabled: row => return true时禁用 }
* 点击事件 row参数 表示当前行数据, 需要返回Promise, 默认点击后会刷新table, resolve(false) 则不刷新
*/
extraButtons: {
type: Array,
default() {
return []
}
},
/**
* 头部的自定义按钮, 渲染的是element-ui的button, 支持包括style在内的以下属性:
* {type: '', text: '', atClick: selected => Promise.resolve(), show: selected => return true时显示, disabled: selected => return true时禁用}
* 点击事件 selected参数 表示选中行所组成的数组, 函数需要返回Promise, 默认点击后会刷新table, resolve(false) 则不刷新
*/
headerButtons: {
type: Array,
default() {
return []
}
},
/**
* 是否有新增按钮
*/
hasNew: {
type: Boolean,
default: true
},
/**
* 是否有编辑按钮
*/
hasEdit: {
type: Boolean,
default: true
},
/**
* 是否有查看按钮
*/
hasView: {
type: Boolean,
default: false
},
/**
* table头部是否有删除按钮(该按钮要多选时才会出现)
*/
hasDelete: {
type: Boolean,
default: true
},
/**
* 新增按钮文案
*/
newText: {
type: String,
default: '新增'
},
/**
* 修改按钮文案
*/
editText: {
type: String,
default: '修改'
},
/**
* 查看按钮文案
*/
viewText: {
type: String,
default: '查看'
},
/**
* 删除按钮文案
*/
deleteText: {
type: String,
default: '删除'
},
/**
* 删除提示语。接受要删除的数据(单个对象或数组);返回字符串
* @param {object|object[]} 要删除的数据 - 单个对象或数组
* @return {string}
*/
deleteMessage: {
type: Function,
default() {
return `确认${this.deleteText}吗?`
}
},
/**
* 某行数据是否可以删除, 返回true表示可以, 控制的是单选时单行的删除按钮
*/
canDelete: {
type: Function,
default() {
return true
}
},
/**
* 点击新增按钮时的方法, 当默认新增方法不满足需求时使用, 需要返回promise
* 参数(data, row) data 是form表单的数据, row 是当前行的数据, 只有isTree为true时, 点击操作列的新增按钮才会有值
*/
onNew: {
type: Function,
default(data) {
return this.$axios.post(this.url, data, this.axiosConfig)
}
},
/**
* 点击修改按钮时的方法, 当默认修改方法不满足需求时使用, 需要返回promise
* 参数(data, row) data 是form表单的数据, row 是当前行的数据
*/
onEdit: {
type: Function,
default(data) {
return this.$axios.put(
`${this.url}/${this.row[this.id]}`,
data,
this.axiosConfig
)
}
},
/**
* 点击删除按钮时的方法, 当默认删除方法不满足需求时使用, 需要返回promise
* 多选时, 参数为selected, 代表选中的行组成的数组; 非多选时参数为row, 代表单行的数据
*/
onDelete: {
type: Function,
default(data) {
const ids = Array.isArray(data)
? data.map(v => v[this.id]).join(',')
: data[this.id]
return this.$axios.delete(this.url + '/' + ids, this.axiosConfig)
}
},
/**
* crud 操作成功后会调用的函数,默认是 this.$message.success('操作成功')
* 接受两个参数:
* type,操作的类型,可能的值有 new | edit | delete;
* data,操作的数据对象
*/
onSuccess: {
type: Function,
default() {
return this.$message.success('操作成功')
}
},
/**
* 是否分页。如果不分页,则请求传参page=-1
*/
hasPagination: {
type: Boolean,
default: true
},
/**
* 分页组件的子组件布局,子组件名用逗号分隔,对应element-ui pagination的layout属性
* @link https://element.eleme.cn/2.4/#/zh-CN/component/pagination
*/
paginationLayout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
/**
* 分页组件的每页显示个数选择器的选项设置,对应element-ui pagination的page-sizes属性
* @link https://element.eleme.cn/2.4/#/zh-CN/component/pagination
*/
paginationSizes: {
type: Array,
default: () => [10, 20, 30, 40, 50]
},
/**
* 分页组件的每页显示个数选择器默认选项,对应element-ui pagination的page-size属性
* @link https://element.eleme.cn/2.4/#/zh-CN/component/pagination
*/
paginationSize: {
type: Number,
default: 20
},
/**
* @deprecated
* 不分页时的size的大小(建议接口约定,不分页时传参page=-1,故一般不会用到此属性)
*/
noPaginationSize: {
type: Number,
default: 999
},
/**
* 要渲染的数据是否是树形结构
*/
isTree: {
type: Boolean,
default: false
},
/**
* 树形结构相关: 子节点的字段名
*/
treeChildKey: {
type: String,
default: 'children'
},
/**
* 树形结构相关: 父节点的字段名
*/
treeParentKey: {
type: String,
default: 'parentId'
},
/**
* 树形结构相关: 父节点字段值的来源字段。
* 新增/修改时会用到, 例如, 在id为2的节点新增子节点, 则子节点的parentId为2, 也即parentId的值来源于字段id, 故treeParentValue为id
*/
treeParentValue: {
type: String,
default: 'id'
},
/**
* 树形结构相关: 是否展开所有节点
*/
expandAll: {
type: Boolean,
default: false
},
/**
* el-table 的 prop 配置,详情配置参考element-ui官网
* @link https://element.eleme.cn/2.4/#/zh-CN/component/table#table-attributes
*/
tableAttrs: {
type: Object,
default() {
return {}
}
},
/**
* el-table 的 eventHandler 配置,详情配置参考element-ui官网
* @link https://element.eleme.cn/2.4/#/zh-CN/component/table#table-attributes
*/
tableEventHandlers: {
type: Object,
default() {
return {}
}
},
/**
* 操作列属性
* @link https://element.eleme.cn/2.4/#/zh-CN/component/table#table-column-attributes
*/
operationAttrs: {
type: Object,
default() {
return {width: '', fixed: 'right'}
}
},
/**
* 新增弹窗的标题,默认为newText的值
*/
dialogNewTitle: {
type: String,
default() {
return this.newText
}
},
/**
* 修改弹窗的标题,默认为editText的值
*/
dialogEditTitle: {
type: String,
default() {
return this.editText
}
},
/**
* 查看弹窗的标题,默认为viewText的值
*/
dialogViewTitle: {
type: String,
default() {
return this.viewText
}
},
/**
* @deprecated
*/
form: {
type: Array,
default() {
return []
}
},
/**
* 弹窗表单, 用于新增与修改, 详情配置参考 el-form-renderer
* 为了 api 更符合直觉而设置
* @link https://femessage.github.io/el-form-renderer/
*/
dialogForm: {
type: Array,
default() {
return null
}
},
/**
* 弹窗表单属性设置, 详情配置参考element-ui官网
* @link https://element.eleme.cn/2.4/#/zh-CN/component/form#form-attributes
*/
formAttrs: {
type: Object,
default() {
return {}
}
},
/**
* 对话框属性设置, 详情配置参考element-ui官网
* @link https://element.eleme.cn/2.4/#/zh-CN/component/dialog#attributes
*/
dialogAttrs: {
type: Object,
default() {
return {}
}
},
/**
* 同extraBody
* @deprecated
*/
extraParams: {
type: Object,
default() {
return undefined
}
},
/**
* 新增/修改提交时,请求体带上额外的参数。
*/
extraBody: {
type: Object,
default() {
return undefined
}
},
/**
* 在新增/修改弹窗 点击确认时调用,返回Promise, 如果reject, 则不会发送新增/修改请求
* 参数: (data, isNew) data为表单数据, isNew true 表示是新增弹窗, false 为 编辑弹窗
*/
beforeConfirm: {
type: Function,
default() {
return Promise.resolve()
}
},
/**
* 同extraQuery
* @deprecated
*/
customQuery: {
type: Object,
default() {
return undefined
}
},
/**
* 向请求url添加的额外参数。
* 可用.sync修饰,此时点击重置按钮后该参数也会被重置
*/
extraQuery: {
type: Object,
default() {
return undefined
}
},
/**
* 是否开启使用url保存query参数的功能
*/
saveQuery: {
type: Boolean,
default: true
},
/**
* 操作栏按钮类型
* `text` 为文本按钮, `button` 为普通按钮
*/
operationButtonType: {
type: String,
default: 'text'
},
/**
* 设置 `按钮` 大小
* @see https://element.eleme.cn/#/zh-CN/component/button#bu-tong-chi-cun
*/
buttonSize: {
type: String,
default: 'small'
},
/**
* 设置axios的config参数
*/
axiosConfig: {
type: Object,
default() {
return {}
}
}
},
data() {
return {
data: [],
size: this.paginationSize || this.paginationSizes[0],
page: defaultFirstPage,
// https://github.com/ElemeFE/element/issues/1153
total: null,
loading: false,
// 多选项的数组
selected: [],
// 要修改的那一行
row: {},
// 初始的extraQuery值, 重置查询时, 会用到
// JSON.stringify是为了后面深拷贝作准备
initExtraQuery: JSON.stringify(this.extraQuery || this.customQuery || {}),
isSearchCollapse: false,
showNoData: false
}
},
computed: {
tableEventHandlersInner() {
const handlers = {}
for (const key in this.tableEventHandlers) {
const kebab = _kebabcase(key)
handlers[kebab] = this.tableEventHandlers[key]
}
return handlers
},
hasSelect() {
return this.columns.length && this.columns[0].type == 'selection'
},
selectable() {
if (this.hasSelect && this.columns[0].selectable) {
return this.columns[0].selectable
}
return () => true
},
columnsAlign() {
if (this.columns.some(col => col.columns && col.columns.length)) {
// 多级表头默认居中
return 'center'
} else {
return ''
}
},
routerMode() {
return this.$router ? this.$router.mode : 'hash'
},
hasSearchForm() {
return this.searchForm.length || this.$slots.search
},
hasHeader() {
return (
this.hasNew ||
(this.hasSelect && this.hasDelete) ||
this.headerButtons.length ||
this.canSearchCollapse ||
this.$scopedSlots.header
)
},
_extraBody() {
return this.extraBody || this.extraParams || {}
},
_extraQuery() {
return this.extraQuery || this.customQuery || {}
},
selectStrategy() {
return getSelectStrategy(this)
},
searchLocatedSlotKeys() {
return getLocatedSlotKeys(this.$slots, 'search:')
},
collapseForm() {
return this.searchForm.map(item => {
if ('collapsible' in item && !item.collapsible) {
return item
}
const itemHidden = item.hidden || (() => false)
return {
...item,
hidden: data => {
return this.isSearchCollapse || itemHidden(data)
}
}
})
},
_searchForm() {
return transformSearchImmediatelyItem(this.collapseForm, this)
}
},
watch: {
url: {
handler(val) {
if (!val) return
this.page = defaultFirstPage
// mounted处有updateForm的行为,所以至少在初始执行时要等到nextTick
this.$nextTick(this.getList)
},
immediate: true
},
selected(val) {
/**
* 多选项发生变化
* @property {array} rows - 已选中的行数据的数组
*/
this.$emit('selection-change', val)
}
},
mounted() {
if (this.saveQuery) {
const query = queryUtil.get(location.href)
if (query) {
this.page = parseInt(query[this.pageKey])
this.size = parseInt(query[this.pageSizeKey])
// 恢复查询条件,但对slot=search无效
if (this.$refs.searchForm) {
delete query[this.pageKey]
delete query[this.pageSizeKey]
this.$refs.searchForm.updateForm(query)
}
}
}
},
methods: {
/**
* 手动刷新列表数据,选项的默认值为: { loading: true }
* @public
* @param {object} options 方法选项
*/
getList({loading = true} = {}) {
const {url} = this
if (!url) {
console.warn('DataTable: url 为空, 不发送请求')
return
}
// 构造query对象
let query = {}
let formValue = {}
if (this.$refs.searchForm) {
formValue = this.$refs.searchForm.getFormValue()
Object.assign(query, formValue)
}
Object.assign(query, this._extraQuery)
query[this.pageSizeKey] = this.hasPagination
? this.size
: this.noPaginationSize
// 根据偏移值计算接口正确的页数
const pageOffset = this.firstPage - defaultFirstPage
query[this.pageKey] = this.hasPagination ? this.page + pageOffset : -1
// 无效值过滤,注意0是有效值
query = Object.keys(query)
.filter(k => ![undefined, null].includes(query[k]))
.reduce((obj, k) => ((obj[k] = query[k]), obj), {})
// 请求开始
this.loading = loading
// 存储query记录, 便于后面恢复
if (this.saveQuery) {
// 存储的page是table的页码,无需偏移
query[this.pageKey] = this.page
const newUrl = queryUtil.set(location.href, query, this.routerMode)
history.replaceState(history.state, 'el-data-table search', newUrl)