|
| 1 | +# 表格组件高级功能 (Advanced Table Features) |
| 2 | + |
| 3 | +本文档描述 ObjectQL UI 库中表格组件的高级功能实现。 |
| 4 | + |
| 5 | +This document describes the advanced features implementation in ObjectQL UI table components. |
| 6 | + |
| 7 | +## 功能概览 (Features Overview) |
| 8 | + |
| 9 | +### ✅ 已实现的功能 (Implemented Features) |
| 10 | + |
| 11 | +1. **Grouping (分组)** - 按列分组数据显示 |
| 12 | +2. **Inline Editing (内联编辑)** - Grid 中直接编辑单元格 |
| 13 | +3. **Bulk Operations (批量操作)** - 批量删除、批量更新 |
| 14 | +4. **Copy/Paste (复制粘贴)** - 复制选中行到剪贴板 |
| 15 | +5. **Drag & Drop (拖拽排序)** - 字段拖拽排序 |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 1. Grouping (分组) |
| 20 | + |
| 21 | +### 功能描述 (Feature Description) |
| 22 | + |
| 23 | +数据分组功能允许用户按照指定列对表格数据进行分组展示,每个分组可以展开或折叠。 |
| 24 | + |
| 25 | +The grouping feature allows users to group table data by a specified column, with each group being expandable or collapsible. |
| 26 | + |
| 27 | +### 使用方法 (Usage) |
| 28 | + |
| 29 | +#### GridView 组件 |
| 30 | + |
| 31 | +```tsx |
| 32 | +import { GridView } from '@objectql/ui' |
| 33 | + |
| 34 | +<GridView |
| 35 | + columns={columns} |
| 36 | + data={data} |
| 37 | + enableGrouping={true} |
| 38 | + groupByColumn="department" // 按部门分组 |
| 39 | +/> |
| 40 | +``` |
| 41 | + |
| 42 | +#### DataTable 组件 |
| 43 | + |
| 44 | +```tsx |
| 45 | +import { DataTable } from '@objectql/ui' |
| 46 | + |
| 47 | +<DataTable |
| 48 | + columns={columns} |
| 49 | + data={data} |
| 50 | + enableGrouping={true} |
| 51 | + groupByColumn="status" // 按状态分组 |
| 52 | +/> |
| 53 | +``` |
| 54 | + |
| 55 | +### 特性 (Features) |
| 56 | + |
| 57 | +- ✅ 按任意列分组 |
| 58 | +- ✅ 展开/折叠分组 |
| 59 | +- ✅ 显示每组的记录数量 |
| 60 | +- ✅ 与行选择功能兼容 |
| 61 | +- ✅ 与其他功能组合使用 |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## 2. Inline Editing (内联编辑) |
| 66 | + |
| 67 | +### 功能描述 (Feature Description) |
| 68 | + |
| 69 | +内联编辑允许用户直接在表格单元格中编辑数据,无需打开单独的编辑表单。 |
| 70 | + |
| 71 | +Inline editing allows users to edit data directly in table cells without opening a separate edit form. |
| 72 | + |
| 73 | +### 使用方法 (Usage) |
| 74 | + |
| 75 | +```tsx |
| 76 | +const columns = [ |
| 77 | + { |
| 78 | + id: 'name', |
| 79 | + label: '名称', |
| 80 | + type: 'text', |
| 81 | + editable: true // 启用编辑 |
| 82 | + }, |
| 83 | + { |
| 84 | + id: 'budget', |
| 85 | + label: '预算', |
| 86 | + type: 'number', |
| 87 | + editable: true |
| 88 | + }, |
| 89 | + { |
| 90 | + id: 'startDate', |
| 91 | + label: '开始日期', |
| 92 | + type: 'date', |
| 93 | + editable: true |
| 94 | + }, |
| 95 | +] |
| 96 | + |
| 97 | +<GridView |
| 98 | + columns={columns} |
| 99 | + data={data} |
| 100 | + onCellEdit={(rowIndex, columnId, value) => { |
| 101 | + console.log('Cell edited:', { rowIndex, columnId, value }) |
| 102 | + // 更新数据 |
| 103 | + }} |
| 104 | +/> |
| 105 | +``` |
| 106 | + |
| 107 | +### 支持的字段类型 (Supported Field Types) |
| 108 | + |
| 109 | +- ✅ **text** - 文本输入 |
| 110 | +- ✅ **number** - 数字输入 |
| 111 | +- ✅ **date** - 日期选择 |
| 112 | + |
| 113 | +### 键盘快捷键 (Keyboard Shortcuts) |
| 114 | + |
| 115 | +- **Enter** - 保存编辑 |
| 116 | +- **Escape** - 取消编辑 |
| 117 | + |
| 118 | +### 注意事项 (Notes) |
| 119 | + |
| 120 | +- Badge、Select 和 Boolean 类型字段不支持内联编辑,需要通过行点击在表单中编辑 |
| 121 | +- Badge, Select, and Boolean type fields don't support inline editing; they should be edited in a form via row click |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 3. Bulk Operations (批量操作) |
| 126 | + |
| 127 | +### 功能描述 (Feature Description) |
| 128 | + |
| 129 | +批量操作允许用户选择多行并对它们执行批量删除等操作。 |
| 130 | + |
| 131 | +Bulk operations allow users to select multiple rows and perform actions like bulk delete on them. |
| 132 | + |
| 133 | +### 使用方法 (Usage) |
| 134 | + |
| 135 | +#### GridView 组件 |
| 136 | + |
| 137 | +```tsx |
| 138 | +<GridView |
| 139 | + columns={columns} |
| 140 | + data={data} |
| 141 | + enableRowSelection={true} |
| 142 | + onBulkDelete={(rows) => { |
| 143 | + console.log('Deleting rows:', rows) |
| 144 | + // 执行批量删除逻辑 |
| 145 | + }} |
| 146 | +/> |
| 147 | +``` |
| 148 | + |
| 149 | +#### DataTable 组件 |
| 150 | + |
| 151 | +```tsx |
| 152 | +<DataTable |
| 153 | + columns={columns} |
| 154 | + data={data} |
| 155 | + enableRowSelection={true} |
| 156 | + onBulkDelete={(rows) => { |
| 157 | + console.log('Deleting rows:', rows) |
| 158 | + // 执行批量删除逻辑 |
| 159 | + }} |
| 160 | + onBulkUpdate={(rows, updates) => { |
| 161 | + console.log('Updating rows:', rows, updates) |
| 162 | + // 执行批量更新逻辑 |
| 163 | + }} |
| 164 | +/> |
| 165 | +``` |
| 166 | + |
| 167 | +### 特性 (Features) |
| 168 | + |
| 169 | +- ✅ 全选/取消全选 |
| 170 | +- ✅ 单独选择行 |
| 171 | +- ✅ 批量删除操作 |
| 172 | +- ✅ 批量更新操作(DataTable) |
| 173 | +- ✅ 显示选中行数量 |
| 174 | +- ✅ 批量操作工具栏 |
| 175 | + |
| 176 | +### 批量操作工具栏 (Bulk Actions Toolbar) |
| 177 | + |
| 178 | +当有行被选中时,会显示批量操作工具栏,包含: |
| 179 | +- 选中行数量显示 |
| 180 | +- 复制按钮(如果启用) |
| 181 | +- 删除按钮 |
| 182 | +- 清除选择按钮 |
| 183 | + |
| 184 | +When rows are selected, a bulk actions toolbar appears with: |
| 185 | +- Selected row count |
| 186 | +- Copy button (if enabled) |
| 187 | +- Delete button |
| 188 | +- Clear selection button |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## 4. Copy/Paste (复制粘贴) |
| 193 | + |
| 194 | +### 功能描述 (Feature Description) |
| 195 | + |
| 196 | +复制粘贴功能允许用户将选中的行数据复制到剪贴板,格式为 TSV(制表符分隔值),可以粘贴到 Excel 等应用程序。 |
| 197 | + |
| 198 | +The copy/paste feature allows users to copy selected row data to clipboard in TSV (Tab-Separated Values) format, which can be pasted into applications like Excel. |
| 199 | + |
| 200 | +### 使用方法 (Usage) |
| 201 | + |
| 202 | +```tsx |
| 203 | +<GridView |
| 204 | + columns={columns} |
| 205 | + data={data} |
| 206 | + enableRowSelection={true} |
| 207 | + enableCopyPaste={true} |
| 208 | +/> |
| 209 | +``` |
| 210 | + |
| 211 | +### 复制方式 (Copy Methods) |
| 212 | + |
| 213 | +1. **按钮复制** - 点击批量操作工具栏中的"Copy"按钮 |
| 214 | +2. **键盘快捷键** - 选中行后按 `Ctrl+C` (Windows/Linux) 或 `Cmd+C` (Mac) |
| 215 | + |
| 216 | +### 数据格式 (Data Format) |
| 217 | + |
| 218 | +复制的数据包含: |
| 219 | +- 第一行:列标题(制表符分隔) |
| 220 | +- 后续行:数据行(制表符分隔) |
| 221 | + |
| 222 | +Copied data includes: |
| 223 | +- First row: Column headers (tab-separated) |
| 224 | +- Following rows: Data rows (tab-separated) |
| 225 | + |
| 226 | +### 示例 (Example) |
| 227 | + |
| 228 | +``` |
| 229 | +Name Department Status Priority |
| 230 | +Website Redesign Engineering active high |
| 231 | +Mobile App Development Engineering active high |
| 232 | +``` |
| 233 | + |
| 234 | +--- |
| 235 | + |
| 236 | +## 5. Drag & Drop (拖拽排序) |
| 237 | + |
| 238 | +### 功能描述 (Feature Description) |
| 239 | + |
| 240 | +拖拽排序功能允许用户通过拖拽列标题来重新排列列的顺序。 |
| 241 | + |
| 242 | +The drag & drop feature allows users to reorder columns by dragging column headers. |
| 243 | + |
| 244 | +### 使用方法 (Usage) |
| 245 | + |
| 246 | +```tsx |
| 247 | +const [columns, setColumns] = React.useState(initialColumns) |
| 248 | + |
| 249 | +<GridView |
| 250 | + columns={columns} |
| 251 | + data={data} |
| 252 | + enableColumnDragDrop={true} |
| 253 | + onColumnReorder={(newColumns) => { |
| 254 | + console.log('Columns reordered:', newColumns.map(c => c.id)) |
| 255 | + setColumns(newColumns) |
| 256 | + }} |
| 257 | +/> |
| 258 | +``` |
| 259 | + |
| 260 | +### 特性 (Features) |
| 261 | + |
| 262 | +- ✅ 拖拽列标题重新排序 |
| 263 | +- ✅ 拖拽时的视觉反馈(高亮目标位置) |
| 264 | +- ✅ 拖拽图标指示 |
| 265 | +- ✅ 保持列顺序状态 |
| 266 | + |
| 267 | +### 视觉指示 (Visual Indicators) |
| 268 | + |
| 269 | +- 列标题显示拖拽图标(⋮⋮) |
| 270 | +- 拖拽时目标位置高亮显示 |
| 271 | +- 鼠标指针变为移动样式 |
| 272 | + |
| 273 | +--- |
| 274 | + |
| 275 | +## 组合使用 (Combined Usage) |
| 276 | + |
| 277 | +所有功能都可以组合使用,创建功能强大的表格界面: |
| 278 | + |
| 279 | +All features can be combined to create powerful table interfaces: |
| 280 | + |
| 281 | +```tsx |
| 282 | +<GridView |
| 283 | + columns={columns} |
| 284 | + data={data} |
| 285 | + // 启用所有功能 |
| 286 | + enableRowSelection={true} |
| 287 | + enableGrouping={true} |
| 288 | + groupByColumn="department" |
| 289 | + enableCopyPaste={true} |
| 290 | + enableColumnDragDrop={true} |
| 291 | + // 处理函数 |
| 292 | + onCellEdit={handleCellEdit} |
| 293 | + onBulkDelete={handleBulkDelete} |
| 294 | + onColumnReorder={handleColumnReorder} |
| 295 | +/> |
| 296 | +``` |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +## API 参考 (API Reference) |
| 301 | + |
| 302 | +### GridView Props |
| 303 | + |
| 304 | +| Prop | Type | Default | Description | |
| 305 | +|------|------|---------|-------------| |
| 306 | +| `enableRowSelection` | `boolean` | `false` | 启用行选择 / Enable row selection | |
| 307 | +| `enableGrouping` | `boolean` | `false` | 启用分组 / Enable grouping | |
| 308 | +| `groupByColumn` | `string` | - | 分组列 ID / Group by column ID | |
| 309 | +| `enableCopyPaste` | `boolean` | `false` | 启用复制粘贴 / Enable copy/paste | |
| 310 | +| `enableColumnDragDrop` | `boolean` | `false` | 启用列拖拽 / Enable column drag & drop | |
| 311 | +| `onBulkDelete` | `(rows: any[]) => void` | - | 批量删除回调 / Bulk delete callback | |
| 312 | +| `onColumnReorder` | `(columns: Column[]) => void` | - | 列重排回调 / Column reorder callback | |
| 313 | + |
| 314 | +### DataTable Props |
| 315 | + |
| 316 | +| Prop | Type | Default | Description | |
| 317 | +|------|------|---------|-------------| |
| 318 | +| `enableRowSelection` | `boolean` | `false` | 启用行选择 / Enable row selection | |
| 319 | +| `enableGrouping` | `boolean` | `false` | 启用分组 / Enable grouping | |
| 320 | +| `groupByColumn` | `string` | - | 分组列 ID / Group by column ID | |
| 321 | +| `enableCopyPaste` | `boolean` | `false` | 启用复制粘贴 / Enable copy/paste | |
| 322 | +| `onBulkDelete` | `(rows: TData[]) => void` | - | 批量删除回调 / Bulk delete callback | |
| 323 | +| `onBulkUpdate` | `(rows: TData[], updates: Partial<TData>) => void` | - | 批量更新回调 / Bulk update callback | |
| 324 | + |
| 325 | +--- |
| 326 | + |
| 327 | +## 示例 (Examples) |
| 328 | + |
| 329 | +完整示例请参考: |
| 330 | +- `examples/advanced-table-features.tsx` - 所有功能的完整演示 |
| 331 | + |
| 332 | +For complete examples, see: |
| 333 | +- `examples/advanced-table-features.tsx` - Full demonstration of all features |
| 334 | + |
| 335 | +--- |
| 336 | + |
| 337 | +## 浏览器支持 (Browser Support) |
| 338 | + |
| 339 | +- Chrome 90+ |
| 340 | +- Firefox 88+ |
| 341 | +- Safari 14+ |
| 342 | +- Edge 90+ |
| 343 | + |
| 344 | +--- |
| 345 | + |
| 346 | +## 技术栈 (Tech Stack) |
| 347 | + |
| 348 | +- React 18 |
| 349 | +- TanStack Table v8 |
| 350 | +- TypeScript 5 |
| 351 | +- Tailwind CSS |
| 352 | +- Lucide React Icons |
| 353 | + |
| 354 | +--- |
| 355 | + |
| 356 | +## 性能考虑 (Performance Considerations) |
| 357 | + |
| 358 | +1. **分组** - 使用 `useMemo` 优化分组计算 |
| 359 | +2. **行选择** - 使用 `Set` 数据结构提高性能 |
| 360 | +3. **拖拽** - 仅在必要时更新状态 |
| 361 | + |
| 362 | +1. **Grouping** - Uses `useMemo` to optimize grouping calculations |
| 363 | +2. **Row Selection** - Uses `Set` data structure for better performance |
| 364 | +3. **Drag & Drop** - Updates state only when necessary |
| 365 | + |
| 366 | +--- |
| 367 | + |
| 368 | +## 未来改进 (Future Enhancements) |
| 369 | + |
| 370 | +- [ ] 多列分组 |
| 371 | +- [ ] 自定义分组聚合函数 |
| 372 | +- [ ] 持久化列顺序到 localStorage |
| 373 | +- [ ] 批量编辑功能 |
| 374 | +- [ ] 拖拽行重新排序 |
| 375 | + |
| 376 | +- [ ] Multi-column grouping |
| 377 | +- [ ] Custom group aggregation functions |
| 378 | +- [ ] Persist column order to localStorage |
| 379 | +- [ ] Batch edit functionality |
| 380 | +- [ ] Drag & drop row reordering |
0 commit comments