11import { watch , reactive , onBeforeMount , ToRefs , Slots , h } from 'vue' ;
2- import { Column , TableColumnPropsTypes } from './column.type'
2+ import { Column , TableColumnPropsTypes } from './column.type' ;
33import { formatWidth , formatMinWidth } from '../utils' ;
44
5+ function defaultRenderHeader ( this : Column ) {
6+ return h ( 'span' , { class : 'title' } , this . header ) ;
7+ }
58
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 > {
1010 const {
1111 field,
1212 header,
@@ -20,43 +20,63 @@ export function createColumn<T extends Record<string, unknown> = any>(
2020 filterMultiple,
2121 order,
2222 fixedLeft,
23- fixedRight
23+ fixedRight,
2424 } = props ;
2525 const column : Column = reactive ( { } ) ;
2626
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+ ) ;
3248
3349 // 排序功能
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+ } ) ;
3854
3955 // 过滤功能
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+ ) ;
4965
5066 // 固定左右功能
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+ ) ;
5575
5676 // 宽度
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 ) ;
6080 column . realWidth = column . width || column . minWidth ;
6181 } ) ;
6282
@@ -71,15 +91,3 @@ export function createColumn<T extends Record<string, unknown> = any>(
7191
7292 return column ;
7393}
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