Table

使用虚拟滚动、选择、排序与可调整列浏览和编辑数据行。

适用场景

  • 当界面需要以下行为时使用 Table:使用虚拟滚动、选择、排序与可调整列浏览和编辑数据行。

安装

选择安装方式,用法示例将同步更新导入路径。

$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/table.json https://orvui.efu.me/r/table-editable.json https://orvui.efu.me/r/switch.json https://orvui.efu.me/r/table-async.json

首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。

示例

Data Table

data.vue

浏览虚拟化数据行,并进行选择、排序和列位置调整。

10,000 rows
Email
Status

Editable Table

editable.vue

编辑单元格数值,同时保留表格的键盘与行导航方式。

Click a cell to edit. Use the column and row handles to insert or delete.

Async Table

async.vue

通过 onEndReached 加载下一页,并保留明确的加载与失败状态。

0 loadedLoading…
Name
Email
Role
Status
MRR

状态与事件

需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。

组件状态绑定
组件Vue 绑定更新事件
Tablev-model:selectedRowIdsstring[]update:selectedRowIds
Tablev-model:sortnull | SortStateupdate:sort

可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。

API 参考

Data Table

data
any[]
—
columns
TableColumn<any>[]
—
getRowId?
(row: any, index: number) => string

Stable id per row, required for correct selection across sorts. Defaults to row index.

—
selectable?
boolean

Render a leading checkbox column with select-all in the header.

false
selectedRowIds?
string[]
—
defaultSelectedRowIds?
string[]
() => []
onSelectionChange?
(ids: string[]) => void
—
sort?
null | SortState
—
defaultSort?
null | SortState
null
onSortChange?
(sort: SortState | null) => void
—
resizable?
boolean

Allow dragging the right edge of a header to resize that column.

false
minColumnWidth?
number

Minimum column width in px when resizing.

64
onColumnResize?
(key: string, width: number) => void
—
reorderable?
boolean

Allow dragging a header grip to reorder columns.

false
onColumnOrderChange?
(keys: string[]) => void
—
onCellEdit?
(rowId: string, columnKey: string, value: string) => void

Called when an `editable` cell changes.

—
onColumnRename?
(columnKey: string, value: string) => void

When set, non-sortable headers become editable inputs for the column name.

—
onInsertRow?
(index: number, position: InsertPosition) => void

Enables the row menu (Insert before / after). Receives the target index.

—
onDeleteRow?
(rowId: string, index: number) => void

Enables Delete in the row menu.

—
onInsertColumn?
(index: number, position: InsertPosition) => void

Enables the column menu (Insert before / after). Receives the target column index.

—
onDeleteColumn?
(columnKey: string, index: number) => void

Enables Delete in the column menu.

—
rowHeight?
number

Fixed row height in px — required for virtualization.

48
height?
number

Scroll viewport height in px.

440
overscan?
number

Rows rendered above/below the viewport.

10
onEndReached?
() => void

Fires when the viewport scrolls near the bottom — load the next page.

—
loading?
boolean

Currently fetching — shows skeleton rows and pauses `onEndReached`.

false
skeletonRows?
number

How many skeleton rows to show while loading more (default 3).

3
emptyState?
boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren
"No data"
className?
string
—

事件

update:selectedRowIds
—
@update:selectedRowIds
selectionChange
—
@selectionChange
update:sort
—
@update:sort
sortChange
—
@sortChange
columnResize
—
@columnResize
columnOrderChange
—
@columnOrderChange
cellEdit
—
@cellEdit
columnRename
—
@columnRename
insertRow
—
@insertRow
deleteRow
—
@deleteRow
insertColumn
—
@insertColumn
deleteColumn
—
@deleteColumn
endReached
—
@endReached

插槽

empty
() => VNodeChild
#empty
['cell-' + column.key]
—

作用域参数: row, value, row-id

#['cell-' + column.key]

Editable Table

data
any[]
—
columns
TableColumn<any>[]
—
getRowId?
(row: any, index: number) => string

Stable id per row, required for correct selection across sorts. Defaults to row index.

—
selectable?
boolean

Render a leading checkbox column with select-all in the header.

false
selectedRowIds?
string[]
—
defaultSelectedRowIds?
string[]
() => []
onSelectionChange?
(ids: string[]) => void
—
sort?
null | SortState
—
defaultSort?
null | SortState
null
onSortChange?
(sort: SortState | null) => void
—
resizable?
boolean

Allow dragging the right edge of a header to resize that column.

false
minColumnWidth?
number

Minimum column width in px when resizing.

64
onColumnResize?
(key: string, width: number) => void
—
reorderable?
boolean

Allow dragging a header grip to reorder columns.

false
onColumnOrderChange?
(keys: string[]) => void
—
onCellEdit?
(rowId: string, columnKey: string, value: string) => void

Called when an `editable` cell changes.

—
onColumnRename?
(columnKey: string, value: string) => void

When set, non-sortable headers become editable inputs for the column name.

—
onInsertRow?
(index: number, position: InsertPosition) => void

Enables the row menu (Insert before / after). Receives the target index.

