Skip to content

Commit 8f76184

Browse files
authoredMar 30, 2022
fix(Table): 修复table fix 在表头分组中fixed固定位置错误问题 (#727)
1 parent 3bf9325 commit 8f76184

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed
 

‎packages/react-table/README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ const columns = [
8080
style: { color: 'red' },
8181
key: 'name',
8282
children: [
83+
{
84+
title: '英文名字',
85+
width: 100,
86+
key: 'name_en',
87+
},
8388
{
8489
title: '中文名字',
8590
key: 'cnname',
@@ -90,7 +95,7 @@ const columns = [
9095
key: 'firstname',
9196
children:[
9297
{ title: '', key: 'name1', width: 80 },
93-
{ title: '', key: 'name2', width: 80 },
98+
{ title: '', key: 'name2', width: 80 },
9499
]
95100
}, {
96101
title: '拼音',
@@ -101,14 +106,11 @@ const columns = [
101106
],
102107
},
103108
],
104-
}, {
105-
title: '英文名字',
106-
width: 100,
107-
key: 'name_en',
108-
},
109+
},
109110
]
110111
}, {
111112
title: '其它',
113+
key: 'other',
112114
children:[
113115
{ title: '生日', key: 'birthday', width: 150 },
114116
{ title: '职业', key: 'job', width: 150 },

‎packages/react-table/src/TableTr.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
115115
}
116116
if (keyName.fixed) {
117117
if (keyName.fixed === 'right') {
118-
objs.className = `${objs.className} ${prefixCls}-fixed-right`;
118+
objs.className = `${prefixCls}-fixed-right`;
119119
} else {
120-
objs.className = `${objs.className} ${prefixCls}-fixed-true`;
120+
objs.className = `${prefixCls}-fixed-true`;
121121
}
122122
}
123123
return (
@@ -134,9 +134,14 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
134134
</span>
135135
}
136136
key={colNum}
137-
className={`${prefixCls}-tr-children-${keyName.align || 'left'} ${keyName.className || ''} ${
138-
objs.className || ''
139-
}`}
137+
className={[
138+
prefixCls + '-tr-children-' + (keyName.align || 'left'),
139+
keyName.className,
140+
objs.className,
141+
]
142+
.filter((it) => it)
143+
.join(' ')
144+
.trim()}
140145
onClick={(evn) => onCell(trData, { rowNum, colNum, keyName: keyName.key! }, evn)}
141146
/>
142147
);

‎packages/react-table/src/index.tsx

+28-35
Original file line numberDiff line numberDiff line change
@@ -118,54 +118,46 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
118118
setLocationWidth(computed());
119119
}
120120
};
121-
const deepClumnsLocation = (
122-
params: Array<TableColumns<T> | number>,
123-
fistIndex: number,
124-
leftOrRight: 'left' | 'right',
125-
isReverse: boolean,
126-
) => {
121+
const deepClumnsLocation = (params: Array<TableColumns<T> | number>, fistIndex: number) => {
127122
let initialValue = 0,
128-
lastIndex = isReverse ? 0 : params.length - 1,
123+
headerIndex = 0,
129124
deepParams: Array<TableColumns<T> | number> = [];
130-
params.forEach(() => {
131-
let abs = Math.abs(lastIndex);
132-
const i = `${fistIndex}${abs}`;
133-
if (isReverse) {
134-
lastIndex += 1;
135-
} else {
136-
lastIndex -= 1;
137-
}
138-
if (typeof params[abs] === 'number') {
139-
initialValue = (params[abs] as number) + initialValue;
140-
deepParams.push(params[abs]);
125+
params.forEach((_, index) => {
126+
const i = `${fistIndex}${headerIndex}`;
127+
if (typeof params[index] === 'number') {
128+
initialValue = (params[index] as number) + initialValue;
129+
deepParams.push(params[index]);
141130
return;
142131
}
143132
if (finalLocationWidth.current[i]) {
144-
finalLocationWidth.current[i][leftOrRight] = initialValue;
133+
finalLocationWidth.current[i].left = initialValue;
145134
initialValue = finalLocationWidth.current[i].width + initialValue;
135+
if (Array.isArray((params[index] as TableColumns<T>).children)) {
136+
deepParams.push(...(params[index] as TableColumns<T>).children!);
137+
} else {
138+
deepParams.push(finalLocationWidth.current[i].width);
139+
}
146140
}
147-
if (Array.isArray((params[abs] as TableColumns<T>).children)) {
148-
deepParams.push(...(params[abs] as TableColumns<T>).children!);
149-
return;
141+
headerIndex += 1;
142+
});
143+
(initialValue = 0), (headerIndex = header[fistIndex].length - 1);
144+
for (let index = params.length - 1; index >= 0; index--) {
145+
const i = `${fistIndex}${headerIndex}`;
146+
if (typeof params[index] === 'number') {
147+
initialValue = (params[index] as number) + initialValue;
148+
continue;
150149
}
151150
if (finalLocationWidth.current[i]) {
152-
deepParams.push(finalLocationWidth.current[i].width);
153-
} else {
154-
const parent = header.find((it) => it.find((it) => it.key === (params[abs] as TableColumns<T>).key)) || [];
155-
const sub = parent.findIndex((it) => it.key === (params[abs] as TableColumns<T>).key);
156-
if (finalLocationWidth.current[`${i[0]}${sub}`]) {
157-
// 合并单元格
158-
deepParams.push(finalLocationWidth.current[`${i[0]}${sub}`].width);
159-
}
151+
finalLocationWidth.current[i].right = initialValue;
152+
initialValue = finalLocationWidth.current[i].width + initialValue;
160153
}
161-
});
162-
if (deepParams.filter((it) => typeof it !== 'number').length)
163-
deepClumnsLocation(deepParams, fistIndex + 1, leftOrRight, isReverse);
154+
headerIndex -= 1;
155+
}
156+
if (deepParams.filter((it) => typeof it !== 'number').length) deepClumnsLocation(deepParams, fistIndex + 1);
164157
};
165158

166159
const computed = () => {
167-
deepClumnsLocation(columns, 0, 'left', true);
168-
deepClumnsLocation(columns, 0, 'right', false);
160+
deepClumnsLocation(columns, 0);
169161
return finalLocationWidth.current;
170162
};
171163
useEffect(() => {
@@ -296,6 +288,7 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
296288
}, [scroll]);
297289
const cls = [prefixCls, className, bordered ? `${prefixCls}-bordered` : null].filter(Boolean).join(' ').trim();
298290
const { header, render, ellipsis } = getLevelItems(self.selfColumns);
291+
299292
return (
300293
<React.Fragment>
301294
<div className={cls} {...other} style={{ ...other.style, ...style.div }}>

0 commit comments

Comments
 (0)
Please sign in to comment.