iter(g88): 数据表支持导出 CSV

This commit is contained in:
HIsheR
2026-10-26 13:08:25 +08:00
parent fa07101f76
commit 9a7c46a3c6
12 changed files with 110 additions and 8 deletions
@@ -154,4 +154,22 @@ assert(
'reset 回到默认态(排序/筛选清空,分页回 1/20)',
);
// getAllRows:过筛选 + 排序但跳过分页,供导出用
const all = createTable({
data: users,
columns: [{ key: 'name', header: 'Name' }, { key: 'city', header: 'City' }],
rowKey: 'id',
plugins: [filterPlugin(), sortPlugin(), paginationPlugin()],
initialState: { pagination: { page: 1, pageSize: 2 } },
});
assert(all.getVisibleRows().rows.length === 2, 'getVisibleRows 受分页约束只给当页');
all.dispatch({ type: 'setFilter', key: 'city', value: 'Beijing' });
assert(all.getAllRows().rows.length === 2 && all.getVisibleRows().rows.length === 2, 'getAllRows 跳过分页但与筛选一致(Beijing 2 行)');
all.dispatch({ type: 'toggleSort', key: 'age' });
assert(
JSON.stringify(all.getAllRows().rows.map((r) => r.id)) === JSON.stringify([4, 2]),
'getAllRows 仍按排序规则排(age 升序 4→2)',
);
assert(all.getPageInfo().totalPages === 1, 'getAllRows 不影响分页快照(2 行 / 每页 2 = 1 页)');
console.log('SMOKE OK');
+5 -2
View File
@@ -80,12 +80,14 @@ export function createTable<T>(
return cachedBase;
}
/*** 可见行模型:依序过插件 derive(filter → sort → paginate) ***/
function getVisibleRows(): RowModel<T> {
/*** 可见行模型:依序过插件 derive(filter → sort → paginate);paged=false 时跳过分页阶段取全量派生行 ***/
function getVisibleRows(paged = true): RowModel<T> {
ctx.paged = paged; // 由插件自行判断是否裁剪,核心不认插件细节
let model = baseModel();
for (const p of plugins) {
if (p.derive) model = p.derive(model, state, ctx);
}
ctx.paged = true; // 还原缺省,避免影响后续调用
return model;
}
@@ -94,6 +96,7 @@ export function createTable<T>(
dispatch,
subscribe,
getVisibleRows,
getAllRows: () => getVisibleRows(false), // 筛选 + 排序后、未分页的全量行(导出等场景)
getColumns: () => columns,
getCellValue,
isRowSelected: (key) => state.selection.includes(key),
@@ -1,5 +1,6 @@
import type {
Action,
DeriveContext,
RowModel,
TableInstance,
TablePlugin,
@@ -32,7 +33,8 @@ export function paginationPlugin<T = unknown>(): TablePlugin<T> {
return state;
}
},
derive(model: RowModel<T>, state: TableState): RowModel<T> {
derive(model: RowModel<T>, state: TableState, ctx: DeriveContext<T>): RowModel<T> {
if (ctx.paged === false) return model; // 不分页:原样放行上游模型(导出全量行)
if (model === lastInput && state.pagination === lastPage && cached) return cached;
const { page, pageSize } = state.pagination;
const totalPages = Math.max(1, Math.ceil(model.total / pageSize));
+2
View File
@@ -61,6 +61,7 @@ export interface RowModel<T = unknown> {
/*** 派生上下文:核心注入给插件的取值能力 ***/
export interface DeriveContext<T = unknown> {
paged?: boolean; // 是否分页裁剪;false 时 pagination 插件原样放行(导出全量行用)
getCellValue(row: T, key: string): unknown; // 单元格取值(走列 accessor)
getKey(row: T): RowKey; // 行标识取值
}
@@ -89,6 +90,7 @@ export interface TableInstance<T = unknown> {
dispatch(action: Action): void; // 派发动作,唯一变更入口
subscribe(fn: () => void): () => void; // 订阅变更,返回取消函数
getVisibleRows(): RowModel<T>; // 可见行模型(过完整管线)
getAllRows(): RowModel<T>; // 筛选 + 排序后、未分页的全量行(导出用)
getColumns(): ColumnDef<T>[]; // 列定义
getCellValue(row: T, key: string): unknown; // 单元格取值
isRowSelected(key: RowKey): boolean; // 行是否已选