1
1
<script setup lang="ts">
2
- import { inject } from " vue " ;
3
- import { type TableColumn , type SortType , sortType } from " ../typing " ;
2
+ import type { SortType , TableColumn } from " ../typing " ;
3
+ import LayRender from " @layui/component/component/_components/render " ;
4
4
import LayCheckboxV2 from " @layui/component/component/checkboxV2/index.vue" ;
5
5
6
6
import { isValueArray } from " @layui/component/utils" ;
7
- import { startResize } from " ../hooks/useResize" ;
7
+
8
+ import { inject } from " vue" ;
8
9
import { LAY_TABLE_CONTEXT } from " ../constant" ;
10
+ import { startResize } from " ../hooks/useResize" ;
11
+ import { sortType } from " ../typing" ;
9
12
10
13
defineOptions ({
11
14
name: " LayTableHeader" ,
12
15
});
13
16
17
+ defineProps <{
18
+ lastLevelShowColumns: TableColumn [];
19
+ hierarchicalColumns: TableColumn [][];
20
+ tableBodyScrollWidth: number ;
21
+ }>();
22
+
14
23
const {
15
24
tableProps,
16
25
tableEmits,
26
+ tableSlots,
17
27
18
28
tableDataSource,
19
29
tableRef,
@@ -29,46 +39,37 @@ const {
29
39
commonGetStylees,
30
40
} = inject (LAY_TABLE_CONTEXT )! ;
31
41
32
- defineProps <{
33
- lastLevelShowColumns: TableColumn [];
34
- hierarchicalColumns: TableColumn [][];
35
- tableBodyScrollWidth: number ;
36
- }>();
37
-
38
- const thSort = (e : Event , column : any ) => {
42
+ function thSort(e : Event , column : any ) {
39
43
const spanDom = (e .currentTarget as HTMLElement ).querySelector (
40
- " span[lay-sort]"
44
+ " span[lay-sort]" ,
41
45
) as HTMLSpanElement ;
42
46
43
- if (! spanDom ) return ;
47
+ if (! spanDom )
48
+ return ;
44
49
45
50
const sortValue = spanDom .getAttribute (" lay-sort" ) as SortType ;
46
51
47
52
const currentIndex = sortType .indexOf (sortValue );
48
53
const nextSort = sortType [(currentIndex + 1 ) % sortType .length ];
49
54
50
55
baseSort (spanDom , column , nextSort );
51
- };
56
+ }
52
57
53
- const iconSort = (e : Event , column : any , sort : Exclude <SortType , " " >) => {
58
+ function iconSort(e : Event , column : any , sort : Exclude <SortType , " " >) {
54
59
const spanDom = (e .target as HTMLElement ).parentNode as HTMLSpanElement ;
55
60
56
61
const sortValue = spanDom .getAttribute (" lay-sort" ) as SortType ;
57
62
58
63
baseSort (spanDom , column , sortValue !== sort ? sort : " " );
59
- };
64
+ }
60
65
61
66
/**
62
67
*
63
68
* @param spanDom 包含lay-sort属性的span dom
64
69
* @param column column
65
70
* @param nextSort 下一次的sort
66
71
*/
67
- const baseSort = (
68
- spanDom : HTMLSpanElement ,
69
- column : any ,
70
- nextSort : SortType
71
- ) => {
72
+ function baseSort(spanDom : HTMLSpanElement , column : any , nextSort : SortType ) {
72
73
const { key, sort } = column ;
73
74
74
75
removeAllSortState ();
@@ -79,15 +80,15 @@ const baseSort = (
79
80
}
80
81
81
82
tableEmits (" sort-change" , key , nextSort );
82
- };
83
+ }
83
84
84
- const defaultSort = (key : string , sortType : SortType ) => {
85
+ function defaultSort(key : string , sortType : SortType ) {
85
86
switch (sortType ) {
86
87
case " " :
87
88
tableDataSource .splice (
88
89
0 ,
89
90
tableDataSource .length ,
90
- ... tableProps .dataSource
91
+ ... tableProps .dataSource ,
91
92
);
92
93
break ;
93
94
case " asc" :
@@ -97,38 +98,38 @@ const defaultSort = (key: string, sortType: SortType) => {
97
98
tableDataSource .sort ((a : any , b : any ) => b [key ] - a [key ]);
98
99
break ;
99
100
}
100
- };
101
+ }
101
102
102
103
// 清空所有的sort状态
103
- const removeAllSortState = () => {
104
+ function removeAllSortState() {
104
105
const sortElements = tableRef .value ! .querySelectorAll (" [lay-sort]" );
105
106
if (sortElements && sortElements .length > 0 ) {
106
107
sortElements .forEach ((element ) => {
107
108
element .setAttribute (" lay-sort" , " " );
108
109
});
109
110
}
110
- };
111
+ }
111
112
</script >
112
113
113
114
<template >
114
115
<div
115
116
class =" layui-table-header"
116
117
:style =" [{ 'padding-right': `${tableBodyScrollWidth}px` }]"
117
118
>
118
- <div class =" layui-table-header-wrapper" ref = " tableHeaderRef " >
119
+ <div ref = " tableHeaderRef " class =" layui-table-header-wrapper" >
119
120
<table
121
+ ref =" tableHeaderTableRef"
120
122
class =" layui-table"
121
123
:lay-size =" tableProps.size"
122
124
:lay-skin =" tableProps.skin"
123
- ref =" tableHeaderTableRef"
124
125
>
125
126
<colgroup >
126
127
<col
127
128
v-for =" (column, index) in lastLevelShowColumns"
128
129
:key =" column.key || column.type || index"
129
130
:width =" column.width"
130
131
:style =" { minWidth: column.minWidth }"
131
- / >
132
+ >
132
133
</colgroup >
133
134
<thead >
134
135
<tr
@@ -153,9 +154,9 @@ const removeAllSortState = () => {
153
154
:style =" commonGetStylees(column)"
154
155
@click =" thSort($event, column)"
155
156
>
156
- <template v-if =" column .type == ' checkbox' " >
157
- <layCheckboxV2
158
- :modelValue =" selectedState.allMSelected.value"
157
+ <template v-if =" column .type === ' checkbox' " >
158
+ <LayCheckboxV2
159
+ :model-value =" selectedState.allMSelected.value"
159
160
:is-indeterminate =" selectedState.someMSelected.value"
160
161
skin =" primary"
161
162
value =" all"
@@ -164,13 +165,14 @@ const removeAllSortState = () => {
164
165
</template >
165
166
<template v-else >
166
167
<span >
167
- <template v-if =" column .titleSlot " >
168
- <slot
169
- :name =" column.titleSlot"
170
- :column =" column"
171
- :columnIndex =" columnIndex"
172
- ></slot >
173
- </template >
168
+ <LayRender
169
+ v-if =" column.titleSlot"
170
+ :slots =" tableSlots"
171
+ :render =" column.titleSlot"
172
+ :column =" column"
173
+ :column-index =" columnIndex"
174
+ />
175
+
174
176
<template v-else >
175
177
{{ column.title }}
176
178
</template >
@@ -186,22 +188,22 @@ const removeAllSortState = () => {
186
188
"
187
189
>
188
190
<i
189
- @click.stop =" iconSort($event, column, 'asc')"
190
191
class =" layui-edge layui-table-sort-asc"
191
192
title =" 升序"
192
- ></i >
193
+ @click.stop =" iconSort($event, column, 'asc')"
194
+ />
193
195
<i
194
- @click.stop =" iconSort($event, column, 'desc')"
195
196
class =" layui-edge layui-table-sort-desc"
196
197
title =" 降序"
197
- ></i >
198
+ @click.stop =" iconSort($event, column, 'desc')"
199
+ />
198
200
</span >
199
201
</template >
200
202
<!-- 列宽拖动区 -->
201
203
<div
202
204
v-if ="
203
- (tableProps.resize || column.resize) &&
204
- !isValueArray(column.children)
205
+ (tableProps.resize || column.resize)
206
+ && !isValueArray(column.children)
205
207
"
206
208
class =" lay-table-cols-resize"
207
209
@mousedown.stop ="
@@ -210,11 +212,11 @@ const removeAllSortState = () => {
210
212
column,
211
213
tableHeaderTableRef,
212
214
tableBodyTableRef,
213
- tableTotalTableRef
215
+ tableTotalTableRef,
214
216
)
215
217
"
216
218
@click.stop
217
- ></ div >
219
+ / >
218
220
</th >
219
221
</template >
220
222
</tr >
0 commit comments