在使用vxetable中的表格组件时,有时会遇到修改点击高亮行背景色的情况。要修改高亮行背景色只需要以下两步即可
- 根据current-change 事件获取并记录当前点击高亮行的点击索引
-
根据row-style 属性绑定的方法,根据回调参数{rowIndex} ,判断如果当前索引和记录点击高亮行的索引相同,则
根据传入的高亮背景色,对行样式进行设置即可
// 简单示例 <template> <vxe-table :row-style="rowStyle" @current-change="onCurrentChange"></vxe-table> </template> <script> import { reactive, toRefs, defineComponent, PropType, computed, ref, watch, nextTick } from 'vue'; const state = reactive({ highlightIndex:0, highlightRowBgColor:'red' }) /** * @description: 当切换选中行时 * @return {*} */ const onCurrentChange = ({ rowIndex }: any) => { state.highlightIndex = rowIndex; }; /** * @description: 动态设置表格内容样式 * @return {*} */ const rowStyle = ({ rowIndex }: any) => { const rowStyleMap = { } as any; // 如果当前开启高亮行选择,高亮行权重比奇偶行设置的权重高 if (state.highlightIndex === rowIndex) { rowStyleMap.backgroundColor = state.highlightRowBgColor; } return rowStyleMap; }; </script>