Vue2+ElementUI el-table初始化时定位并高亮指定行

该文章描述了一个Vue.js项目中的需求,即当从特定项跳转到审核页面时,需要在待审核列表中高亮显示该选择项,并确保如果该选择项不在可视区域内,则自动滚动列表使其可见。通过使用`el-table`组件,结合`current-change`事件和DOM操作实现了这一功能。

需求描述

项目里遇到了这样一个需求: 从指定项跳转到审核页时,需要在待审核列表里高亮显示所选项,若该项在列表可视区域之外,还需要将列表滚动至该项出现在可视区域内

预期效果

image.png

image.png

实现

<el-table
        ref="couseList"
        :data="showList"
        style="width: 100%; font-size: 12px;"
        height="98%"
        highlight-current-row
        @current-change="handleChooseRow">
    <el-table-column>
    ...
    </el-table-column>
</el-table>    

js部分:

export default{
    data(){
        return{
                examinedCourseId: null,
                showList: [],
                textList: [
                {
                    id: 0,
                    name: "xxx"
                },
                //...
            ]
        }
    },
    mounted(){
        if(typeof(this.$route.params.id)=='undefined'){
            // ...
        }else{
            this.examinedCourseId = this.$route.params.id
            console.log(this.editedCourseId)
            this.getList(this.examinedCourseId).then((res)=>{
                if (res) {
                    // 模拟审核
                    this.scrollToGoalRow(12)      
                }
            })
        }
        this.showList = this.testList
    },
    methods: {
        getList(id){
            // 模拟从后端获取数据
            return new Promise((resolve, reject) => {
                setTimeout(()=>{
                    resolve(true)
                }, 200)
            })
        },
        handleChooseRow(currentRow, oldCurrentRow){
            this.examinedCourseId = currentRow.id
        },
        srollToGoalRow(id){
            let index = this.testList.findIndex((item) => item.id == id)
            let courseListEL = this.$refs.couseList.$el
            
            if(courseListEL){
                const goalRowTop = courseListEL.getElementsByClassName("el-table__row")[index].getBoundingClientRect().top
                const listTop = courseListEL.querySelector('.el-table__body').getBoundingClientRect().top
                
                if(goalRowTop - listTop - 483 > 0){
                    this.$refs.couseList.setCurrentRow(this.$refs.couseList.data[index])
                    this.$refs.couseList.$refs.bodyWrapper.scrollTop = goalRowTop - listTop
                }
            }
        }
    }
}

高亮样式

::v-deep{
    .el-table{
        .el-table__body tr.current-row>td{
            background: #6785f1bf !important;
            color: white;
            font-weight: 600;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六时二一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值