首頁 > 軟體

vue.js el-table虛擬捲動完整範例程式碼

2022-12-29 14:00:31

前言

基於Element-UI的Table 元件開發的虛擬捲動元件,支援動態高度,解決資料量大時捲動卡頓的問題

範例程式碼

<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[])

    // 可視區列表
    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>

總結

到此這篇關於el-table虛擬捲動的文章就介紹到這了,更多相關el-table虛擬捲動內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com