解决ant-vue SELECT框渲染大量数据卡顿问题

Ant Design of Vue a-select下拉框因为数据量太大造成卡顿的问题
解决方案:

最开始渲染时只渲染总数据前100条数据以保证不卡顿,然后当需要搜索的时候对从后台拿到的数据进行过滤,也只取前100条,然后通过select下拉框popupScroll事件,下拉列表滚动时的回调,每次回调时都添加一部分数据来解决下拉框的卡顿问题。

Ant Design of Vue 官网文档事件方法:
在这里插入图片描述
效果图:
在这里插入图片描述
代码部分:

html:

<a-select
    allowClear
    mode="multiple"
    placeholder="归属业务"
    style="width: 100%"
    :value="sourceOwnerSystems"
    @change="onTreeSelect"
    @popupScroll="handlePopupScroll"
    @search="handleSearch"
  >
    <a-select-option v-for="frontSelect in frontDataZ.filter(item=>item.sourceOwnerSystems)" :key="frontSelect.sourceOwnerSystems"> //过滤一下数据是否为空
      {{ frontSelect.sourceOwnerSystems }}
    </a-select-option>
</a-select>

js:

(1).data中定义变量与数组

data(){
  return {
    dataZ: [],//总数据(不会改变)
    frontDataZ: [], //存放前100的数据
    sourceOwnerSystems: [],
    valueData: '',
    treePageSize: 100,
    scrollPage: 1
  }
}

(2).methods:
//通过接口获取数据

//通过接口获取数据showTabelCiList () {
  listSimpleInfos({sourceOwnerSystems: this.sourceOwnerSystems}).then(res => {
    if (res && res.data) {
      this.dataZ = res.data
      this.frontDataZ = res.data.slice(0, 100)
    }
  }).catch(e => {
    this.$message.error('查询维护信息失败:' + e)
  })
},

handleSearch (val) {
  this.valueData = val
  if (!val) {
    this.showTabelCiList()
  } else {
    this.frontDataZ = []
    this.scrollPage = 1
    this.dataZ.forEach(item => {
      if (item.instanceId.indexOf(val) >= 0) {
        this.frontDataZ.push(item)
      }
    })
    this.allDataZ = this.frontDataZ
    this.frontDataZ = this.frontDataZ.slice(0, 100)
  }
},

//下拉框下滑事件
handlePopupScroll (e) {
  const { target } = e
  const scrollHeight = target.scrollHeight - target.scrollTop
  const clientHeight = target.clientHeight
  // 下拉框不下拉的时候
  if (scrollHeight === 0 && clientHeight === 0) {
    this.scrollPage = 1
    console.log(this.scrollPage)
  } else {
    // 当下拉框滚动条到达底部的时候
    if (scrollHeight < clientHeight + 5) {
      this.scrollPage = this.scrollPage + 1
      const scrollPage = this.scrollPage// 获取当前页
      const treePageSize = this.treePageSize * (scrollPage || 1)// 新增数据量
      const newData = [] // 存储新增数据
      let max = '' // max 为能展示的数据的最大条数
      if (this.dataZ.length > treePageSize) {
        // 如果总数据的条数大于需要展示的数据
        max = treePageSize
      } else {
        // 否则
        max = this.dataZ.length
      }
      // 判断是否有搜索
      if (this.valueData) {
        this.allDataZ.forEach((item, index) => {
          if (index < max) { // 当data数组的下标小于max时
            newData.push(item)
          }
        })
      } else {
        this.dataZ.forEach((item, index) => {
          if (index < max) { // 当data数组的下标小于max时
            newData.push(item)
          }
        })
      }

      this.frontDataZ = newData // 将新增的数据赋值到要显示的数组中
    }
  }
}

官方文档: https://www.antdv.com/components/select-cn/#API

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ant-design-vueselect组件提供了多种样式配置,您可以通过设置不同的属性来实现自定义样式。以下是一些常用的样式配置: 1. 修改下拉的宽度:使用`dropdownStyle`属性,设置`width`属性。 ```html <a-select v-model="selectedValue" :dropdownStyle="{ width: '200px' }"> <a-select-option v-for="option in options" :key="option.value" :value="option.value"> {{ option.label }} </a-select-option> </a-select> ``` 2. 修改下拉中选项的样式:使用`option-template`属性,自定义选项的样式。 ```html <a-select v-model="selectedValue" :option-template="optionTemplate"> <a-select-option v-for="option in options" :key="option.value" :value="option.value"> {{ option.label }} </a-select-option> </a-select> <template #optionTemplate="{ option }"> <div class="custom-option"> {{ option.label }} </div> </template> <style> .custom-option { color: red; font-weight: bold; } </style> ``` 3. 修改选中的选项的样式:使用`selected-template`属性,自定义选中的选项的样式。 ```html <a-select v-model="selectedValue" :selected-template="selectedTemplate"> <a-select-option v-for="option in options" :key="option.value" :value="option.value"> {{ option.label }} </a-select-option> </a-select> <template #selectedTemplate="{ option }"> <div class="custom-selected"> {{ option.label }} </div> </template> <style> .custom-selected { color: blue; font-weight: bold; } </style> ``` 以上是一些常用的样式配置,您可以根据自己的需求进行自定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值