Table 表格
功能丰富的表格组件,用于显示表格数据。
import { Table } from 'asterui'import type { ColumnType, RowSelection, ExpandableConfig, PaginationConfig } from 'asterui'基础表格
带有数据的简单表格。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User' },
];
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
return (
<Table columns={columns} dataSource={userData} pagination={false} />
)
}
export default App 自定义渲染
使用渲染函数生成标签、按钮和自定义内容。
Name | Email | Role | Status | Actions |
|---|---|---|---|---|
| John Doe | john@example.com | Admin | active | |
| Jane Smith | jane@example.com | User | active | |
| Bob Johnson | bob@example.com | User | inactive | |
| Alice Williams | alice@example.com | Editor | active |
import { Table, Tag, Button, Space } from 'asterui'
function App() {
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'active' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'active' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User', status: 'inactive' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor', status: 'active' },
];
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name', width: 150 },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role', align: 'center' },
{
key: 'status',
title: 'Status',
dataIndex: 'status',
align: 'center',
render: (value) => (
<Tag color={value === 'active' ? 'success' : 'ghost'} size="sm">
{String(value)}
</Tag>
),
},
{
key: 'actions',
title: 'Actions',
align: 'right',
render: () => (
<Space size="xs">
<Button size="xs" variant="ghost">Edit</Button>
<Button size="xs" variant="ghost">Delete</Button>
</Space>
),
},
];
return (
<Table columns={columns} dataSource={userData} pagination={false} />
)
}
export default App 条纹行
斑马条纹以提高可读性。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<Table columns={basicColumns} dataSource={userData.slice(0, 5)} striped pagination={false} />
)
}
export default App 带边框表格
添加边框以获得更明确的外观。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<Table columns={basicColumns} dataSource={userData.slice(0, 5)} bordered pagination={false} />
)
}
export default App 紧凑尺寸
用于密集数据的超小尺寸。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<Table
columns={basicColumns}
dataSource={userData.slice(0, 5)}
size="xs"
striped
bordered
pagination={false}
/>
)
}
export default App 超大尺寸
用于强调的超大尺寸。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<Table columns={basicColumns} dataSource={userData.slice(0, 3)} size="xl" pagination={false} />
)
}
export default App 空状态
可自定义的空状态消息。
Name | Email | Role |
|---|---|---|
| No users found | ||
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
return (
<Table
columns={columns}
dataSource={[]}
pagination={false}
locale={{ emptyText: 'No users found' }}
/>
)
}
export default App 加载状态
在获取数据时显示加载微调器。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [isLoading, setIsLoading] = useState(false);
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User' },
];
return (
<div className="space-y-4">
<Button size="sm" onClick={() => setIsLoading(!isLoading)}>
{isLoading ? 'Hide' : 'Show'} Loading
</Button>
<Table columns={columns} dataSource={userData} loading={isLoading} pagination={false} />
</div>
)
}
export default App 基础分页
对大数据集进行分页。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<Table columns={basicColumns} dataSource={userData} pagination={{ pageSize: 5 }} />
)
}
export default App 高级分页
页面大小更改器、快速跳转器和总数显示。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
1-5 of 12 items
Go to
import { Table } from 'asterui'
function App() {
return (
<Table
columns={basicColumns}
dataSource={userData}
pagination={{
pageSize: 5,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total, range) => `${range[0]}-${range[1]} of ${total} items`,
pageSizeOptions: [5, 10, 20],
}}
/>
)
}
export default App 固定表头
滚动时保持表头可见。
Name | Email | Role |
|---|---|---|
| John Doe | john@example.com | Admin |
| Jane Smith | jane@example.com | User |
| Bob Johnson | bob@example.com | User |
| Alice Williams | alice@example.com | Editor |
| Charlie Brown | charlie@example.com | User |
| David Lee | david@example.com | User |
| Emma Wilson | emma@example.com | Editor |
| Frank Miller | frank@example.com | User |
| Grace Taylor | grace@example.com | Admin |
| Henry Davis | henry@example.com | User |
| Iris Martin | iris@example.com | Editor |
| Jack White | jack@example.com | User |
import { Table } from 'asterui'
function App() {
return (
<div className="max-h-64 overflow-auto border border-base-content/10 rounded-lg">
<Table columns={basicColumns} dataSource={userData} pinRows striped pagination={false} />
</div>
)
}
export default App 列排序
点击列标题对数据进行排序。
Name | Age | Email |
|---|---|---|
| Jane Smith | 28 | jane@example.com |
| Charlie Brown | 29 | charlie@example.com |
| John Doe | 32 | john@example.com |
| Alice Williams | 35 | alice@example.com |
| David Lee | 41 | david@example.com |
| Bob Johnson | 45 | bob@example.com |
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name', sorter: true },
{ key: 'age', title: 'Age', dataIndex: 'age', sorter: true, defaultSortOrder: 'ascend' },
{ key: 'email', title: 'Email', dataIndex: 'email', sorter: true },
];
return (
<Table
columns={columns}
dataSource={userData.slice(0, 6)}
pagination={false}
/>
)
}
export default App 受控排序
从外部控制排序状态。
Name | Email | Role |
|---|---|---|
| Alice Williams | alice@example.com | Editor |
| Bob Johnson | bob@example.com | User |
| Charlie Brown | charlie@example.com | User |
| Jane Smith | jane@example.com | User |
| John Doe | john@example.com | Admin |
import { Table, Button, Space } from 'asterui'
import { useState } from 'react'
function App() {
const [sortOrder, setSortOrder] = useState<'ascend' | 'descend' | null>('ascend');
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name', sorter: true, sortOrder },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User' },
];
return (
<div className="space-y-4">
<Space size="sm">
<Button size="sm" variant={sortOrder === 'ascend' ? 'primary' : 'outline'} onClick={() => setSortOrder('ascend')}>
Ascending
</Button>
<Button size="sm" variant={sortOrder === 'descend' ? 'primary' : 'outline'} onClick={() => setSortOrder('descend')}>
Descending
</Button>
<Button size="sm" variant={sortOrder === null ? 'primary' : 'outline'} onClick={() => setSortOrder(null)}>
None
</Button>
</Space>
<Table
columns={columns}
dataSource={userData}
pagination={false}
onSortChange={(sorter) => setSortOrder(sorter.order ?? null)}
/>
</div>
)
}
export default App 列过滤
向列添加过滤器下拉菜单。
Name | Role | Status |
|---|---|---|
| John Doe | Admin | active |
| Jane Smith | User | active |
| Bob Johnson | User | inactive |
| Alice Williams | Editor | active |
| Charlie Brown | User | active |
| David Lee | User | active |
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{
key: 'role',
title: 'Role',
dataIndex: 'role',
filters: [
{ text: 'Admin', value: 'Admin' },
{ text: 'User', value: 'User' },
{ text: 'Editor', value: 'Editor' },
],
onFilter: (value, record) => record.role === value,
},
{
key: 'status',
title: 'Status',
dataIndex: 'status',
filters: [
{ text: 'Active', value: 'active' },
{ text: 'Inactive', value: 'inactive' },
],
onFilter: (value, record) => record.status === value,
},
];
return (
<Table
columns={columns}
dataSource={userData.slice(0, 6)}
pagination={false}
/>
)
}
export default App 复选框选择
使用复选框启用行选择。
Selected: None
Name | Email | Role | |
|---|---|---|---|
| John Doe | john@example.com | Admin | |
| Jane Smith | jane@example.com | User | |
| Bob Johnson | bob@example.com | User | |
| Alice Williams | alice@example.com | Editor | |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
import { useState } from 'react'
function App() {
const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User' },
];
return (
<div className="space-y-4">
<div className="text-sm">Selected: {selectedKeys.join(', ') || 'None'}</div>
<Table
columns={columns}
dataSource={userData}
rowSelection={{
selectedRowKeys: selectedKeys,
onChange: (keys) => setSelectedKeys(keys),
}}
pagination={false}
/>
</div>
)
}
export default App 单选按钮选择
使用单选按钮进行单行选择。
Selected: None
Name | Email | Role | |
|---|---|---|---|
| John Doe | john@example.com | Admin | |
| Jane Smith | jane@example.com | User | |
| Bob Johnson | bob@example.com | User | |
| Alice Williams | alice@example.com | Editor | |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
import { useState } from 'react'
function App() {
const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User' },
];
return (
<div className="space-y-4">
<div className="text-sm">Selected: {selectedKeys.join(', ') || 'None'}</div>
<Table
columns={columns}
dataSource={userData}
rowSelection={{
type: 'radio',
selectedRowKeys: selectedKeys,
onChange: (keys) => setSelectedKeys(keys),
}}
pagination={false}
/>
</div>
)
}
export default App 可展开行
展开行以显示附加内容。
| Expand | Name | Email | Role |
|---|---|---|---|
| John Doe | john@example.com | Admin | |
| Jane Smith | jane@example.com | User | |
| Bob Johnson | bob@example.com | User | |
| Alice Williams | alice@example.com | Editor | |
| Charlie Brown | charlie@example.com | User |
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name' },
{ key: 'email', title: 'Email', dataIndex: 'email' },
{ key: 'role', title: 'Role', dataIndex: 'role' },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'active', age: 32, description: 'Senior administrator' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'active', age: 28, description: 'Regular user' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User', status: 'inactive', age: 45, description: 'Account suspended' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor', status: 'active', age: 35, description: 'Content editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User', status: 'active', age: 29, description: 'New user' },
];
const expandable = {
expandedRowRender: (record) => (
<div className="p-2">
<p className="text-sm"><strong>Description:</strong> {record.description}</p>
<p className="text-sm"><strong>Age:</strong> {record.age}</p>
</div>
),
rowExpandable: (record) => record.status === 'active',
};
return (
<Table
columns={columns}
dataSource={userData}
expandable={expandable}
pagination={false}
/>
)
}
export default App 文本省略
使用省略号截断长文本。
Name | Email | Description |
|---|---|---|
| John Doe | john@example.com | Senior administrator with full system access |
| Jane Smith | jane@example.com | Regular user account |
| Bob Johnson | bob@example.com | Account currently suspended |
| Alice Williams | alice@example.com | Content editor with publishing rights |
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name', width: 100 },
{ key: 'email', title: 'Email', dataIndex: 'email', width: 150, ellipsis: true },
{ key: 'description', title: 'Description', dataIndex: 'description', ellipsis: true },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', description: 'Senior administrator with full system access' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', description: 'Regular user account' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', description: 'Account currently suspended' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', description: 'Content editor with publishing rights' },
];
return (
<Table
columns={columns}
dataSource={userData}
pagination={false}
/>
)
}
export default App 滚动配置
设置最大宽度和高度并滚动。
Name | Email | Role | Status | Age | Description |
|---|---|---|---|---|---|
| John Doe | john@example.com | Admin | active | 32 | Senior administrator |
| Jane Smith | jane@example.com | User | active | 28 | Regular user |
| Bob Johnson | bob@example.com | User | inactive | 45 | Account suspended |
| Alice Williams | alice@example.com | Editor | active | 35 | Content editor |
| Charlie Brown | charlie@example.com | User | active | 29 | New user |
import { Table } from 'asterui'
function App() {
const columns = [
{ key: 'name', title: 'Name', dataIndex: 'name', width: 150 },
{ key: 'email', title: 'Email', dataIndex: 'email', width: 200 },
{ key: 'role', title: 'Role', dataIndex: 'role', width: 100 },
{ key: 'status', title: 'Status', dataIndex: 'status', width: 100 },
{ key: 'age', title: 'Age', dataIndex: 'age', width: 80 },
{ key: 'description', title: 'Description', dataIndex: 'description', width: 300 },
];
const userData = [
{ id: '1', name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'active', age: 32, description: 'Senior administrator' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'active', age: 28, description: 'Regular user' },
{ id: '3', name: 'Bob Johnson', email: 'bob@example.com', role: 'User', status: 'inactive', age: 45, description: 'Account suspended' },
{ id: '4', name: 'Alice Williams', email: 'alice@example.com', role: 'Editor', status: 'active', age: 35, description: 'Content editor' },
{ id: '5', name: 'Charlie Brown', email: 'charlie@example.com', role: 'User', status: 'active', age: 29, description: 'New user' },
];
return (
<Table
columns={columns}
dataSource={userData}
scroll={{ x: 800, y: 200 }}
pagination={false}
/>
)
}
export default App 完整示例
组合多个功能的表格。
Name | Email | Role | Status | Actions |
|---|---|---|---|---|
| John Doe | john@example.com | Admin | active | |
| Jane Smith | jane@example.com | User | active | |
| Bob Johnson | bob@example.com | User | inactive | |
| Alice Williams | alice@example.com | Editor | active | |
| Charlie Brown | charlie@example.com | User | active |
import { Table, Tag, Button, Space } from 'asterui'
function App() {
return (
<Table
columns={columnsWithRender}
dataSource={userData}
size="sm"
striped
bordered
pagination={{ pageSize: 5 }}
/>
)
}
export default App | 属性 | 描述 | 类型 | 默认值 |
|---|---|---|---|
columns | 表格列配置 | ColumnType<T>[] | - |
dataSource | 要在表格中显示的数据 | T[] | - |
rowKey | 每行的唯一键 | keyof T | ((record: T) => string) | 'id' |
loading | 加载状态 | boolean | false |
size | 表格大小 | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' |
bordered | 在表格周围添加边框 | boolean | false |
hoverable | 悬停时突出显示行 | boolean | true |
striped | 交替行颜色(斑马条纹) | boolean | false |
pinRows | 滚动时固定表头行 | boolean | false |
pinCols | 滚动时固定列 | boolean | false |
pagination | 分页配置或 false 以禁用 | false | PaginationConfig | { pageSize: 10 } |
rowSelection | 行选择配置 | RowSelection<T> | - |
expandable | 可展开行配置 | ExpandableConfig<T> | - |
scroll | 固定尺寸的滚动配置 | ScrollConfig | - |
className | 额外的 CSS 类 | string | - |
onRow | 行事件处理程序 | (record: T, index: number) => HTMLAttributes | - |
onChange | 分页、排序和过滤的统一回调 | (pagination, filters, sorter, extra) => void | - |
onSortChange | 排序更改时的回调 | (sorter: SorterResult<T>) => void | - |
onFilterChange | 过滤器更改时的回调 | (filters: Record<string, ...>) => void | - |
locale | 空文本、过滤器标签的本地化配置 | { emptyText?, filterConfirm?, filterReset?, selectAll? } | - |
data-testid | 组件的测试 ID | string | 'table' |
aria-label | 表格的可访问标签 | string | - |
- 使用带有
role="grid"的语义<table> - 列标题包含
role="columnheader"和用于可排序列的aria-sort - 行选择复选框具有正确的
aria-label - 排序控件可通过键盘访问(Enter/Space 切换)
- 过滤器下拉菜单支持 Escape 键关闭
- 分页按钮包含
aria-label和aria-current - 展开/折叠按钮包含
aria-expanded - 部分选择的不确定复选框状态