2021-06-22 vue element el-select组件封装 支持懒加载 多选 回显

vue element el-select组件封装 支持懒加载 多选 回显

el-select数据过多处理方式

一种优化思路:

element-ui的select有一个remote-method,支持远程搜索,我们让服务端支持一下不就可以了,当然这是一种解决的方案。但是有时候这种方法对我并能够不适用,因为这样会出现回显问题,回显的时候是还需拿到所需option;

另一种优化思路:

下拉懒加载, 当select滚动到底部时, 你再去请求一部分数据, 加入到当前数据中.

假设我们有个下拉框是用来选择课程的:

<template>
  <el-select
    v-model="newValue"
    :laceholder="placeholderText"
    filterable
    remote
    clearable
    :disabled="disabled"
    :multiple="isMultiple"
    @change="handleChange"
    reserve-keyword
    :remote-method="remoteMethod"
    :style="styles"
    :loading="loading"
    v-el-select-loadmore="loadmore"
  >
    <el-option
      v-for="item in list"
      :key="item.index"
      :label="item.title"
      :value="item.id"
    >
    </el-option>
  </el-select>
</template>

javascript

var timer;
export default {
  props: {
    value: {},
    styles: {},
    condition: {},
    isMultiple: { default: false },
    placeholderText: { default: "请选择" },
    size: { default: "mini" },
    disabled: { default: false },
    autoLoad: { default: true },
    table: {
      type: String,
      required: true,
    },
  },
  // 自定义指令
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function() {
          /**
           * scrollHeight 获取元素内容高度(只读)
           * scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
           * clientHeight 读取元素的可见高度(只读)
           * 如果元素滚动到底, 下面等式返回true, 没有则返回false:
           * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
           */
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      },
    },
  },
  data() {
    return {
      newValue: this.isMultiple ? [] : "",
      list: [],
      loading: false,
      formData: {
        pageNum: 1,
        pageSize: 20,
      },
    };
  },
  watch: {
    value: function(val) {
      if (
        ((this.isMultiple && val.length != 0) ||
          (typeof this.isMultiple == "undefined" && val != "")) &&
        this.list.length == 0
      ) {
        this.getList();
      } else {
        this.newVal = val;
      }
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    loadmore() {
      this.formData.pageNum++;
      this.getList();
    },
    loadData() {
      if (this.autoLoad == false) return;
      this.getList();
    },
    getList() {
      this.loading = true;
      var path;
      switch (this.table) {
        case "course":
          path = "course/list";
          this.pk = "id";
          this.label = "title";
          break; // 内部 库内课程
        default:
          alert("缺少组件参数");
      }
      this.$http.fetch(path, this.formData, this.condition).then((res) => {
        console.log(res);
        // this.list = res.data;
        this.loading = false;
        this.list = [...this.list, ...res.data];
      });
    },
    handleChange() {
      this.$emit("input", this.newValue);
    },
    remoteMethod(query) {
      console.log(query);
      // 远程搜索 逻辑
      var url = "";
      if (this.table == "course") {
        url = "course/list";
      } else {
        return;
      }
      if (query !== "") {
        this.loading = true;
        clearTimeout(timer);
        timer = setTimeout(() => {
          this.$http.fetch(url, { search_title: query }).then((res) => {
            this.loading = false;
            this.list = res.data;
          });
        }, 500);
      } else {
        this.list = [];
      }
    },
  },
};

到这里组件就已经编写完成了,现在只要在使用页面调取组件就可以使用了

这里包括了 import 组件引用 还有一些配置
现在来看一下调用组件之后的页面

在这里插入图片描述
选择之后的页面
在这里插入图片描述
支持多选 单个删除 一键删除 回显
这样就做到了滚动懒加载, 具体细节在使用时修改.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值