在企业平台的表格中,用户常常需要快速选取整列或整行进行批量操作(如复制、删除、样式统一),而无需逐个单元格点选。vxe-table 提供了强大的区域选取能力,配合表头/行状态高亮,可以实现鼠标滑过表头即选中整列、点击首列即选中整行等交互,极大提升操作效率。
本文将基于 vxe-table 的 areaConfig 与 mouseConfig 配置,详细介绍如何实现这些功能。
配置概览
实现"鼠标滑动表头选择整列"与"选择整行"主要依赖以下配置:
| 配置项 | 作用 |
|---|---|
| mouseConfig.area | 开启区域选取功能(必须) |
| areaConfig.selectCellByHeader | 启用后,点击或滑过表头会自动选中该列所有单元格(需配合 mouseConfig.area) |
| areaConfig.selectCellToRow | 启用后,点击首列(通常是 seq 或 checkbox 列)会自动选中整行,且该列变为不可编辑 |
| areaConfig.showColumnStatus / showRowStatus | 是否高亮显示选中列/行的状态(背景色变化) |
| areaConfig.multiple | 是否允许同时选择多个区域(按住 Ctrl/Shift 扩展) |
重要:selectCellByHeader 和 selectCellToRow 均为布尔值,需要在 areaConfig 中显式设置为 true 才会生效。
功能拆解与配置示例
鼠标滑过表头选中整列
开启 areaConfig.selectCellByHeader: true,当鼠标悬停在表头单元格上时,该列的所有数据行会被自动高亮选中(如同点击表头)。若配合 areaConfig.showColumnStatus: true,还会在列头底部显示选中状态条。
javascript
mouseConfig: {
area: true
},
areaConfig: {
selectCellByHeader: true, // 表头悬停选中整列
showColumnStatus: true, // 显示列选中状态
// 其他配置...
}
点击首列选中整行
开启 areaConfig.selectCellToRow: true,当用户点击第一列(通常是 type: 'seq' 序号列或 type: 'checkbox' 列)的任意单元格时,该行所有单元格会被自动选中(相当于行选择)。同时,该列单元格将变为只读(无法编辑),以避免干扰行选中操作。
javascript
areaConfig: {
selectCellToRow: true, // 点击首列选中整行
showRowStatus: true, // 显示行选中状态
// 其他配置...
}
多区域选择与键盘联动
- areaConfig.multiple: true 允许用户通过按住 Ctrl 或 Shift 键扩展选择多个不连续或连续区域。
- keyboardConfig 中的 isShift、isArrow 等配置支持键盘方向键扩展选区,进一步提升操作效率。
代码

html
<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const gridOptions = reactive({
border: true,
height: 600,
showOverflow: true,
columnConfig: {
resizable: true,
minWidth: 120
},
rowConfig: {
resizable: true
},
resizableConfig: {
isDblclickAutoWidth: true // 启用双击自适应列宽
},
mouseConfig: {
area: true // 是否开启区域选取
},
areaConfig: {
multiple: true, // 是否启用多区域选取功能
showColumnStatus: true, // 选择单元格后显示列状态
showRowStatus: true, // 选择单元格后显示行状态
selectCellByHeader, // 选择列头自动选中整列
selectCellToRow: true // 选择首列单元格动选中整行
},
editConfig: {
mode: 'cell', // 单元格编辑模式
trigger: 'dblclick' // 双击单元格激活编辑状态
},
keyboardConfig: {
arrowCursorLock: true, // 方向键光标锁,开启后处于非聚焦式编辑状态,将支持在编辑状态中通过方向键切换单元格。(切换为聚焦编辑状态,可以按 F2 键或者鼠标左键点击输入框,就可以用方向键左右移动输入框的光标)
isClip: true, // 是否开启复制粘贴
isArrow: true, // 是否开启方向键功能
isShift: true, // 是否开启同时按住方向键以活动区域为起始,向指定方向扩展单元格区域
isTab: true, // 是否开启 Tab 键功能
isEnter: true, // 是否开启回车键功能
isEdit: true, // 是否开启任意键进入编辑(功能键除外)
isBack: true, // 是否开启Backspace键功能
isDel: true, // 是否开启删除键功能
isEsc: true, // 是否开启Esc键关闭编辑功能
isFNR: true, // 是否开启查找与替换
isMerge: true // 是否开启单元格合并功能
},
columns: [
{ type: 'seq', fixed: 'left', width: 60, rowResize: true },
{ field: 'a', title: 'A', editRender: { name: 'input' } },
{ field: 'b', title: 'B', editRender: { name: 'input' } },
{ field: 'c', title: 'C', editRender: { name: 'input' } },
{ field: 'd', title: 'D', editRender: { name: 'input' } },
{ field: 'e', title: 'E', editRender: { name: 'input' } },
{ field: 'f', title: 'F', editRender: { name: 'input' } },
{ field: 'g', title: 'G', editRender: { name: 'input' } },
{ field: 'h', title: 'H', editRender: { name: 'input' } },
{ field: 'i', title: 'I', editRender: { name: 'input' } },
{ field: 'j', title: 'J', editRender: { name: 'input' } }
],
data: [
{ id: 10001, a: 'Test1', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10002, a: 'Test2', b: 'abcdef abcdef abcdef abcdef', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10003, a: 'Test3', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10004, a: 'Test4', b: 'abcdef abcdef abcdef', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10005, a: 'Test5', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10006, a: 'Test6', b: 'abcdef', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10007, a: 'Test7', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10008, a: 'Test8', b: 'abcdef abcdef abcdef abcdef abcdef abcdef', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10009, a: 'Test9', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10010, a: 'Test10', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10011, a: 'Test11', b: '', c: '', d: 'abcdef abcdef abcdef abcdef abcdef abcdef', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10012, a: 'Test12', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10013, a: 'Test13', b: '', c: '', d: 'abcdef abcdef abcdef abcdef abcdef abcdef abcdef abcdef abcdef abcdef abcdef abcdef', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10014, a: 'Test14', b: '', c: '', d: 'abcdef abcdef abcdef', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10015, a: 'Test15', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10016, a: 'Test16', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10017, a: 'Test17', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10018, a: 'Test18', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10019, a: 'Test19', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10020, a: 'Test20', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10021, a: 'Test21', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10022, a: 'Test22', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' },
{ id: 10023, a: 'Test23', b: '', c: '', d: '', e: '', f: '', g: '', h: '', i: '', j: '', k: '' }
]
})
</script>
常用说明
- 首列只读行为
- 开启 selectCellToRow 后,第一列将自动变为只读,即使你在 editConfig 中开启了编辑,也无法编辑该列。如需编辑首列,请关闭此配置或调整列顺序(将可编辑列放在前面)。
- 性能考量
- 表格数据量极大时(>10000 行),开启区域高亮可能会增加渲染压力。建议配合虚拟滚动(virtualYConfig)使用,并酌情关闭 showColumnStatus / showRowStatus 以减少 DOM 更新。
vxe-table 通过 areaConfig 提供了简洁的配置方式,让开发者轻松实现表头悬停选中整列和首列点击选中整行,显著提升表格数据的选取效率。只需开启 mouseConfig.area 并配置相应的 selectCellByHeader / selectCellToRow 即可,无需编写额外逻辑。
在实际项目中,建议根据业务场景灵活组合这些配置,并注意与编辑模式、复选框等的协同。如需更复杂的选取逻辑,可借助事件 API 进行二次定制。