您好,登錄后才能下訂單哦!
今天小編給大家分享一下vue.js el-table虛擬滾動效果怎么實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
基于Element-UI的Table 組件開發的虛擬滾動組件,支持動態高度,解決數據量大時滾動卡頓的問題
<template> <div ref="listWrap" @scroll="scrollListener" > <div ref="list"> <el-table @select="select" @select-all="selectAll" :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[]) // 可視區列表 const showList = computed(() => { return props.initList.slice(start.value, end.value) }) // 數據長度 const length = computed(() => { return props.initList.length }) // 滾動 const scrollListener = () => { // 獲取滾動高度 const scrollTop = listWrap.value.scrollTop // 開始的數組索引 start.value = Math.floor(scrollTop / props.itemHeight) // 結束索引 end.value = start.value + 10 list.value.style.transform = `translateY(${start.value * 65}px)` // 對列表項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>
以上就是“vue.js el-table虛擬滾動效果怎么實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。