iview中如何解决下拉框数据太多,导致的页面卡,慢情况 (1)

项目背景:
发送告警信息需要配置通知人,但由于通知人分类,又划分用户、用户组、及钉钉微信等通知方式,而用户下面人数过多,全部渲染导致页面奇卡,用户体验很差,原来采用Cascader做选择配置,后面发现数据太多,且iview的Cascader没有监控搜索数据变化的方法,所以采用两个下拉框。
我在网上搜了,很少有这种搞法的,但是现在碰到了,顶着头皮也得上不是吗。
在这里插入图片描述

我想了一下解决方法
1、截取部分数据渲染dom,将全量数据在前台做缓存
2、支持用户搜索,获取用户在输入下拉框中输入的数据,在全量数据中搜索过滤,搜索之后进行还原

下面是具体的方案(忽略各种不规范的野鸡)
1、首先请求数据做缓存处理,减少后台请求次数,()

 initUser() {
      var contact_info = JSON.parse(
        this.$libs.getSessionStorage('contact_info')
      )
      if (contact_info && contact_info.length > 0) {
        var array = contact_info
        this.contactDataMap = this.$libs.arrayToMap(array, 'value', 'children')
        this.userListCache = this.contactDataMap['1']
        this.userMap = this.$libs.arrayToMap(
          this.userListCache,
          'value',
          'label'
        )
        // 
        if (this.userListCache.length < 1000) {
          this.userList = this.userListCache
        } else {
          this.userList = this.userListCache.slice(0, 1000)
        }
      }
    },

2、select 的下拉框添加 filterable 属性和 @on-query-change="filterUserList“ @on-change=“addUser” 方法

					<Select
                      v-model="formData.dispatchUser"
                      filterable
                      clearable
                      @on-query-change="filterUserList"
                      @on-change="addUser"
                      placeholder="请选择分派人"
                    >
                      <Option
                        v-for="item in userList"
                        :value="item.value.toString()"
                        :key="item.value.toString()">
                        {{ item.label }}</Option >
                    </Select>

// 新增联系人

 addUser(data) {
    // 根据用户id过滤掉已添加的用户
      if (data && data !== '') {
        var temp = this.formData.userList.filter(item => {
          if (data == item.staff_id) {
            return item
          }
        })
        if (temp.length < 1) {
          const user = {
            staff_id: data,
            name: this.userMap[data]
          }
          this.formData.userList.push(user)
        }
      }
    },
    // 在全量用户里搜索用户进行下拉框的渲染
      filterUserList: function(name) {
      if (name !== null && name !== '') {
        this.userList = []
        var arr = []
        var reg = new RegExp(name)
        const array = this.userListCache
        for (let index = 0; index < array.length; index++) {
          const element = array[index]
          if (element.label.match(reg)) {
            arr.push(element)
          }
        }
        this.userList = arr
      } else {
        if (this.userListCache.length < 1000) {
          this.userList = this.userListCache
        } else {
          this.userList = this.userListCache.slice(0, 1000)
        }
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值