elementUI下拉框分页加远程搜索组件封装

<template>
  <el-select v-model="copyValue"
             :disabled="disabled"
             filterable
             clearable
             remote
             @clear="selectClear"
             placeholder="请选择"
             @change="updateValue"
             :remote-method="remoteMethod"
             @focus="focusMethod">
    <el-option
      v-for="item in dataList"
      :key="item[valueKey]"
      :label="item[labelKey]"
      :value="item[valueKey]"
    >
    </el-option>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="pageInfo.pageNum"
      :page-size="pageInfo.pageSize"
      layout=" prev, pager, next,total"
      :total="pageInfo.total">
    </el-pagination>

  </el-select>
</template>

<script>
export default {
  name: "paginationSelect",
  props: {
    //绑定值
    value: String,
    //下拉列表
    dataList: Array,
    //option的label在列表中对应的key
    labelKey: String,
    //option的value在列表中对应的key
    valueKey: String,
    //分页信息
    pageInfo: Object,
    disabled: false,
    elSelectClass: Boolean,
  },
  watch: {
    value: {
      handler: function (val) {
        this.copyValue = val;
      },
      deep: true
    }
  },
  methods: {
    //更新值
    updateValue(val) {
      this.$emit('selectChange', this.copyValue);
    },
    remoteMethod (query) {
      this.copyValue = query;
      this.$emit('remoteSearch', query);
    },
    focusMethod () {
      if (!this.dataList || (this.dataList && this.dataList.length == 0)) {
        this.filterMethod();
      }
    },
    //清楚输入框内容
    selectClear () {
      this.$emit('selectPageChange', 1, this.pageInfo.pageSize, "clear");
    },
    //翻页
    handleCurrentChange(currenPageNo) {
      this.$emit('selectPageChange', currenPageNo, this.pageInfo.pageSize);
    }
  },
  data() {
    return {
      copyValue: this.value
    };
  }
};
</script>

<style scoped>
.elPaginationSelect {
  width: 100%;
  border: none !important;
}

.el-input input {
  border: none !important;
  width: 100%;
}

</style>

效果如下:
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用Element UI组件库中的`<el-select>`组件和`<el-option>`组件来实现下拉框分页搜索功能。以下是一个简单的示例代码: ```html <template> <div> <el-select v-model="selectedItem" filterable remote :remote-method="fetchData" :loading="isLoading"> <el-option v-for="item in items" :key="item.id" :label="item.name" :value="item.id"></el-option> </el-select> <el-pagination :current-page="currentPage" :page-size="pageSize" :total="total" @current-change="handleCurrentChange"></el-pagination> </div> </template> <script> export default { data() { return { selectedItem: null, items: [], total: 0, currentPage: 1, pageSize: 10, isLoading: false } }, methods: { fetchData(query) { this.isLoading = true; setTimeout(() => { // 发送ajax请求获取数据 // ... // 将获取到的数据设置到items和total中 this.items = [{id: 1, name: 'Option 1'}, {id: 2, name: 'Option 2'}]; this.total = 2; this.isLoading = false; }, 1000); }, handleCurrentChange(currentPage) { this.currentPage = currentPage; // 重新发送数据请求 this.fetchData(); } } } </script> ``` 在上面的代码中,我们使用`<el-select>`组件来展示下拉框,设置`filterable`属性为`true`表示启用搜索功能,设置`remote`属性为`true`表示启用远程搜索功能,然后使用`:remote-method`属性来绑定搜索方法`fetchData`。在`fetchData`方法中,我们可以使用`setTimeout`模拟异步请求数据的过程,并将获取到的数据设置到`items`和`total`中。同时,我们还使用了`<el-pagination>`组件来展示分页器,并使用`@current-change`事件来监听分页器页码的变化,然后重新发送数据请求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值