2024-10-13 18:33:43 +08:00
|
|
|
---
|
|
|
|
|
outline: deep
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Vben Vxe Table 表格
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
`Vben Vxe Table` 基于 [vxe-table](https://vxetable.cn/v4/#/grid/api?apiKey=grid) 和 `Vben Form` 做了二次封装,用于构建带搜索表单的列表页面。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
> 如果文档内没有覆盖到你需要的细节,可以结合在线示例和 [vxe-grid 官方 API](https://vxetable.cn/v4/#/grid/api?apiKey=grid) 一起查看。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
::: info 写在前面
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
如果现有封装不满足你的场景,可以直接使用原生 `vxe-table` 能力,或者在适配层中继续扩展。:::
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2024-11-06 23:03:33 +08:00
|
|
|
## 适配器
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
底层表格基于 `vxe-table`,每个应用都可以在自己的适配层中配置默认行为、自定义渲染器以及与 UI 组件库的集成。
|
2024-11-06 23:03:33 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
### 适配器示例
|
2024-11-06 23:03:33 +08:00
|
|
|
|
|
|
|
|
::: details vxe-table 表格适配器
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
import { h } from 'vue';
|
|
|
|
|
|
|
|
|
|
import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table';
|
|
|
|
|
|
2026-05-18 17:30:59 +08:00
|
|
|
import { Button, Image } from 'antdv-next';
|
2024-11-06 23:03:33 +08:00
|
|
|
|
|
|
|
|
import { useVbenForm } from './form';
|
|
|
|
|
|
|
|
|
|
setupVbenVxeTable({
|
|
|
|
|
configVxeTable: (vxeUI) => {
|
|
|
|
|
vxeUI.setConfig({
|
|
|
|
|
grid: {
|
|
|
|
|
align: 'center',
|
|
|
|
|
border: false,
|
|
|
|
|
columnConfig: {
|
|
|
|
|
resizable: true,
|
|
|
|
|
},
|
|
|
|
|
minHeight: 180,
|
|
|
|
|
formConfig: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
|
|
|
|
proxyConfig: {
|
|
|
|
|
autoLoad: true,
|
|
|
|
|
response: {
|
|
|
|
|
result: 'items',
|
|
|
|
|
total: 'total',
|
|
|
|
|
list: 'items',
|
|
|
|
|
},
|
|
|
|
|
showActiveMsg: true,
|
|
|
|
|
showResponseMsg: false,
|
|
|
|
|
},
|
|
|
|
|
round: true,
|
|
|
|
|
showOverflow: true,
|
|
|
|
|
size: 'small',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vxeUI.renderer.add('CellImage', {
|
|
|
|
|
renderTableDefault(_renderOpts, params) {
|
|
|
|
|
const { column, row } = params;
|
|
|
|
|
return h(Image, { src: row[column.field] });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vxeUI.renderer.add('CellLink', {
|
|
|
|
|
renderTableDefault(renderOpts) {
|
|
|
|
|
const { props } = renderOpts;
|
|
|
|
|
return h(
|
|
|
|
|
Button,
|
|
|
|
|
{ size: 'small', type: 'link' },
|
|
|
|
|
{ default: () => props?.text },
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
useVbenForm,
|
|
|
|
|
});
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2024-11-06 23:03:33 +08:00
|
|
|
export { useVbenVxeGrid };
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2024-11-06 23:03:33 +08:00
|
|
|
export type * from '@vben/plugins/vxe-table';
|
2024-11-06 21:44:02 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
## 基础表格
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过 `useVbenVxeGrid` 创建一个基础表格。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/basic" />
|
|
|
|
|
|
|
|
|
|
## 远程加载
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过配置 `proxyConfig.ajax.query` 实现远程数据加载。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/remote" />
|
|
|
|
|
|
|
|
|
|
## 树形表格
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
树形表格的数据源通常是扁平结构,可以通过 `treeConfig` 转换为树形展示。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
```ts
|
2024-11-06 21:44:02 +08:00
|
|
|
treeConfig: {
|
2026-03-14 21:34:48 +08:00
|
|
|
transform: true,
|
|
|
|
|
parentField: 'parentId',
|
|
|
|
|
rowField: 'id',
|
2024-11-06 21:44:02 +08:00
|
|
|
},
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/tree" />
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
## 固定列
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
固定列可选值为 `'left' | 'right' | '' | null`。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/fixed" />
|
|
|
|
|
|
|
|
|
|
## 自定义单元格
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
可以通过插槽或自定义渲染器实现单元格定制。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
```ts
|
2024-11-06 21:44:02 +08:00
|
|
|
vxeUI.renderer.add('CellImage', {
|
2026-03-14 21:34:48 +08:00
|
|
|
renderTableDefault(_renderOpts, params) {
|
2024-11-06 21:44:02 +08:00
|
|
|
const { column, row } = params;
|
2026-03-14 21:34:48 +08:00
|
|
|
return h(Image, { src: row[column.field] } as any);
|
2024-11-06 21:44:02 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
vxeUI.renderer.add('CellLink', {
|
2026-03-14 21:34:48 +08:00
|
|
|
renderTableDefault(renderOpts) {
|
2024-11-06 21:44:02 +08:00
|
|
|
const { props } = renderOpts;
|
|
|
|
|
return h(
|
|
|
|
|
Button,
|
|
|
|
|
{ size: 'small', type: 'link' },
|
|
|
|
|
{ default: () => props?.text },
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/custom-cell" />
|
|
|
|
|
|
|
|
|
|
## 搜索表单
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
搜索区域底层使用的是 `Vben Form`。启用搜索表单后,可以通过 `gridOptions.toolbarConfig.search = true` 在工具栏中显示搜索面板开关按钮。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
所有以 `form-` 开头的具名插槽都会自动转发到搜索表单。
|
2024-12-12 22:28:03 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
### 自定义分隔条
|
2025-04-08 20:28:50 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
启用搜索表单时,表单和表格主体之间默认会显示一个分隔条。可以通过 `separator` 调整或关闭它。
|
2025-04-08 20:28:50 +08:00
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
const [Grid] = useVbenVxeGrid({
|
|
|
|
|
formOptions: {},
|
|
|
|
|
gridOptions: {},
|
|
|
|
|
separator: false,
|
|
|
|
|
// separator: { show: false },
|
|
|
|
|
// separator: { backgroundColor: 'rgba(100,100,0,0.5)' },
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2024-11-06 21:44:02 +08:00
|
|
|
<DemoPreview dir="demos/vben-vxe-table/form" />
|
|
|
|
|
|
|
|
|
|
## 单元格编辑
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过设置 `editConfig.mode = 'cell'` 开启单元格编辑。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/edit-cell" />
|
|
|
|
|
|
|
|
|
|
## 行编辑
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过设置 `editConfig.mode = 'row'` 开启整行编辑。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/edit-row" />
|
|
|
|
|
|
|
|
|
|
## 虚拟滚动
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过 `scroll-y.enabled` 和 `scroll-y.gt` 组合开启纵向虚拟滚动。
|
2024-11-06 21:44:02 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
> 参考 [vxe-table 官方文档 - 虚拟滚动](https://vxetable.cn/v4/#/component/grid/scroll/vertical)
|
2024-11-06 21:44:02 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-vxe-table/virtual" />
|
2024-11-06 23:03:33 +08:00
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
`useVbenVxeGrid` 返回一个数组,第一个元素是表格组件,第二个元素是表格 API。
|
2024-11-06 23:03:33 +08:00
|
|
|
|
|
|
|
|
```vue
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
|
|
|
|
|
|
|
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
|
|
|
|
gridOptions: {},
|
|
|
|
|
formOptions: {},
|
|
|
|
|
gridEvents: {},
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<Grid />
|
|
|
|
|
</template>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### GridApi
|
|
|
|
|
|
2024-12-12 22:28:03 +08:00
|
|
|
| 方法名 | 描述 | 类型 | 说明 |
|
|
|
|
|
| --- | --- | --- | --- |
|
2026-03-14 21:34:48 +08:00
|
|
|
| setLoading | 设置 loading 状态 | `(loading: boolean) => void` | - |
|
|
|
|
|
| setGridOptions | 更新 `gridOptions` | `(options: Partial<VxeGridProps['gridOptions']>) => void` | - |
|
|
|
|
|
| reload | 重新加载表格,并重置到初始分页 | `(params?: Record<string, any>) => void` | - |
|
|
|
|
|
| query | 重新查询表格,保留当前分页 | `(params?: Record<string, any>) => void` | - |
|
|
|
|
|
| grid | `vxe-grid` 实例 | `VxeGridInstance` | - |
|
|
|
|
|
| formApi | 搜索表单 API 实例 | `FormApi` | - |
|
|
|
|
|
| toggleSearchForm | 切换或指定搜索表单显示状态 | `(show?: boolean) => boolean` | 传入参数时强制设置;不传参数时在显示和隐藏之间切换,并返回当前状态 |
|
2024-11-06 23:03:33 +08:00
|
|
|
|
|
|
|
|
## Props
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
所有属性都通过 `useVbenVxeGrid` 的第一个参数传入。
|
2024-11-06 23:03:33 +08:00
|
|
|
|
2025-04-08 20:28:50 +08:00
|
|
|
| 属性名 | 描述 | 类型 | 版本要求 |
|
|
|
|
|
| --- | --- | --- | --- |
|
|
|
|
|
| tableTitle | 表格标题 | `string` | - |
|
|
|
|
|
| tableTitleHelp | 表格标题帮助信息 | `string` | - |
|
2026-03-14 21:34:48 +08:00
|
|
|
| class | 外层容器的 class | `string` | - |
|
|
|
|
|
| gridClass | `vxe-grid` 的 class | `string` | - |
|
|
|
|
|
| gridOptions | `vxe-grid` 配置 | `DeepPartial<VxeTableGridOptions>` | - |
|
|
|
|
|
| gridEvents | `vxe-grid` 事件 | `DeepPartial<VxeGridListeners>` | - |
|
|
|
|
|
| formOptions | 搜索表单配置 | `VbenFormProps` | - |
|
2025-04-08 20:28:50 +08:00
|
|
|
| showSearchForm | 是否显示搜索表单 | `boolean` | - |
|
2026-03-14 21:34:48 +08:00
|
|
|
| separator | 搜索表单与表格主体之间的分隔条 | `boolean \| SeparatorOptions` | `>5.5.4` |
|
2025-03-01 22:12:15 +08:00
|
|
|
|
|
|
|
|
## Slots
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
大部分插槽说明可参考 [vxe-table 官方文档](https://vxetable.cn/v4/#/grid/api),这里列出封装层新增或约定的部分。
|
2025-03-01 22:12:15 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
| 插槽名 | 描述 |
|
|
|
|
|
| --------------- | ------------------------------------ |
|
|
|
|
|
| toolbar-actions | 工具栏左侧区域,位于标题附近 |
|
|
|
|
|
| toolbar-tools | 工具栏右侧区域,位于内置工具按钮左侧 |
|
|
|
|
|
| table-title | 自定义表格标题 |
|
2025-03-10 10:53:17 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
::: info 搜索表单插槽
|
2025-03-10 10:53:17 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
当启用了搜索表单时,所有以 `form-` 开头的具名插槽都会被转发给表单。:::
|