1
1
/**
2
2
* 处理自适应列宽的函数
3
- * @param autoArr - 需要自适应的列数组
3
+ * @param autoList - 需要自适应的列数组
4
4
* @param meanWidth - 平均宽度
5
5
* @param minCellWidth - 最小单元格宽度
6
6
* @param tableWidth - 表格总宽度
7
7
* @param fit - 是否需要填充满容器
8
8
* @param bodyWidth - 表格容器宽度
9
9
*/
10
- const adaptive = ( { autoArr , meanWidth, minCellWidth, tableWidth, fit, bodyWidth } ) => {
11
- autoArr . forEach ( ( column , index ) => {
12
- let width = Math . max ( meanWidth , minCellWidth )
10
+ const adaptive = ( { autoList , meanWidth, minCellWidth, tableWidth, fit, bodyWidth } ) => {
11
+ autoList . forEach ( ( column , index ) => {
12
+ const width = Math . max ( meanWidth , minCellWidth )
13
13
14
14
column . renderWidth = width
15
15
tableWidth += width
16
16
17
- if ( fit && index === autoArr . length - 1 ) {
17
+ if ( fit && index === autoList . length - 1 ) {
18
18
// 如果所有列足够放的情况下,修补列之间的误差
19
- let odiffer = bodyWidth - tableWidth
19
+ const odiffer = bodyWidth - tableWidth
20
20
21
21
if ( odiffer > 0 ) {
22
22
column . renderWidth += odiffer
@@ -39,52 +39,52 @@ const initTableWidth = ({ remainWidth, columnStore }) => {
39
39
let tableWidth = 0
40
40
41
41
// 从columnStore中解构出不同类型的列数组:
42
- // resizeArr - 用户手动调整过宽度的列
43
- // pxMinArr - 设置了最小像素宽度(min-width="100px")的列
44
- // pxArr - 设置了固定像素宽度(width="100px")的列
45
- let { resizeList : resizeArr , pxMinList : pxMinArr , pxList : pxArr } = columnStore
42
+ // resizeList - 用户手动调整过宽度的列
43
+ // pxMinList - 设置了最小像素宽度(min-width="100px")的列
44
+ // pxList - 设置了固定像素宽度(width="100px")的列
45
+ const { resizeList, pxMinList, pxList } = columnStore
46
46
47
- // scaleArr - 设置了百分比宽度(width="20%")的列
47
+ // scaleList - 设置了百分比宽度(width="20%")的列
48
48
// scaleMinArr - 设置了最小百分比宽度(min-width="20%")的列
49
- let { scaleList : scaleArr , scaleMinList : scaleMinArr } = columnStore
49
+ const { scaleList, scaleMinList } = columnStore
50
50
51
51
// 1. 首先处理设置了最小像素宽度的列
52
52
// 这些列的宽度不能小于设定的最小宽度
53
- pxMinArr . forEach ( ( column ) => {
54
- let minWidth = parseInt ( column . minWidth )
53
+ pxMinList . forEach ( ( column ) => {
54
+ const minWidth = parseInt ( column . minWidth )
55
55
tableWidth += minWidth
56
56
column . renderWidth = minWidth
57
57
} )
58
58
59
59
// 计算1%宽度对应的像素值,用于处理百分比宽度
60
- let meanWidth = remainWidth / 100
60
+ const meanWidth = remainWidth / 100
61
61
62
62
// 2. 处理设置了最小百分比宽度的列
63
63
// 将百分比转换为实际像素值
64
- scaleMinArr . forEach ( ( column ) => {
65
- let scaleWidth = Math . floor ( parseInt ( column . minWidth ) * meanWidth )
64
+ scaleMinList . forEach ( ( column ) => {
65
+ const scaleWidth = Math . floor ( parseInt ( column . minWidth ) * meanWidth )
66
66
tableWidth += scaleWidth
67
67
column . renderWidth = scaleWidth
68
68
} )
69
69
70
70
// 3. 处理设置了固定百分比宽度的列
71
- scaleArr . forEach ( ( column ) => {
72
- let scaleWidth = Math . floor ( parseInt ( column . width ) * meanWidth )
71
+ scaleList . forEach ( ( column ) => {
72
+ const scaleWidth = Math . floor ( parseInt ( column . width ) * meanWidth )
73
73
tableWidth += scaleWidth
74
74
column . renderWidth = scaleWidth
75
75
} )
76
76
77
77
// 4. 处理设置了固定像素宽度的列
78
- pxArr . forEach ( ( column ) => {
79
- let width = parseInt ( column . width )
78
+ pxList . forEach ( ( column ) => {
79
+ const width = parseInt ( column . width )
80
80
tableWidth += width
81
81
column . renderWidth = width
82
82
} )
83
83
84
84
// 5. 最后处理用户手动调整过宽度的列
85
85
// 这些列的宽度优先级最高
86
- resizeArr . forEach ( ( column ) => {
87
- let width = parseInt ( column . resizeWidth )
86
+ resizeList . forEach ( ( column ) => {
87
+ const width = parseInt ( column . resizeWidth )
88
88
tableWidth += width
89
89
column . renderWidth = width
90
90
} )
@@ -107,21 +107,21 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
107
107
// 初始化表格宽度和平均宽度
108
108
let { tableWidth, meanWidth } = initTableWidth ( { remainWidth : bodyWidth , columnStore } )
109
109
// 获取最小像素宽度列、最小百分比宽度列和自适应列
110
- let { pxMinList : pxMinArr , scaleMinList : scaleMinArr , autoList : autoArr } = columnStore
110
+ let { pxMinList, scaleMinList, autoList } = columnStore
111
111
112
112
// 计算剩余可用宽度
113
113
const remainWidth = bodyWidth - tableWidth
114
114
// 计算每列平均可分配宽度
115
115
// 如果有剩余宽度,则平均分配给最小宽度列和自适应列
116
116
// 如果没有剩余宽度,则为0
117
- meanWidth = remainWidth > 0 ? Math . floor ( remainWidth / ( scaleMinArr . length + pxMinArr . length + autoArr . length ) ) : 0
117
+ meanWidth = remainWidth > 0 ? Math . floor ( remainWidth / ( scaleMinList . length + pxMinList . length + autoList . length ) ) : 0
118
118
119
119
// 如果需要填充满容器
120
120
if ( fit ) {
121
121
// 如果有剩余宽度
122
122
if ( remainWidth > 0 ) {
123
123
// 将剩余宽度平均分配给最小百分比宽度列和最小像素宽度列
124
- scaleMinArr . concat ( pxMinArr ) . forEach ( ( column ) => {
124
+ scaleMinList . concat ( pxMinList ) . forEach ( ( column ) => {
125
125
tableWidth += meanWidth
126
126
column . renderWidth += meanWidth
127
127
} )
@@ -132,7 +132,7 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
132
132
}
133
133
134
134
// 处理自适应列的宽度
135
- tableWidth = adaptive ( { autoArr , meanWidth, minCellWidth, tableWidth, fit, bodyWidth } )
135
+ tableWidth = adaptive ( { autoList , meanWidth, minCellWidth, tableWidth, fit, bodyWidth } )
136
136
137
137
// 计算处理完自适应列后与容器的剩余空间
138
138
const remainingSpace = bodyWidth - tableWidth
@@ -141,8 +141,8 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
141
141
if ( fit && remainingSpace > 0 ) {
142
142
// 将剩余空间以1px为单位分配给最小百分比宽度列和最小像素宽度列
143
143
// 分配数量不超过剩余空间大小
144
- scaleMinArr
145
- . concat ( pxMinArr )
144
+ scaleMinList
145
+ . concat ( pxMinList )
146
146
. slice ( 0 , remainingSpace )
147
147
. forEach ( ( column ) => {
148
148
tableWidth += 1
0 commit comments