1
1
import { watch , reactive , onBeforeMount , ToRefs , Slots , h } from 'vue' ;
2
- import { Column , TableColumnPropsTypes } from './column.type'
2
+ import { Column , TableColumnPropsTypes } from './column.type' ;
3
3
import { formatWidth , formatMinWidth } from '../utils' ;
4
4
5
+ function defaultRenderHeader ( this : Column ) {
6
+ return h ( 'span' , { class : 'title' } , this . header ) ;
7
+ }
5
8
6
- export function createColumn < T extends Record < string , unknown > = any > (
7
- props : ToRefs < TableColumnPropsTypes > ,
8
- templates : Slots
9
- ) : Column < T > {
9
+ export function createColumn < T extends Record < string , unknown > = any > ( props : ToRefs < TableColumnPropsTypes > , templates : Slots ) : Column < T > {
10
10
const {
11
11
field,
12
12
header,
@@ -20,43 +20,63 @@ export function createColumn<T extends Record<string, unknown> = any>(
20
20
filterMultiple,
21
21
order,
22
22
fixedLeft,
23
- fixedRight
23
+ fixedRight,
24
24
} = props ;
25
25
const column : Column = reactive ( { } ) ;
26
26
27
- watch ( [ field , header , order ] , ( [ field , header , order ] ) => {
28
- column . field = field ;
29
- column . header = header ;
30
- column . order = order ;
31
- } , { immediate : true } ) ;
27
+ function defaultRenderCell < K extends Record < string , unknown > > ( rowData : K , index : number ) {
28
+ const value = rowData [ this . field ] ;
29
+ if ( templates . default ) {
30
+ return templates . default ( rowData ) ;
31
+ }
32
+ if ( this . formatter ) {
33
+ return this . formatter ( rowData , value , index ) ;
34
+ }
35
+
36
+ return value ?. toString ?.( ) ?? '' ;
37
+ }
38
+
39
+ watch (
40
+ [ field , header , order ] ,
41
+ ( [ fieldVal , headerVal , orderVal ] ) => {
42
+ column . field = fieldVal ;
43
+ column . header = headerVal ;
44
+ column . order = orderVal ;
45
+ } ,
46
+ { immediate : true }
47
+ ) ;
32
48
33
49
// 排序功能
34
- watch ( [ sortable , compareFn ] , ( [ sortable , compareFn ] ) => {
35
- column . sortable = sortable ;
36
- column . compareFn = compareFn ;
37
- } )
50
+ watch ( [ sortable , compareFn ] , ( [ sortableVal , compareFnVal ] ) => {
51
+ column . sortable = sortableVal ;
52
+ column . compareFn = compareFnVal ;
53
+ } ) ;
38
54
39
55
// 过滤功能
40
- watch ( [
41
- filterable ,
42
- filterList ,
43
- filterMultiple ,
44
- ] , ( [ filterable , filterList , filterMultiple ] ) => {
45
- column . filterable = filterable ;
46
- column . filterMultiple = filterMultiple ;
47
- column . filterList = filterList ;
48
- } , { immediate : true } )
56
+ watch (
57
+ [ filterable , filterList , filterMultiple ] ,
58
+ ( [ filterableVal , filterListVal , filterMultipleVal ] ) => {
59
+ column . filterable = filterableVal ;
60
+ column . filterMultiple = filterMultipleVal ;
61
+ column . filterList = filterListVal ;
62
+ } ,
63
+ { immediate : true }
64
+ ) ;
49
65
50
66
// 固定左右功能
51
- watch ( [ fixedLeft , fixedRight ] , ( [ left , right ] ) => {
52
- column . fixedLeft = left ;
53
- column . fixedRight = right ;
54
- } , { immediate : true } ) ;
67
+ watch (
68
+ [ fixedLeft , fixedRight ] ,
69
+ ( [ left , right ] ) => {
70
+ column . fixedLeft = left ;
71
+ column . fixedRight = right ;
72
+ } ,
73
+ { immediate : true }
74
+ ) ;
55
75
56
76
// 宽度
57
- watch ( [ width , minWidth ] , ( [ width , minWidth ] ) => {
58
- column . width = formatWidth ( width ) ;
59
- column . minWidth = formatMinWidth ( minWidth ) ;
77
+ watch ( [ width , minWidth ] , ( [ widthVal , minWidthVal ] ) => {
78
+ column . width = formatWidth ( widthVal ) ;
79
+ column . minWidth = formatMinWidth ( minWidthVal ) ;
60
80
column . realWidth = column . width || column . minWidth ;
61
81
} ) ;
62
82
@@ -71,15 +91,3 @@ export function createColumn<T extends Record<string, unknown> = any>(
71
91
72
92
return column ;
73
93
}
74
-
75
- function defaultRenderHeader ( this : Column ) {
76
- return h ( 'span' , { class : 'title' } , this . header ) ;
77
- }
78
-
79
- function defaultRenderCell < T extends Record < string , unknown > > ( this : Column , rowData : T , index : number ) {
80
- const value = rowData [ this . field ] ;
81
- if ( this . formatter ) {
82
- return this . formatter ( rowData , value , index ) ;
83
- }
84
- return value ?. toString ?.( ) ?? '' ;
85
- }
0 commit comments