ProComponents, templates & AI tooling
HeroUI
27.7k

Table 表格更新

表格以行和列展示结构化数据,支持排序、选择、列宽调整与无限滚动。

引入

import { Table } from '@heroui/react';

用法

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com
import {Table} from "@heroui/react";

export function Basic() {
  return (
    <Table>

组件结构

引入 Table 组件,并通过点语法访问各部分。

import { Table } from '@heroui/react';

export default () => (
  <Table>
    <Table.ScrollContainer>
      <Table.Content aria-label="Example table">
        <Table.Header>
          <Table.Column>Name</Table.Column>
          <Table.Column>Role</Table.Column>
        </Table.Header>
        <Table.Body>
          <Table.Row>
            <Table.Cell>Kate Moore</Table.Cell>
            <Table.Cell>CEO</Table.Cell>
          </Table.Row>
        </Table.Body>
      </Table.Content>
    </Table.ScrollContainer>
    <Table.Footer>{/* Optional footer content */}</Table.Footer>
  </Table>
);

次要变体

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com
import {Table} from "@heroui/react";

export function SecondaryVariant() {
  return (
    <Table variant="secondary">

排序

Table.Column 上设置 allowsSorting 可将列设为可排序。在 Table.Content 上使用 sortDescriptoronSortChange 管理排序状态。

姓名角色状态邮箱
Emily Davis产品经理未激活emily@acme.com
John Smith首席技术官在职john@acme.com
Kate Moore首席执行官在职kate@acme.com
Michael Brown首席财务官在职michael@acme.com
Sara Johnson首席营销官休假sara@acme.com
"use client";

import type {SortDescriptor} from "@heroui/react";

import {Table, cn} from "@heroui/react";

选择

Table.Content 上设置 selectionMode 以启用行选择。全选与每行复选框可使用带 slot="selection"Checkbox

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com

已选:

"use client";

import type {Selection} from "@heroui/react";

import {Checkbox, Table} from "@heroui/react";

自定义单元格

员工 ID成员角色状态操作
#1234567
ED
Emily Davisemily@acme.com
产品经理未激活
#5273849
JS
John Smithjohn@acme.com
首席技术官在职
#4586932
KM
Kate Moorekate@acme.com
首席执行官在职
#8293746
MB
Michael Brownmichael@acme.com
首席财务官在职
#7492836
SJ
Sara Johnsonsara@acme.com
首席营销官休假
"use client";

import type {Selection, SortDescriptor} from "@heroui/react";

import {Avatar, Button, Checkbox, Chip, Table, cn} from "@heroui/react";

可展开行

行可以嵌套以展示层级数据。使用 treeColumn 指定列,并在该列单元格内渲染带 slot="chevron"Button,以便用户展开/收起行。使用 expandedKeys 控制哪些行处于展开状态。

姓名类型修改日期
文档文件夹10/20/2025
"use client";

import type {Selection} from "@heroui/react";

import {Button, Table, cn} from "@heroui/react";

分页

使用 Table.Footer 在表格下方添加分页组件。

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com
"use client";

import {Pagination, Table} from "@heroui/react";
import {useMemo, useState} from "react";

列宽调整

使用 Table.ResizableContainer 包裹表格,并在每个可调整宽度的列中加入 Table.ColumnResizer

姓名角色状态邮箱
Kate Moore首席执行官Activekate@acme.com
John Smith首席技术官Activejohn@acme.com
Sara Johnson首席营销官On Leavesara@acme.com
Michael Brown首席财务官Activemichael@acme.com
Emily Davis产品经理Inactiveemily@acme.com
import {Chip, Table} from "@heroui/react";

export function ColumnResizing() {
  return (
    <Table>

空状态

Table.Body 上使用 renderEmptyState,在表格无数据时展示自定义内容。

姓名角色状态邮箱
未找到结果
"use client";

import {EmptyState, Table} from "@heroui/react";
import {Icon} from "@iconify/react";

异步加载

使用 Table.LoadMore 实现无限滚动:会渲染一行哨兵节点,在进入视口时触发 onLoadMore

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com
Emily Davis产品经理未激活emily@acme.com
Davis Wilson首席设计师在职davis@acme.com
"use client";

import {Chip, Spinner, Table} from "@heroui/react";
import {useCallback, useRef, useState} from "react";

虚拟化

Table 通过 Virtualizer 支持虚拟化,仅渲染视口内可见行,从而高效处理大数据集。

姓名
Emma Smith
"use client";

import {Table, TableLayout, Virtualizer} from "@heroui/react";

interface User {

TanStack Table

HeroUI 的 Table 可作为无头表格库之上的渲染层。 本示例使用 TanStack Table 处理列定义、排序与分页,而样式与无障碍由 HeroUI 负责。

姓名角色状态邮箱
Kate Moore首席执行官在职kate@acme.com
John Smith首席技术官在职john@acme.com
Sara Johnson首席营销官休假sara@acme.com
Michael Brown首席财务官在职michael@acme.com
"use client";

import type {SortDescriptor} from "@heroui/react";
import type {SortingState} from "@tanstack/react-table";

样式

传入 Tailwind CSS 类

你可以为 Table 的各个部分分别传入类名:

import { Table } from '@heroui/react';

function CustomTable() {
  return (
    <Table className="border border-purple-200">
      <Table.ScrollContainer>
        <Table.Content aria-label="Custom styled table">
          <Table.Header className="bg-purple-50">
            <Table.Column>Name</Table.Column>
          </Table.Header>
          <Table.Body>
            <Table.Row className="hover:bg-purple-50">
              <Table.Cell>Kate Moore</Table.Cell>
            </Table.Row>
          </Table.Body>
        </Table.Content>
      </Table.ScrollContainer>
    </Table>
  );
}

自定义组件类

若要自定义 Table 组件类,可以使用 @layer components 指令。
了解更多

@layer components {
  .table-root {
    @apply relative grid w-full overflow-clip;
  }

  .table__header {
    @apply bg-gray-100;
  }

  .table__column {
    @apply px-4 py-2.5 text-left text-xs font-medium text-gray-600;
  }

  .table__row {
    @apply bg-white border-b border-gray-200;
  }

  .table__cell {
    @apply px-4 py-3 text-sm;
  }

  .table__footer {
    @apply flex items-center px-4 py-2.5;
  }
}

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

CSS 类

Table 组件使用以下 CSS 类(查看源码样式):

基础类

  • .table-root - 根容器(命名为 table-root 而非 table,因为 table 是 Tailwind CSS 内置的 display: table 工具类)
  • .table__scroll-container - 横向滚动包裹层与自定义滚动条
  • .table__content - <table> 元素
  • .table__header - 表头行(<thead>
  • .table__column - 列表头单元格(<th>
  • .table__body - 表体(<tbody>
  • .table__row - 行(<tr>
  • .table__cell - 数据单元格(<td>
  • .table__footer - 表底容器(位于 table 外部)

进阶类

  • .table__column-resizer - 列宽拖拽手柄
  • .table__resizable-container - 启用列宽调整的包裹层
  • .table__load-more - 无限滚动的哨兵行
  • .table__load-more-content - 加载指示器的样式容器

变体类

  • .table-root--primary - 灰色背景容器与卡片式表体(默认)
  • .table-root--secondary - 无背景,独立圆角表头

交互状态

Table 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:

  • 悬停:hover[data-hovered="true"](行背景变化)
  • 已选中[data-selected="true"](行高亮)
  • 聚焦:focus-visible[data-focus-visible="true"](行、列与单元格的内嵌焦点环)
  • 禁用:disabled[aria-disabled="true"](降低透明度)
  • 可排序[data-allows-sorting="true"](列上的交互指针样式)
  • 拖动中[data-dragging="true"](降低透明度)
  • 放置目标[data-drop-target="true"](强调色背景)

API 参考

Table Props

Prop类型默认值描述
variant"primary" | "secondary""primary"视觉变体。Primary 为灰色背景容器;Secondary 为扁平透明行。
classNamestring-根容器的额外 CSS 类。
childrenReact.ReactNode-表格内容(ScrollContainer、Footer 等)。

Table.ScrollContainer Props

Prop类型默认值描述
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-Table.Content 元素。

Table.Content Props

继承自 React Aria Table

Prop类型默认值描述
aria-labelstring-表格的无障碍标签。
selectionMode"none" | "single" | "multiple""none"选择行为。
selectedKeysSelection-受控的已选中 key。
onSelectionChange(keys: Selection) => void-选择变化时的事件处理函数。
sortDescriptorSortDescriptor-当前排序状态。
onSortChange(descriptor: SortDescriptor) => void-排序变化时的事件处理函数。
classNamestring-额外的 CSS 类。

Table.Header Props

继承自 React Aria TableHeader

Prop类型默认值描述
columnsT[]-渲染函数模式下的动态列数据。
childrenReact.ReactNode | (column: T) => React.ReactNode-静态列或渲染函数。

Table.Column Props

继承自 React Aria Column

Prop类型默认值描述
idstring-列标识符。
allowsSortingbooleanfalse列是否可排序。
isRowHeaderbooleanfalse该列是否作为行表头。
defaultWidthstring | number-可调整列的默认宽度。
minWidthnumber-可调整列的最小宽度。
childrenReact.ReactNode | (values: ColumnRenderProps) => React.ReactNode-列内容或带排序方向的渲染函数。

Table.Body Props

继承自 React Aria TableBody

Prop类型默认值描述
itemsT[]-渲染函数模式下的动态行数据。
renderEmptyState() => React.ReactNode-表格为空时展示的内容。
childrenReact.ReactNode | (item: T) => React.ReactNode-静态行或渲染函数。

Table.Row Props

继承自 React Aria Row

Prop类型默认值描述
idstring | number-行标识符。
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-行单元格。

Table.Cell Props

继承自 React Aria Cell

Prop类型默认值描述
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-单元格内容。

Table.Footer Props

Prop类型默认值描述
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-表底内容(例如分页)。

Table.ColumnResizer Props

继承自 React Aria ColumnResizer

Prop类型默认值描述
classNamestring-额外的 CSS 类。

Table.ResizableContainer Props

继承自 React Aria ResizableTableContainer

Prop类型默认值描述
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-Table.Content 元素。

Table.LoadMore Props

继承自 React Aria TableLoadMoreItem

Prop类型默认值描述
isLoadingbooleanfalse数据是否正在加载。
onLoadMore() => void-哨兵行可见时的事件处理函数。
childrenReact.ReactNode-加载指示器内容。

Table.LoadMoreContent Props

Prop类型默认值描述
classNamestring-额外的 CSS 类。
childrenReact.ReactNode-加载指示器内容(例如 Spinner)。

Table.Collection Props

由 React Aria Collection 重新导出。用于在行内与静态单元格并存时渲染动态单元格(例如复选框)。

Prop类型默认值描述
itemsT[]-集合条目。
children(item: T) => React.ReactNode-每个条目的渲染函数。

TableLayout

Name类型默认值描述
rowHeightnumber | undefined48行的固定高度(px)。
estimatedRowHeightnumber | undefined行高可变时的估算高度。
headingHeightnumber | undefined48分区表头的固定高度(px)。
estimatedHeadingHeightnumber | undefined表头高度可变时的估算高度。
loaderHeightnumber | undefined48加载器元素的固定高度(px)。该加载器用于在根级或嵌套行/分区中渲染「加载更多」等加载行。
dropIndicatorThicknessnumber | undefined2放置指示器的线条粗细。
gapnumber | undefined0条目之间的间距。
paddingnumber | undefined0列表的内边距。

本页目录