效果:

- html
<el-table
:data="data"
border
style="width: 100%"
@row-click="selectRow" //给行添加鼠标点击事件
:row-style="rowStyle" // 设置行样式
>
- js
// 行鼠标点击事件
selectRow(row, column, event) {
console.log(row);
console.log(column);
console.log(event);
this.name = row.name;
},
// 更改选中行背景色
rowStyle({ row }) {
if (this.name === row.name) {
return { 'background-color': '#F7EDED', cursor: 'pointer' };
}
return { cursor: 'pointer' };
},
- css
取消表格鼠标进入高亮显示,要不然会影响rowStyle中自定义的行背景色效果
// 取消表格鼠标进入高亮显示
.el-table__row:hover > td {
background-color: transparent;
}
本文介绍了如何在Vue.js中使用Element UI的el-table组件实现行点击事件和自定义选中行背景颜色。通过row-click和rowStyle方法,展示了如何跟踪行数据并修改样式,同时取消了表格的鼠标悬停高亮以保持定制背景的清晰可见。

2117

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



