sidebar-pptx-generator/references/component-table.md
3.2 KB查看排版效果| 1 | --- |
| 2 | name: component-table |
| 3 | description: Table component - cells format, defaultTextStyle, zebra striping, border config |
| 4 | --- |
| 5 | |
| 6 | # Table 组件规范 |
| 7 | |
| 8 | Table 组件用于展示结构化数据、对比信息或规格参数。它支持简化的单元格格式,能够自动处理对齐和基础样式。 |
| 9 | |
| 10 | > 继承关系:`Table` 继承自 `Box`,完整兼容 `Box` 的 `style` 属性,可直接设置背景、边距、阴影等样式。 |
| 11 | |
| 12 | ## 核心属性 |
| 13 | |
| 14 | - **style**: 必填。通过 `style.width` / `style.height` 指定 Table 尺寸(必填),否则可能导致布局问题。`style.width` 支持百分比(例如 `width: '100%'`)。 |
| 15 | - 固定数值:`width={800} height={450}` |
| 16 | - 百分比:`width="100%" height="100%"` (相对于父容器)- 推荐用于 LLM 生成,避免溢出父容器 |
| 17 | - **cells**: 二维数组,支持字符串或对象格式。 |
| 18 | - **defaultTextStyle**: 必填。默认文本样式 (推荐: `{ fontSize: 16, textAlign: 'center' }`)。 |
| 19 | - **defaultCellStyle**: 必填。默认单元格样式(推荐包含边框、背景色等基础样式)。 |
| 20 | |
| 21 | ## 布局与样式 |
| 22 | |
| 23 | - **默认布局**: `display: 'block'`。 |
| 24 | - **单元格对象格式**: |
| 25 | - `text`: 文本内容。 |
| 26 | - `textStyle`: 字体大小、颜色、加粗等。 |
| 27 | - `cellStyle`: 背景色、边框等。 |
| 28 | - **注意事项**: |
| 29 | - 所有行的列数必须一致。 |
| 30 | - 单元格边框支持 `left`, `right`, `top`, `bottom` 独立设置。 |
| 31 | |
| 32 | ## 示例代码 |
| 33 | |
| 34 | ### 基础用法 (带样式与斑马纹) |
| 35 | |
| 36 | ```jsx |
| 37 | // 百分比尺寸 - 推荐用于 LLM 生成,避免溢出父容器 |
| 38 | <Table |
| 39 | style={{ width: '100%', height: '100%' }} |
| 40 | defaultTextStyle={{ fontSize: 16, textAlign: 'center', color: '#222' }} |
| 41 | defaultCellStyle={{ |
| 42 | border: { |
| 43 | left: { width: 1, color: '#e5e5e5' }, |
| 44 | right: { width: 1, color: '#e5e5e5' }, |
| 45 | top: { width: 1, color: '#e5e5e5' }, |
| 46 | bottom: { width: 1, color: '#e5e5e5' }, |
| 47 | }, |
| 48 | }} |
| 49 | cells={[ |
| 50 | // 表头 |
| 51 | [ |
| 52 | { text: '功能', textStyle: { bold: true, color: '#fff' }, cellStyle: { background: { color: '#333' } } }, |
| 53 | { text: '基础版', textStyle: { bold: true, color: '#fff' }, cellStyle: { background: { color: '#333' } } }, |
| 54 | { text: '专业版', textStyle: { bold: true, color: '#fff' }, cellStyle: { background: { color: '#333' } } }, |
| 55 | ], |
| 56 | // 数据行 (斑马纹示例) |
| 57 | ['用户数', '10人', { text: '无限制', textStyle: { color: '#007aff', bold: true } }], |
| 58 | [ |
| 59 | { text: '存储', cellStyle: { background: { color: '#f9f9f9' } } }, |
| 60 | { text: '5GB', cellStyle: { background: { color: '#f9f9f9' } } }, |
| 61 | { text: '100GB', cellStyle: { background: { color: '#f9f9f9' } } }, |
| 62 | ], |
| 63 | ]} |
| 64 | /> |
| 65 | |
| 66 | // 固定尺寸 |
| 67 | <Table |
| 68 | style={{ width: 800, height: 450 }} |
| 69 | // ... 其他属性 |
| 70 | /> |
| 71 | ``` |
| 72 | |
| 73 | ## 最佳实践 |
| 74 | |
| 75 | - **明确宽高**: 始终通过 `style` 为 Table 设置 `width` 和 `height`。推荐使用百分比尺寸(如 `style={{ width: '100%', height: '100%' }}`),避免溢出父容器。 |
| 76 | - **视觉区分**: 为表头设置深色背景,为数据行设置交替背景色(斑马纹)以增强可读性。 |
| 77 | - **对齐一致**: 数值类信息建议保持居中或右对齐。 |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

