Skip to content

Commit 23dcfe1

Browse files
committed
feat: 更新自适应列宽处理逻辑,统一参数命名,增强代码可读性
1 parent 4ee698c commit 23dcfe1

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

packages/vue/src/grid/src/table/src/utils/autoCellWidth.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/**
22
* 处理自适应列宽的函数
3-
* @param autoArr - 需要自适应的列数组
3+
* @param autoList - 需要自适应的列数组
44
* @param meanWidth - 平均宽度
55
* @param minCellWidth - 最小单元格宽度
66
* @param tableWidth - 表格总宽度
77
* @param fit - 是否需要填充满容器
88
* @param bodyWidth - 表格容器宽度
99
*/
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)
1313

1414
column.renderWidth = width
1515
tableWidth += width
1616

17-
if (fit && index === autoArr.length - 1) {
17+
if (fit && index === autoList.length - 1) {
1818
// 如果所有列足够放的情况下,修补列之间的误差
19-
let odiffer = bodyWidth - tableWidth
19+
const odiffer = bodyWidth - tableWidth
2020

2121
if (odiffer > 0) {
2222
column.renderWidth += odiffer
@@ -39,52 +39,52 @@ const initTableWidth = ({ remainWidth, columnStore }) => {
3939
let tableWidth = 0
4040

4141
// 从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
4646

47-
// scaleArr - 设置了百分比宽度(width="20%")的列
47+
// scaleList - 设置了百分比宽度(width="20%")的列
4848
// scaleMinArr - 设置了最小百分比宽度(min-width="20%")的列
49-
let { scaleList: scaleArr, scaleMinList: scaleMinArr } = columnStore
49+
const { scaleList, scaleMinList } = columnStore
5050

5151
// 1. 首先处理设置了最小像素宽度的列
5252
// 这些列的宽度不能小于设定的最小宽度
53-
pxMinArr.forEach((column) => {
54-
let minWidth = parseInt(column.minWidth)
53+
pxMinList.forEach((column) => {
54+
const minWidth = parseInt(column.minWidth)
5555
tableWidth += minWidth
5656
column.renderWidth = minWidth
5757
})
5858

5959
// 计算1%宽度对应的像素值,用于处理百分比宽度
60-
let meanWidth = remainWidth / 100
60+
const meanWidth = remainWidth / 100
6161

6262
// 2. 处理设置了最小百分比宽度的列
6363
// 将百分比转换为实际像素值
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)
6666
tableWidth += scaleWidth
6767
column.renderWidth = scaleWidth
6868
})
6969

7070
// 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)
7373
tableWidth += scaleWidth
7474
column.renderWidth = scaleWidth
7575
})
7676

7777
// 4. 处理设置了固定像素宽度的列
78-
pxArr.forEach((column) => {
79-
let width = parseInt(column.width)
78+
pxList.forEach((column) => {
79+
const width = parseInt(column.width)
8080
tableWidth += width
8181
column.renderWidth = width
8282
})
8383

8484
// 5. 最后处理用户手动调整过宽度的列
8585
// 这些列的宽度优先级最高
86-
resizeArr.forEach((column) => {
87-
let width = parseInt(column.resizeWidth)
86+
resizeList.forEach((column) => {
87+
const width = parseInt(column.resizeWidth)
8888
tableWidth += width
8989
column.renderWidth = width
9090
})
@@ -107,21 +107,21 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
107107
// 初始化表格宽度和平均宽度
108108
let { tableWidth, meanWidth } = initTableWidth({ remainWidth: bodyWidth, columnStore })
109109
// 获取最小像素宽度列、最小百分比宽度列和自适应列
110-
let { pxMinList: pxMinArr, scaleMinList: scaleMinArr, autoList: autoArr } = columnStore
110+
let { pxMinList, scaleMinList, autoList } = columnStore
111111

112112
// 计算剩余可用宽度
113113
const remainWidth = bodyWidth - tableWidth
114114
// 计算每列平均可分配宽度
115115
// 如果有剩余宽度,则平均分配给最小宽度列和自适应列
116116
// 如果没有剩余宽度,则为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
118118

119119
// 如果需要填充满容器
120120
if (fit) {
121121
// 如果有剩余宽度
122122
if (remainWidth > 0) {
123123
// 将剩余宽度平均分配给最小百分比宽度列和最小像素宽度列
124-
scaleMinArr.concat(pxMinArr).forEach((column) => {
124+
scaleMinList.concat(pxMinList).forEach((column) => {
125125
tableWidth += meanWidth
126126
column.renderWidth += meanWidth
127127
})
@@ -132,7 +132,7 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
132132
}
133133

134134
// 处理自适应列的宽度
135-
tableWidth = adaptive({ autoArr, meanWidth, minCellWidth, tableWidth, fit, bodyWidth })
135+
tableWidth = adaptive({ autoList, meanWidth, minCellWidth, tableWidth, fit, bodyWidth })
136136

137137
// 计算处理完自适应列后与容器的剩余空间
138138
const remainingSpace = bodyWidth - tableWidth
@@ -141,8 +141,8 @@ export const calcTableWidth = ({ bodyWidth, columnStore, fit, minCellWidth }) =>
141141
if (fit && remainingSpace > 0) {
142142
// 将剩余空间以1px为单位分配给最小百分比宽度列和最小像素宽度列
143143
// 分配数量不超过剩余空间大小
144-
scaleMinArr
145-
.concat(pxMinArr)
144+
scaleMinList
145+
.concat(pxMinList)
146146
.slice(0, remainingSpace)
147147
.forEach((column) => {
148148
tableWidth += 1

0 commit comments

Comments
 (0)