<template>
<el-table
ref="tableRef"
:data="tableData"
highlight-current-row
:current-row-key="currentRowKey"
@current-change="handleCurrentChange"
style="width: 100%"
>
<el-table-column prop="name" label="姓名" />
<el-table-column prop="age" label="年龄" />
</el-table>
</template>
<script setup>
import { ref } from 'vue'
const tableData = ref([
{ id: 1, name: '张三', age: 20 },
{ id: 2, name: '李四', age: 25 },
{ id: 3, name: '王五', age: 30 }
])
const currentRowKey = ref(null)
// 点击行触发
const handleCurrentChange = (row) => {
if (row) {
currentRowKey.value = row.id // 确保唯一标识
}
}
</script>
现在还需要给表格加上 row-key,确保每行有唯一标识:
已修改完成。改动要点:
row-key="id"--- 给表格指定唯一行标识,让 Element Plus 能正确追踪每行:current-row-key="currentRowId"--- 绑定当前高亮行的 key,快速点击时表格会根据currentRowId的值正确维护高亮状态,避免出现多行同时高亮的问题
当前 selectMachineType 中 currentRowId.value = row.id ?? null 是同步执行的,在异步请求前就已经更新,因此 current-row-key 会立即生效。
已进行更改。