el-select下拉框加载分页

  • 组件demo
  <template>
        <el-select v-model="val"
                  @change="onChange"
                  :clearable="clearable"
                  popper-class="account-select-dropdown"
                  placeholder="请选择"
                  filterable
                  remote
                  @clear="onClear"
                  @visible-change="onBlur"
                  :remote-method="remoteMethod"
                  :multiple="multiple"
                  :disabled="disabled">
          <el-option v-for="(item,index) in List"
                    :key="item.code + '_' + index"
                    :label="item.name"
                    :value="item.code">
          </el-option>
        </el-select>
      </template>

      <script lang="ts">
        import { Component, Vue, Model, Watch, Prop } from "vue-property-decorator";
        import { getARConfirmTerm } from "@/api/home";
        import _ from "lodash";

        @Component({ components: {} })
        export default class extends Vue {
          @Prop({ type: Boolean, required: false, default: false }) disabled;
          @Prop({ type: Boolean, required: false, default: false }) multiple;
          @Prop({ type: Boolean, required: false, default: true }) clearable;
          @Prop({ type: String }) org; // 组织
          @Prop({ type: String }) orderType;
          @Prop({ type: String }) name; // 名称

          @Model("change") value;
          val = null;
          @Watch("value")
          onChangeValue(value) {
            this.val = value;
          }
          List = [];
          timer = null;
          textKey = null;
          pageNum = 1; // 当前页码
          pageSize = 50; // 每页条数
          hasMore = true; // 是否有更多数据

          created() {
            this.val = this.value;
            this.getData();
          }
          mounted() {
            this.$nextTick(() => {
              const dropdownWrapper = this.$el.querySelector(
                ".account-select-dropdown .el-select-dropdown__wrap"
              );
              if (dropdownWrapper) {
                dropdownWrapper.addEventListener("scroll", this.handleScroll);
              }
            });
          }
          beforeDestroy() {
            const dropdownWrapper = this.$el.querySelector(
              ".account-select-dropdown .el-select-dropdown__wrap"
            );
            if (dropdownWrapper) {
              dropdownWrapper.removeEventListener("scroll", this.handleScroll);
            }
          }

          remoteMethod(query) {
            this.List = [];
            this.textKey = query;
            if (this.timer) {
              clearTimeout(this.timer);
            }
            this.timer = setTimeout(() => {
              this.getData();
            }, 300);
          }
          resetSearch() {
            if (!this.textKey) {
              return;
            }
            this.textKey = "";
            this.pageNum = 1;
            this.hasMore = true;
            this.List = [];
            this.getData();
          }
          onBlur() {
            if (this.List.length === 0) {
              setTimeout(() => {
                this.textKey = "";
                this.pageNum = 1;
                this.getData();
              }, 300);
            }
          }
          onClear() {
            this.textKey = "";
            this.pageNum = 1;
            this.List = [];
            this.getData();
          }
          // 客户滚动事件
          handleScroll(event) {
            const target = event.target;
            if (
              target.scrollTop + target.clientHeight >= target.scrollHeight &&
              this.hasMore
            ) {
              this.pageNum++;
              this.getData();
            }
          }
          getData() {
            if (this.org) {
              getARConfirmTerm({
                org: this.org,
                textKey: this.textKey,
                OrderType: this.orderType,
                page: this.pageNum,
                size: this.pageSize,
              }).then(res => {
                const data = res.data;
                this.List.push(...data);
                if (this.val) {
                  const tmp = this.List.find(el => el.code === this.val);
                  if (!tmp && this.val && this.name) {
                    this.List.unshift({
                      code: this.val,
                      name: this.name,
                    });
                  }
                  const tmp1 = _.uniqBy(this.List, "code");
                  this.List = tmp1;
                }
                if (data.length < this.pageSize) {
                  this.hasMore = false;
                }
              });
            }
          }

          onChange() {
            this.$emit("change", this.val);
            const tmp = this.List.find(el => el.code === this.val);
            this.$emit("changeObj", tmp);
          }
        }
      </script>

      <style lang="scss" scoped>

        .account-select-dropdown .el-select-dropdown__wrap {
          max-height: 200px;
          /* 根据需要设置 */
          overflow-y: auto;
          /* 启用滚动 */
          background: #03a9f4;
        }
      </style>
      
posted @ 2026-03-25 17:11  不完美的完美  阅读(27)  评论(0)    收藏  举报