VxeGrid 全面使用指南与操作列实现
一、VxeGrid 简介
VxeGrid 是基于 Vue.js 的高性能表格组件库,提供了丰富的功能和灵活的配置选项,支持虚拟滚动、树形表格、单元格编辑、数据校验等企业级功能,是构建复杂数据表格的理想选择。
二、基础使用步骤
2.1 安装与引入
# npm 安装
npm install vxe-table vue-i18n
# yarn 安装
yarn add vxe-table vue-i18n
// 全局引入
import Vue from 'vue'
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
Vue.use(VXETable)
2.2 基本结构
<template>
<vxe-grid
:data="tableData"
:columns="columns"
border
stripe
/>
</template>
<script>
export default {
data() {
return {
// 表格数据
tableData: [],
// 列配置
columns: []
}
}
}
</script>
三、核心配置详解
3.1 数据源配置
tableData: [
{ id: 1, name: '张三', age: 25, email: 'zhangsan@example.com' },
{ id: 2, name: '李四', age: 30, email: 'lisi@example.com' }
]
3.2 列配置(columns)
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
field | 字段名称 | String | - |
title | 列标题 | String | - |
width | 列宽度 | Number/String | - |
minWidth | 最小宽度 | Number/String | - |
fixed | 固定列(left/right) | String | - |
formatter | 格式化函数 | Function | - |
slots | 插槽配置 | Object | - |
cellRender | 单元格渲染配置 | Object | - |
columns: [
{ field: 'id', title: 'ID', width: 80 },
{ field: 'name', title: '姓名', width: 120 },
{
field: 'age',
title: '年龄',
width: 100,
formatter: ({ cellValue }) => `${cellValue}岁`
},
{ field: 'email', title: '邮箱', minWidth: 200 }
]
3.3 外观配置
<vxe-grid
border <!-- 显示边框 -->
stripe <!-- 斑马纹 -->
show-header <!-- 显示表头 -->
show-overflow <!-- 内容溢出时显示省略号 -->
height="500px" <!-- 固定高度 -->
max-height="800px" <!-- 最大高度 -->
/>
四、操作列实现(重点)
4.1 插槽方式实现操作列(推荐)
4.1.1 基本实现
<template>
<div class="grid-container">
<vxe-grid
:data="tableData"
:columns="columns"
:column-config="columnConfig"
border
stripe
>
<!-- 操作列插槽 -->
<template v-slot:operation="{ row }">
<el-button
type="text"
icon="el-icon-edit"
@click="handleEdit(row)"
style="color: #409EFF;"
>
修改
</el-button>
<el-button
type="text"
icon="el-icon-delete"
@click="handleDelete(row)"
style="color: #F56C6C;"
>
删除
</el-button>
<el-button
type="text"
icon="el-icon-view"
@click="handleView(row)"
style="color: #67C23A;"
>
查看
</el-button>
</template>
</vxe-grid>
</div>
</template>
<script>
const tableData = [
{ id: 1, name: '张三', age: 25, email: 'zhangsan@example.com', address: '北京市朝阳区' },
{ id: 2, name: '李四', age: 30, email: 'lisi@example.com', address: '上海市浦东新区' },
{ id: 3, name: '王五', age: 28, email: 'wangwu@example.com', address: '广州市天河区' }
];
export default {
data() {
return {
tableData,
columns: [
{ field: 'id', title: 'ID', width: 80 },
{ field: 'name', title: '姓名', width: 120 },
{ field: 'age', title: '年龄', width: 100 },
{ field: 'email', title: '邮箱', width: 200 },
{ field: 'address', title: '地址', minWidth: 200 },
{
title: '操作',
width: 220,
slots: { default: 'operation' }, // 绑定插槽
fixed: 'right', // 固定在右侧
headerAlign: 'center',
align: 'center'
}
],
// 列拖拽配置
columnConfig: {
drag: true // 启用列拖拽
}
};
},
methods: {
/**
* 编辑操作
* @param {Object} row - 当前行数据
*/
handleEdit(row) {
console.log('编辑数据:', row);
// 打开编辑弹窗逻辑
this.$modal.open({
title: '编辑用户',
content: EditForm,
data: row,
onConfirm: () => {
// 保存逻辑
}
});
},
/**
* 删除操作
* @param {Object} row - 当前行数据
*/
handleDelete(row) {
this.$confirm(`确定要删除【${row.name}】吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
// 执行删除操作
this.tableData = this.tableData.filter(item => item.id !== row.id);
this.$message({
type: 'success',
message: '删除成功!'
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
},
/**
* 查看详情
* @param {Object} row - 当前行数据
*/
handleView(row) {
console.log('查看详情:', row);
// 打开详情弹窗
}
}
};
</script>
<style scoped>
.grid-container {
padding: 20px;
}
</style>
4.1.2 动态操作权限控制
<template v-slot:operation="{ row }">
<el-button
v-if="row.status !== '已锁定'"
type="text"
icon="el-icon-edit"
@click="handleEdit(row)"
>
修改
</el-button>
<el-button
v-if="hasDeletePermission"
type="text"
icon="el-icon-delete"
@click="handleDelete(row)"
style="color: #F56C6C;"
>
删除
</el-button>
</template>
4.2 cellRender 方式实现操作列
<template>
<div class="grid-container">
<vxe-grid
:data="tableData"
:columns="columns"
border
stripe
/>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{ id: 1, name: '张三', age: 25, address: '北京' },
{ id: 2, name: '李四', age: 30, address: '上海' },
{ id: 3, name: '王五', age: 28, address: '广州' }
],
columns: [
{ field: 'id', title: 'ID', width: 80 },
{ field: 'name', title: '姓名', width: 120 },
{ field: 'age', title: '年龄', width: 100 },
{ field: 'address', title: '地址', width: 150 },
{
title: '操作',
width: 180,
cellRender: {
name: 'render-operation' // 关联自定义渲染器
}
}
]
};
},
// 注册自定义渲染器
renderCell: {
'render-operation'(h, params) {
const { row } = params;
return h('div', {
style: { display: 'flex', justifyContent: 'center', gap: '10px' }
}, [
h('el-button', {
props: { type: 'text', icon: 'el-icon-edit', size: 'mini' },
style: { color: '#409EFF' },
on: { click: () => this.handleEdit(row) }
}, '修改'),
h('el-button', {
props: { type: 'text', icon: 'el-icon-delete', size: 'mini' },
style: { color: '#F56C6C' },
on: { click: () => this.handleDelete(row) }
}, '删除')
]);
}
},
methods: {
handleEdit(row) {
console.log('编辑:', row);
},
handleDelete(row) {
console.log('删除:', row);
this.tableData = this.tableData.filter(item => item.id !== row.id);
}
}
};
</script>
五、高级功能配置
5.1 列拖拽功能
<vxe-grid
:data="tableData"
:columns="columns"
:column-config="columnConfig"
:column-drag-config="columnDragConfig"
border
/>
data() {
return {
columnConfig: {
drag: true, // 启用列拖拽
resizable: true // 启用列宽调整
},
columnDragConfig: {
trigger: 'header', // 表头触发拖拽
showIcon: true, // 显示拖拽图标
showGuides: true // 显示拖拽参考线
}
}
}
5.2 分页配置
<vxe-grid
:data="tableData"
:columns="columns"
:pager-config="pagerConfig"
@page-change="handlePageChange"
border
/>
data() {
return {
pagerConfig: {
pageSize: 10,
pageSizes: [10, 20, 50, 100],
align: 'right',
layouts: ['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'Total']
}
}
},
methods: {
handlePageChange({ page, pageSize }) {
console.log(`当前页:${page},每页条数:${pageSize}`);
// 加载对应页数据
this.loadData(page, pageSize);
}
}
5.3 多选功能
<vxe-grid
:data="tableData"
:columns="columns"
:checkbox-config="checkboxConfig"
@checkbox-all="handleCheckboxAll"
@checkbox-change="handleCheckboxChange"
border
/>
data() {
return {
checkboxConfig: {
labelField: 'name',
range: true, // 支持范围选择
highlight: true
}
}
},
methods: {
handleCheckboxAll(checked, records) {
console.log('全选状态:', checked, '选中数据:', records);
},
handleCheckboxChange({ checked, records }) {
console.log('选中状态变更:', checked, '选中数据:', records);
}
}
六、最佳实践
6.1 性能优化
- 虚拟滚动:大数据量时启用虚拟滚动
<vxe-grid
:data="tableData"
:scroll-y="{ enabled: true, gt: 100 }" // 超过100行启用虚拟滚动
/>
- 延迟加载:避免一次性加载所有数据
- 合理设置列宽:避免不必要的列宽自适应计算
6.2 代码组织
- 列配置抽离:复杂表格的列配置单独抽离成文件
// columns/user.columns.js
export default [
{ field: 'id', title: 'ID', width: 80 },
// ...其他列配置
];
// 在组件中引入
import userColumns from './columns/user.columns.js';
export default {
data() {
return {
columns: userColumns
}
}
}
- 事件处理统一管理:将表格操作事件集中管理
6.3 操作列设计原则
- 操作按钮简洁:只保留必要的操作按钮
- 视觉区分:不同类型操作使用不同颜色区分
- 权限控制:根据用户权限动态显示操作按钮
- 操作反馈:提供明确的操作确认和结果反馈
七、常见问题解答
Q1: 操作列按钮点击无响应?
A:检查作用域问题,确保事件处理函数能正确访问到组件实例。插槽方式比 cellRender 方式更不易出现作用域问题。
Q2: 列拖拽后操作列位置变化?
A:为操作列设置fixed: 'right'属性固定位置。
Q3: 操作列宽度不够导致按钮换行?
A:合理设置操作列宽度,或使用下拉菜单整合多个操作。
八、总结
VxeGrid 是功能强大的表格组件,操作列的实现主要有两种方式:
- 插槽方式:推荐使用,代码清晰,易于维护,支持复杂交互
- cellRender 方式:适合简单场景,配置更集中
在实际项目中,建议根据需求选择合适的实现方式,并遵循性能优化和代码组织的最佳实践,以构建高效、易用的数据表格。
关键点回顾:
- VxeGrid 操作列推荐使用插槽方式实现,代码更清晰易维护
- 列拖拽功能通过
column-config和column-drag-config配置启用 - 操作列应设置
fixed: 'right'固定位置,提升用户体验 - 操作按钮需提供明确的视觉区分和操作反馈
- 复杂表格建议将列配置抽离管理,提升代码可维护性
1719

被折叠的 条评论
为什么被折叠?



