el-table表格行取消高亮

本文介绍如何在 Vue 中使用 Element UI 的 el-table 组件实现表格行的高亮显示和取消高亮功能。通过设置 highlight-current-row 属性和监听 row-click 与 current-change 事件,结合自定义逻辑实现单选行切换时的高亮效果。同时,提供了取消高亮的代码示例,通过 setCurrentRow 方法将行设置为 -1 实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

表格行选中高亮 highlight-current-row

 <el-table
      ref="singleTableRef"
      v-loading="loading"
      :data="menuList"
      border
      row-key="menuId"
      :default-expand-all="isExpandAll"
      highlight-current-row // 表格行高亮
      @row-click="handleClick" // 行选择事件
      @current-change="handleCurrentChange" //当前行选择
    >

取消高亮:setCurrentRow()

proxy.$refs.singleTableRef.setCurrentRow(-1)

由于current-change事件只选中一次,因此需要同时使用row-click

let isFirst = ref(false)
let nowRow = ref(false) //此变量保证进行单选行切换时可以成功高亮
function handleCurrentChange(currentRow,oldCurrentRow) {
  if (oldCurrentRow!== undefined && currentRow.menuId !== oldCurrentRow?.menuId) {
    isFirst.value = true
    nowRow.value = true
  }
}
function handleClick(row, column, event) {
  if (!isFirst.value || nowRow.value) {
    isFirst.value = true
    nowRow.value = false
  }else{
    isFirst.value = false
    proxy.$refs.singleTableRef.setCurrentRow(-1)
  }
}
### 取消 Element UI `el-table` 排序时的默认高亮样式 对于取消 `el-table` 排序时的默认高亮样式,可以采取调整列配置的方法来实现。具体来说,在 `el-table-column` 组件中存在一个名为 `columnConfig.order` 的属性用于控制排序的状态。当此属性被设为空字符串或未定义时,则能够有效移除排序后的高亮效果[^1]。 ```javascript // 假定这是初始化表格列的部分代码片段 const columns = [ { prop: 'name', label: '姓名', sortable: true, columnConfig: { order: '' // 将order置空以清除排序高亮 } }, ... ]; ``` 另外一种方式涉及更深入的操作 CSS 类名机制。如果上述方法未能满足需求,还可以尝试重写 `.is-sorting` 或者其他关联到排序样式的类的选择器,从而达到隐藏或改变原有视觉表现的目的。不过这种方法可能会影响全局样式的一致性,因此需谨慎处理[^2]。 最后值得注意的是,Element UI 提供了一些内置功能帮助开发者更好地管理表格交互为,比如通过设置 `highlight-current-row` 属性可让某一处于选中状态并带有特殊标记;然而这与排序操作无直接关系,仅作为额外的信息提供给读者了解整个库的功能范围[^3]。 #### 实现示例 下面给出一段简单的 Vue 单文件组件例子展示如何应用以上提到的技术: ```html <template> <div class="table-container"> <!-- 使用 :default-sort 来指定初始排序 --> <el-table :data="tableData" style="width: 100%" @sort-change="handleSortChange"> <el-table-column v-for="(item, index) in columns" :key="index" :prop="item.prop" :label="item.label" :sortable="item.sortable" :column-config="{ order: item.columnConfig && item.columnConfig.order || null}"> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [...], // 表格数据源 columns: [{ prop: 'date', label: '日期', sortable: false }, { prop: 'name', label: '姓名', sortable: true, columnConfig: { order: '' } }] }; }, methods: { handleSortChange(sortInfo){ console.log('Sorting changed:', sortInfo); this.columns.forEach(col => { if (col.prop === sortInfo.prop) { col.columnConfig.order = sortInfo.order; } else { col.columnConfig.order = ''; } }); } } }; </script> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值