vue table直接定位到指定元素的操作代碼
vue+element中的表格,直接定位到指定的元素。
需求:點擊某一個節(jié)點,彈窗,直接定位到點擊的節(jié)點,高亮并顯示數(shù)據(jù)。
<el-table ref="highTable" :data="treeData" highlight-current-row border default-expand-all row-key="'id" :tree-props="{children:'children',hasChildren:'hasChildren'}" :row-style="rowClassRender" :row-class-name="tableRowClassName">
...
</el-table>treeData是我的樹狀數(shù)據(jù),表格樹。
default-expand-all:是否默認展開所有行,當(dāng) Table 包含展開行存在或者為樹形表格時有效;
row-style:行的 style 的回調(diào)方法,也可以使用一個固定的 Object 為所有行設(shè)置一樣的 Style; //高亮顯示
row-class-name:行的 className 的回調(diào)方法,也可以使用字符串為所有行設(shè)置一個固定的 className。 //獲取index(我用的是樹狀數(shù)據(jù),如果是列表數(shù)據(jù)就不需要)
rowClassRender({
row
}) {
if (row.id === this.currentItemId) {
return {
'background-color': 'red'
}
} else {
return ''
}
},
tableRowClassName({
row,
index
}) {
//this.nodeItem是我最開始點擊的節(jié)點
if (row.id === this.nodeItem.id) {
this.currentIndex = index;
}
}注意:一定要在獲取數(shù)據(jù)之后去賦值!??!不然scrollTop一直為0?。。。?!
在獲取列表的代碼塊中:
let divT = this.$refs.hightTable;
this.$nextTick(()=>{
divT.scrollTop = 36 * this.currentIndex
})vue中table表格做定位處理

需求:前端根據(jù)搜索條件中的partNo的值,進行定位查詢到table列表中對應(yīng)的每一行
search(){
if(this.positionIndx.length==0){
this.tableData.forEach((item,index)=>{
if(item.partNo == this.queryForm.partNo){
this.positionIndx.push(index) // 定義一個空數(shù)組 positionIndx 用來保存相同partNo的下標;
}
})
}
if (this.tableData.length > 0) {
if (this.queryForm.partNo !== '') {
if (this.$refs['selectPartRefs']) {
let vmEl = this.$refs['selectPartRefs'].$el // selectPartRefs 是table中綁定的ref的值,一定要保持一致;
//計算滾動條的位置
const targetTop = vmEl.querySelectorAll('.el-table__body tr')[this.positionIndx[this.inPosition]].getBoundingClientRect().top // 找到table中的每一行利用下標來計算每一行dom元素的上部與瀏覽器的可視窗口的上面的高度距離;
const containerTop = vmEl.querySelector('.el-table__body').getBoundingClientRect().top
const scrollParent = vmEl.querySelector('.el-table__body-wrapper')
scrollParent.scrollTop = targetTop - containerTop;
this.inPosition++; // 首先在data中定義 inPosition = 0 ,每次點擊search按鈕的時候,讓下標進行++操作,以遍定位到下一個匹配的partNO;
if( this.inPosition >= this.positionIndx.length ){
this.inPosition = 0; // 所有的都遍歷完成以后,讓定位回到第一個匹配的partNo的行上,以此達到可以循環(huán)定位的效果;
}
}
} else {
this.$message({
type: 'error',
message:'Please enter the partNo of the query'
})
}
}
},

到此這篇關(guān)于vue table直接定位到指定元素的文章就介紹到這了,更多相關(guān)vue定位指定元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中對監(jiān)聽esc事件和退出全屏問題的解決方案
這篇文章主要介紹了vue中對監(jiān)聽esc事件和退出全屏問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue使用富文本編輯器vue-quill-editor的操作指南和注意事項
vue中很多項目都需要用到富文本編輯器,在使用了ueditor和tinymce后,發(fā)現(xiàn)并不理想,所以果斷使用vue-quill-editor來實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue使用富文本編輯器vue-quill-editor的操作指南和注意事項,需要的朋友可以參考下2023-05-05
vue3網(wǎng)絡(luò)請求添加loading過程
這篇文章主要介紹了vue3網(wǎng)絡(luò)請求添加loading過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

