vue.js?el-table虛擬滾動(dòng)完整實(shí)例代碼
前言
基于Element-UI的Table 組件開發(fā)的虛擬滾動(dòng)組件,支持動(dòng)態(tài)高度,解決數(shù)據(jù)量大時(shí)滾動(dòng)卡頓的問(wèn)題
實(shí)例代碼
<template>
<div
ref="listWrap"
style="height: 400px; overflow-y: scroll; margin-top: 20px; padding: 10px"
@scroll="scrollListener"
>
<div ref="list">
<el-table
@select="select"
@select-all="selectAll"
style="margin-top: 10px"
:data="showList"
ref="scrollTable"
>
<slot></slot>
</el-table>
</div>
</div>
</template>
<script lang="ts">
import { ref, onMounted, computed, watch, defineComponent, nextTick } from 'vue'
interface IProps {
start: number
end: number
height: number
itemHeight: number
rowKey: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initList: any[]
}
export default defineComponent({
name: 'Vue3VitualTable',
props: ['start', 'end', 'height', 'itemHeight', 'initList', 'rowKey'],
emits: ['handleSelect'],
setup(props: IProps, { emit }) {
// 表格
const listWrap = ref()
const list = ref()
const scrollTable = ref()
const start = ref(props.start)
const end = ref(props.end)
const isAllSelected = ref(false)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const selections = ref([] as any[])
// 可視區(qū)列表
const showList = computed(() => {
return props.initList.slice(start.value, end.value)
})
// 數(shù)據(jù)長(zhǎng)度
const length = computed(() => {
return props.initList.length
})
// 滾動(dòng)
const scrollListener = () => {
// 獲取滾動(dòng)高度
const scrollTop = listWrap.value.scrollTop
// 開始的數(shù)組索引
start.value = Math.floor(scrollTop / props.itemHeight)
// 結(jié)束索引
end.value = start.value + 10
list.value.style.transform = `translateY(${start.value * 65}px)` // 對(duì)列表項(xiàng)y軸偏移
nextTick(() => {
selections.value.forEach((ele) => {
scrollTable.value.toggleRowSelection(ele, true)
})
})
}
watch(length, (val) => {
if (val > 10) {
listWrap.value.style.height = props.itemHeight * 10 + 'px'
} else {
listWrap.value.style.height = props.itemHeight * val + 57 + 'px'
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleSelect = (val: any) => {
if (!isAllSelected.value) {
isAllSelected.value = scrollTable.value.store.states.isAllSelected.value
}
console.log('store.states.isAllSelected', scrollTable.value.store.states.isAllSelected.value)
emit('handleSelect', val)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const select = (val: any) => {
if (val.length < props.initList.length) {
isAllSelected.value = false
} else {
isAllSelected.value = true
}
selections.value = val
emit('handleSelect', selections.value)
console.log('select', val)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const selectAll = (val: any) => {
if (val.length) {
selections.value = props.initList
isAllSelected.value = true
} else {
selections.value = []
isAllSelected.value = false
}
emit('handleSelect', selections.value)
console.log('selectAll', val)
}
onMounted(() => {
console.log('onMounted')
})
return {
listWrap,
list,
scrollTable,
scrollListener,
showList,
length,
handleSelect,
selections,
select,
selectAll,
}
},
})
</script>

總結(jié)
到此這篇關(guān)于el-table虛擬滾動(dòng)的文章就介紹到這了,更多相關(guān)el-table虛擬滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中ref標(biāo)簽屬性和$ref的關(guān)系解讀
這篇文章主要介紹了vue中ref標(biāo)簽屬性和$ref的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined&nbs
這篇文章主要介紹了vue3編譯報(bào)錯(cuò)ESLint:defineProps is not defined no-undef的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue 指令實(shí)現(xiàn)按鈕級(jí)別權(quán)限管理功能
這篇文章主要介紹了Vue 指令實(shí)現(xiàn)按鈕級(jí)別權(quán)限管理功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue開發(fā)實(shí)現(xiàn)評(píng)論列表
這篇文章主要為大家詳細(xì)介紹了vue開發(fā)實(shí)現(xiàn)評(píng)論列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法
今天小編就為大家分享一篇vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue淺析axios二次封裝與節(jié)流及防抖的實(shí)現(xiàn)
axios是基于promise的HTTP庫(kù),可以使用在瀏覽器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包裝器,可以像插件一樣安裝使用:Vue.use(VueAxios,?axios),本文給大家介紹axios的二次封裝和節(jié)流與防抖2022-08-08

