el-table实现表格整行选中状态,背景颜色切换

html 复制代码
<el-table :data="tableData" @row-click="handleRowClick" :rowClassName="tableRowClassName" 
    :header-row-style="{color:'#333',fontWeight:'bold',fontSize:'14px'}" style="width: 100%"> 
    <el-table-column
        prop="totalPrice"
        label="现金总额"> 
    </el-table-column>
    <el-table-column
        prop="createTime"
        label="注册时间">
        <template slot-scope="scope">
            <div> 
                <el-switch"
                    v-model="scope.row.experienceFlag"
                    >
                </el-switch>
            </div>
        </template>
    </el-table-column>
    <el-table-column
        prop="address"
        label="消费签名时间">
        <template slot-scope="scope">
            <div>
                {{ scope.row.dateFlag?'限制':'不限制' }}
            </div>
        </template>
    </el-table-column>  
    <el-table-column
        prop="address"
        label="员工">
    </el-table-column> 
    <el-table-column
        prop="categoryName"
        label="获客渠道">
    </el-table-column> 
    <el-table-column 
        label="选择">
        <template slot-scope="scope">
            <div>
                <svg-icon iconClass="praise" v-show="scope.$index!=tableRowIndex" class="icons praise verticalMiddle" ></svg-icon>
                <span class="el-icon-check checkValTable" v-show="scope.$index==tableRowIndex"></span>
            </div>
        </template>
    </el-table-column> 
</el-table>
javascript 复制代码
<script>  
    export default{  
        data(){
            return{
                tableData:[],
                tableRowIndex:'-1'
            }
        },
        methods:{
            //选择行
            handleRowClick(row,rowIndex){ 
                //获取选择行的下标
                console.log(rowIndex);
                this.tableRowIndex=this.getArrayIndex(this.tableData,row)
                console.log('tableRowIndex');
                console.log(this.tableRowIndex);
                this.tableRowClassName(row,this.tableRowIndex);
            },
           //最终与下方获取到的数组下标,与选中的下标对比,若相等则将指定行增加highLight-row类名
            tableRowClassName({row,rowIndex}){
                if(rowIndex==this.tableRowIndex){ 
                    return 'highlight-row'
                }
            },
            getArrayIndex(arr,obj){
                var i=arr.length;
                //循环获取所选行的数据与表格列表数组的某个下标数据是否相等
                //若相等返回下标
                while(i--){
                    if(arr[i]===obj){
                        return i;
                    }
                }
                return -1;
            },
        }
    }
</script>
相关推荐
程序员大阳12 分钟前
副队长JavaScript教程(65)-详解JSON:轻量级数据交换格式
javascript·json·序列化·反序列化·stringify·parse
小大宇1 小时前
Vue知识点-ElementPlus-06 单选+多选+下拉+日期选择
前端·vue.js·elementui
paopaokaka_luck2 小时前
基于springboot+vue的益暖微光公益服务平台(DeepSeek公益助手、善款信息追溯、志愿服务积分闭环、ECharts运营分析、活动状态自动流转)
java·前端·javascript·spring boot·mysql·架构·echarts
栀椩2 小时前
Electron 内嵌页面跨域问题(CORS/PNA)解决全记录
前端·javascript·electron
SEO_juper2 小时前
Chrome 扩展一键查看当前页面 SEO 信息:不用切页面,点一下全出来
前端·javascript·chrome
Highcharts.js2 小时前
CSUN 2026|Highcharts无障碍数据与AI分享
javascript·人工智能·数据分析·highcharts·图表开发
系统集成-鹰翔3 小时前
ERP推单数据库怎么备份:SQLite WAL模式下只复制db文件为什么可能漏任务
javascript·数据库·经验分享·后端
小大宇4 小时前
Vue知识点-ElementPlus-05 输入框
前端·javascript·vue.js
软件技术NINI4 小时前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
外贸GEO优化4 小时前
外贸独立站的可抓取性:JS 渲染、懒加载与无限滚动到底影响什么
开发语言·javascript·ecmascript