—
onDeleteRow?
(rowId: string, index: number) => void

Enables Delete in the row menu.

—
onInsertColumn?
(index: number, position: InsertPosition) => void

Enables the column menu (Insert before / after). Receives the target column index.

—
onDeleteColumn?
(columnKey: string, index: number) => void

Enables Delete in the column menu.

—
rowHeight?
number

Fixed row height in px — required for virtualization.

48
height?
number

Scroll viewport height in px.

440
overscan?
number

Rows rendered above/below the viewport.

10
onEndReached?
() => void

Fires when the viewport scrolls near the bottom — load the next page.

—
loading?
boolean

Currently fetching — shows skeleton rows and pauses `onEndReached`.

false
skeletonRows?
number

How many skeleton rows to show while loading more (default 3).

3
emptyState?
boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren
"No data"
className?
string
—

事件

update:selectedRowIds
—
@update:selectedRowIds
selectionChange
—
@selectionChange
update:sort
—
@update:sort
sortChange
—
@sortChange
columnResize
—
@columnResize
columnOrderChange
—
@columnOrderChange
cellEdit
—
@cellEdit
columnRename
—
@columnRename
insertRow
—
@insertRow
deleteRow
—
@deleteRow
insertColumn
—
@insertColumn
deleteColumn
—
@deleteColumn
endReached
—
@endReached

插槽

empty
() => VNodeChild
#empty
['cell-' + column.key]
—

作用域参数: row, value, row-id

#['cell-' + column.key]

Async Table

data
any[]
—
columns
TableColumn<any>[]
—
getRowId?
(row: any, index: number) => string

Stable id per row, required for correct selection across sorts. Defaults to row index.

—
selectable?
boolean

Render a leading checkbox column with select-all in the header.

false
selectedRowIds?
string[]
—
defaultSelectedRowIds?
string[]
() => []
onSelectionChange?
(ids: string[]) => void
—
sort?
null | SortState
—
defaultSort?
null | SortState
null
onSortChange?
(sort: SortState | null) => void
—
resizable?
boolean

Allow dragging the right edge of a header to resize that column.

false
minColumnWidth?
number

Minimum column width in px when resizing.

64
onColumnResize?
(key: string, width: number) => void
—
reorderable?
boolean

Allow dragging a header grip to reorder columns.

false
onColumnOrderChange?
(keys: string[]) => void
—
onCellEdit?
(rowId: string, columnKey: string, value: string) => void

Called when an `editable` cell changes.

—
onColumnRename?
(columnKey: string, value: string) => void

When set, non-sortable headers become editable inputs for the column name.

—
onInsertRow?
(index: number, position: InsertPosition) => void

Enables the row menu (Insert before / after). Receives the target index.

—
onDeleteRow?
(rowId: string, index: number) => void

Enables Delete in the row menu.

—
onInsertColumn?
(index: number, position: InsertPosition) => void

Enables the column menu (Insert before / after). Receives the target column index.

—
onDeleteColumn?
(columnKey: string, index: number) => void

Enables Delete in the column menu.

—
rowHeight?
number

Fixed row height in px — required for virtualization.

48
height?
number

Scroll viewport height in px.

440
overscan?
number

Rows rendered above/below the viewport.

10
onEndReached?
() => void

Fires when the viewport scrolls near the bottom — load the next page.

—
loading?
boolean

Currently fetching — shows skeleton rows and pauses `onEndReached`.

false
skeletonRows?
number

How many skeleton rows to show while loading more (default 3).

3
emptyState?
boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren
"No data"
className?
string
—

事件

update:selectedRowIds
—
@update:selectedRowIds
selectionChange
—
@selectionChange
update:sort
—
@update:sort
sortChange
—
@sortChange
columnResize
—
@columnResize
columnOrderChange
—
@columnOrderChange
cellEdit
—
@cellEdit
columnRename
—
@columnRename
insertRow
—
@insertRow
deleteRow
—
@deleteRow
insertColumn
—
@insertColumn
deleteColumn
—
@deleteColumn
endReached
—
@endReached

插槽

empty
() => VNodeChild
#empty
['cell-' + column.key]
—

作用域参数: row, value, row-id

#['cell-' + column.key]

无障碍

为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。

常见问题

如何配置 Table?
通过 data、columns、getRowId、selectable、selectedRowIds、defaultSelectedRowIds、onSelectionChange、sort、defaultSort、onSortChange、resizable、minColumnWidth、onColumnResize、reorderable、onColumnOrderChange、onCellEdit、onColumnRename、onInsertRow、onDeleteRow、onInsertColumn、onDeleteColumn、rowHeight、height、overscan、onEndReached、loading、skeletonRows、emptyState、className 配置 Table;API 表列出其类型与默认值。
如何连接到应用状态?
监听 update:selectedRowIds、selectionChange、update:sort、sortChange、columnResize、columnOrderChange、cellEdit、columnRename、insertRow、deleteRow、insertColumn、deleteColumn、endReached,将组件连接到应用状态。
可以在服务端渲染吗?
可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。

更新于