function scrollToColumn(str: string) {
if (!str) {
ElMessage({
showClose: true,
message: '请先输入条件',
type: 'warning',
});
return;
}
try {
const rowHeightList = [];
//下面都可以使用
let temp = tableRef.value.$el.getElementsByClassName('el-table__row'); //获取到的都是元素
// let temp = tableRef.value.$el.querySelectorAll('.el-table__row');//获取到的都是元素
for (let i = 0; i < temp.length; i++) {
const item = temp[i];
rowHeightList.push(item.scrollHeight);
}
let itemRow= {}; //存放当前行的所有数据
let rowIndex = 0; //选中行位于第几行
for (let i = 0; i < answerCompareArr.value.length; i++) {
const item = answerCompareArr.value[i];
// 判断查询的数据是否存在,存在则进行定位操作
if (item.questionno == str) {
itemRow= item;
rowIndex = i;
break;
}
}
let totalHeight = 0; //求出选中行之前的的高度之和,需要注意的是,当前行的高度不能包含进去
for (let index = 0; index < rowHeightList.length; index++) {
const row = rowHeightList[index];
if (index < rowIndex) {
totalHeight += row;
}
}
// 滚动到指定行
tableRef.value.setScrollTop(totalHeight);
tableRef.value.setCurrentRow(itemRow);
} catch (error) {
console.error('错误', error);
}
}
vue3 el-table滚动到指定行
最新推荐文章于 2024-05-20 10:06:54 发布