Skip to Content
文档

表格

用于以表格形式显示数据。

SourceStorybookRecipe
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

用法

¥Usage

import { Table } from "@chakra-ui/react"
<Table.Root>
  <Table.Caption />
  <Table.Header>
    <Table.Row>
      <Table.ColumnHeader />
    </Table.Row>
  </Table.Header>
  <Table.Body>
    <Table.Row>
      <Table.Cell />
    </Table.Row>
  </Table.Body>
  <Table.Footer>
    <Table.Row>
      <Table.Cell />
    </Table.Row>
  </Table.Footer>
</Table.Root>

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改表格的大小。

¥Use the size prop to change the size of the table.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

变量

¥Variants

使用 variant 属性更改表格的外观。

¥Use the variant prop to change the appearance of the table.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

条纹

¥Striped

使用 striped 属性为表格添加斑马线。

¥Use the striped prop to add zebra-stripes to the table.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

标题

¥Caption

使用 Table.Caption 组件为表格添加标题。

¥Use the Table.Caption component to add a caption to the table.

Product inventory and pricing information
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

标题顶部

¥Caption Top

使用 Table.Caption 上的 side 属性将标题定位到表格顶部。

¥Use the side prop on Table.Caption to position the caption at the top of the table.

Product inventory and pricing information
ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

列边框

¥Column Border

使用 showColumnBorder 属性在列之间添加边框。

¥Use the showColumnBorder prop to add borders between columns.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

溢出

¥Overflow

渲染 Table.ScrollArea 组件以启用水平滚动。

¥Render the Table.ScrollArea component to enable horizontal scrolling.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

贴在标题

¥Sticky Header

使用 stickyHeader 属性使表格标题固定。

¥Use the stickyHeader prop to make the table header sticky.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

贴在列

¥Sticky Column

以下是使用 data-sticky 属性使表格列在水平滚动期间保持粘性的示例。

¥Here's an example what uses data-sticky attributes to make table columns sticky during horizontal scrolling.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

悬停时高亮

¥Highlight on Hover

使用 interactive 属性在鼠标悬停时高亮行。

¥Use the interactive prop to highlight rows on hover.

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

分页

¥Pagination

以下是如何组合带有分页的表格的示例。

¥Here's an example of how to compose a table with pagination.

Products

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

选择

¥Selection

以下是如何在表格中添加带有复选框的行选择的示例。

¥Here's an example of how to add row selection with checkboxes to your table.

ProductCategoryPrice
LaptopElectronics$999.99
Coffee MakerHome Appliances$49.99
Desk ChairFurniture$150
SmartphoneElectronics$799.99
HeadphonesAccessories$199.99

操作栏

¥Action Bar

以下是如何组合带有操作栏和复选框的表格的示例。这对于显示选定表格行的操作非常有用。

¥Here's an example of how to compose a table with an action bar and checkboxes. This is useful for showing actions for selected table rows.

ProductCategoryPrice
LaptopElectronics$999.99
Coffee MakerHome Appliances$49.99
Desk ChairFurniture$150
SmartphoneElectronics$799.99
HeadphonesAccessories$199.99

列组

¥Column Group

使用 Table.ColumnGroup 组件通过 html colgroup 元素分配列宽。

¥Use the Table.ColumnGroup component to distribute the column widths using the html colgroup element.

warning

此组件唯一可用的 prop 是 htmlWidth

¥The only prop that works for this component is htmlWidth

ProductCategoryPrice
LaptopElectronics999.99
Coffee MakerHome Appliances49.99
Desk ChairFurniture150
SmartphoneElectronics799.99
HeadphonesAccessories199.99

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

variant 'line'
'line' | 'outline'

The variant of the component

size 'md'
'sm' | 'md' | 'lg'

The size of the component

native
boolean | undefined

If `true`, the table will style its descendants with nested selectors

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

interactive
'true' | 'false'

The interactive of the component

stickyHeader
'true' | 'false'

The stickyHeader of the component

striped
'true' | 'false'

The striped of the component

showColumnBorder
'true' | 'false'

The showColumnBorder of the component

Previous

状态

Next

标签