-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(table): some features * docs(table): update comments * feat(table): code improve * test(unit): update snapshots * fix(table): dynamic data fro rowspan and colspan * style(table): remove unused variable
- Loading branch information
Showing
3 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ref, watch, Ref } from 'vue'; | ||
import { BaseTableCellParams, BaseTableCol, TableRowData, TableRowspanAndColspanFunc } from '../type'; | ||
|
||
export interface SkipSpansValue { | ||
colspan?: number; | ||
rowspan?: number; | ||
skipped?: boolean; | ||
} | ||
|
||
export default function useRowspanAndColspan( | ||
data: Ref<TableRowData[]>, | ||
columns: Ref<BaseTableCol<TableRowData>[]>, | ||
rowspanAndColspan: TableRowspanAndColspanFunc<TableRowData>, | ||
) { | ||
const skipSpansMap = ref(new Map<string, SkipSpansValue>()); | ||
|
||
// 计算单元格是否跳过渲染 | ||
const onTrRowspanOrColspan = (params: BaseTableCellParams<TableRowData>, skipSpansValue: SkipSpansValue) => { | ||
const { rowIndex, colIndex } = params; | ||
if (!skipSpansValue.rowspan && !skipSpansValue.colspan) return; | ||
const maxRowIndex = rowIndex + (skipSpansValue.rowspan || 1); | ||
const maxColIndex = colIndex + (skipSpansValue.colspan || 1); | ||
for (let i = rowIndex; i < maxRowIndex; i++) { | ||
for (let j = colIndex; j < maxColIndex; j++) { | ||
if (i !== rowIndex || j !== colIndex) { | ||
const cellKey = [i, j].join(); | ||
const state = skipSpansMap.value.get(cellKey) || {}; | ||
state.skipped = true; | ||
skipSpansMap.value.set(cellKey, state); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// 计算单元格是否需要设置 rowspan 和 colspan | ||
const updateSkipSpansMap = ( | ||
data: TableRowData[], | ||
columns: BaseTableCol<TableRowData>[], | ||
rowspanAndColspan: TableRowspanAndColspanFunc<TableRowData>, | ||
) => { | ||
if (!data || !rowspanAndColspan) return; | ||
for (let i = 0, len = data.length; i < len; i++) { | ||
const row = data[i]; | ||
for (let j = 0, colLen = columns.length; j < colLen; j++) { | ||
const params = { | ||
row, | ||
col: columns[j], | ||
rowIndex: i, | ||
colIndex: j, | ||
}; | ||
const cellKey = [i, j].join(); | ||
const state = skipSpansMap.value.get(cellKey) || {}; | ||
const o = rowspanAndColspan(params) || {}; | ||
if (o.rowspan > 1 || o.colspan > 1 || state.rowspan || state.colspan) { | ||
o.rowspan > 1 && (state.rowspan = o.rowspan); | ||
o.colspan > 1 && (state.colspan = o.colspan); | ||
skipSpansMap.value.set(cellKey, state); | ||
} | ||
onTrRowspanOrColspan?.(params, state); | ||
} | ||
} | ||
}; | ||
|
||
watch( | ||
() => [data.value, columns.value], | ||
() => { | ||
if (!data || !rowspanAndColspan) return; | ||
updateSkipSpansMap(data.value, columns.value, rowspanAndColspan); | ||
}, | ||
{ immediate: true }, | ||
); | ||
|
||
return { skipSpansMap, updateSkipSpansMap }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters