亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue.js?el-table虛擬滾動完整實例代碼

 更新時間:2022年12月29日 11:28:07   作者:CryptoCode  
這篇文章主要給大家介紹了關(guān)于el-table虛擬滾動的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue.js具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

基于Element-UI的Table 組件開發(fā)的虛擬滾動組件,支持動態(tài)高度,解決數(shù)據(jù)量大時滾動卡頓的問題

實例代碼

<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ù)長度
    const length = computed(() => {
      return props.initList.length
    })

    // 滾動
    const scrollListener = () => {
      // 獲取滾動高度
      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)` // 對列表項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虛擬滾動的文章就介紹到這了,更多相關(guān)el-table虛擬滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解vue父子組件關(guān)于模態(tài)框狀態(tài)的綁定方案

    詳解vue父子組件關(guān)于模態(tài)框狀態(tài)的綁定方案

    這篇文章主要介紹了詳解vue父子組件關(guān)于模態(tài)框狀態(tài)的綁定方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • vant中的toast層級改變操作

    vant中的toast層級改變操作

    這篇文章主要介紹了vant中的toast層級改變操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue cli4下環(huán)境變量和模式示例詳解

    vue cli4下環(huán)境變量和模式示例詳解

    這篇文章主要介紹了vue cli4環(huán)境變量和模式示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題

    關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題

    這篇文章主要介紹了vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 解析VUE中nextTick是什么

    解析VUE中nextTick是什么

    nextTick是Vue提供的一個全局API,由于Vue的異步更新策略導(dǎo)致我們對數(shù)據(jù)的修改不會立刻體現(xiàn),在DOM變化上,此時如果想要立即獲取更新后的DOM狀態(tài),就需要使用這個方法,這篇文章主要介紹了解析VUE中nextTick,需要的朋友可以參考下
    2022-11-11
  • Vue3+Element-Plus?實現(xiàn)點擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能

    Vue3+Element-Plus?實現(xiàn)點擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能

    這篇文章主要介紹了Vue3+Element-Plus?實現(xiàn)點擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • Vue3中使用i18n,this.$t報錯問題及解決

    Vue3中使用i18n,this.$t報錯問題及解決

    這篇文章主要介紹了Vue3中使用i18n,this.$t報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue如何統(tǒng)一樣式(reset.css與border.css)

    vue如何統(tǒng)一樣式(reset.css與border.css)

    這篇文章主要介紹了vue如何統(tǒng)一樣式(reset.css與border.css),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解vue如何使用自定義指令

    詳解vue如何使用自定義指令

    在Vue的模板語法中,我們學(xué)了很多指令,當(dāng)然除了這些指令,Vue也允許我們自己定義自己的指令,所以本文就來和大家聊聊如何使用自定義指令吧
    2023-10-10
  • vue+element下拉列表默認(rèn)值問題

    vue+element下拉列表默認(rèn)值問題

    這篇文章主要介紹了vue+element下拉列表默認(rèn)值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論