文件夹位置packages/table/src/store
一、核心架构
store/
├── index.ts → 主 Store(定义 mutations 和 commit)
├── watcher.ts → 核心状态管理(整合所有状态)
├── helper.ts → Store 工厂函数(createStore)
├── current.ts → 当前行功能模块
├── expand.ts → 展开行功能模块
└── tree.ts → 树形表格功能模块
// watcher.ts 中整合所有模块
const watcher = useWatcher<T>() // 核心状态
├─ useExpand() → expandStates
├─ useTree() → treeStates
└─ useCurrent() → currentData
// 合并所有状态
states: {
...基础状态,
...expandStates, // 来自 expand.ts
...treeStates, // 来自 tree.ts
...currentData, // 来自 current.ts
}
// index.ts 中使用 watcher
const watcher = useWatcher<T>()
// 添加 mutations 和 commit 方法
// helper.ts 中创建最终 store 对外提供createStore()
const store = useStore<T>() // 来自 index.ts
// 在table.vue里面用createStore()创建store
const store = createStore<Row>(table, props)
二、store维护的主要内容
- 数据管理
// 表格数据
data: Ref<T[]> // 显示的数据(经过筛选、排序后)
_data: Ref<T[]> // 原始数据
filteredData: Ref<T[] | null> // 筛选后的数据
- 列管理
// 列相关状态
_columns: Ref<TableColumnCtx<T>[]> // 原始列配置
originColumns: Ref<TableColumnCtx<T>[]> // 原始列(用于固定列)
columns: Ref<TableColumnCtx<T>[]> // 处理后的列
fixedColumns: Ref<TableColumnCtx<T>[]> // 左侧固定列
rightFixedColumns: Ref<TableColumnCtx<T>[]> // 右侧固定列
leafColumns: Ref<TableColumnCtx<T>[]> // 叶子列(无子列)
fixedLeafColumns: Ref<TableColumnCtx<T>[]> // 固定叶子列
rightFixedLeafColumns: Ref<TableColumnCtx<T>[]> // 右侧固定叶子列
leafColumnsLength: Ref<number> // 叶子列长度
- 行选择管理
// 选择相关状态
selection: Ref<T[]> // 选中的行数据
isAllSelected: Ref<boolean> // 是否全选
reserveSelection: Ref<boolean> // 是否保留选择(数据更新时)
selectOnIndeterminate: Ref<boolean> // 半选状态是否可选中
selectable: Ref<Function | null> // 行是否可选的回调函数
- 排序管理
// 排序相关状态
sortingColumn: Ref<TableColumnCtx<T> | null> // 当前排序列
sortProp: Ref<string | null> // 排序属性
sortOrder: Ref<string | number | null> // 排序顺序(asc/desc)
- 筛选管理
// 筛选相关状态
filters: Ref<StoreFilter> // 筛选条件 { columnId: [values] }
- 当前行管理 来自current.ts
// 当前行状态
currentRow: Ref<T | null> // 当前高亮的行
_currentRowKey: Ref<string | null> // 当前行的 key
- 展开行管理 来自expand.ts
// 展开行状态
expandRows: Ref<T[]> // 展开的行数据
defaultExpandAll: Ref<boolean> // 是否默认全部展开
- 树形表格管理 来自tree.ts
// 树形表格状态
expandRowKeys: Ref<string[]> // 展开的行 key 数组
treeData: Ref<Record<string, TreeData>> // 树形数据映射
indent: Ref<number> // 缩进距离
lazy: Ref<boolean> // 是否懒加载
lazyTreeNodeMap: Ref<Record<string, T[]>> // 懒加载节点映射
lazyColumnIdentifier: Ref<string> // 懒加载标识字段
childrenColumnName: Ref<string> // 子节点字段名
checkStrictly: Ref<boolean> // 是否严格模式(父子不关联)
- 其他状态
// 其他状态
rowKey: Ref<string | null> // 行的唯一标识字段
tableSize: Ref<string> // 表格尺寸(large/medium/small)
isComplex: Ref<boolean> // 是否为复杂表格(有固定列等)
hoverRow: Ref<T | null> // 当前悬停的行
updateOrderFns: (() => void)[] // 列顺序更新函数数组
二、总结
Store 是 Table 组件的状态管理中心,负责:
1. 数据:原始数据、显示数据、筛选数据
2. 列:列配置、固定列、列顺序
3. 交互:行选择、当前行、悬停行
4. 功能:排序、筛选、展开、树形结构
5. 状态:表格尺寸、复杂度、各种开关
所有表格相关的状态和操作都通过 store 统一管理,确保状态一致性和可追踪性。