ant的a-select远程搜索加分页

a-select扩展菜单使用 dropdownRender 插槽并且增加搜索调后端接口,整理一下

1.template中


          <a-select
            show-search
            :default-active-first-option="false"
            :showArrow="false"
            @change="getName"
            :filter-option="false"
            @search="search"
            placeholder="请输入姓名"
            v-decorator="['name', validatorRules.name]"
          >
            <a-select-option
              v-for="item in customerList"
              :key="item.customerId"
              :value="item.customerId"
              >{
  { item.customerName }}</a-select-option
            >
            //a-select扩展菜单使用 dropdownRender 插槽
            <div slot="dropdownRender" slot-scope="menu">
              <v-nodes :vnodes="menu" />
              <a-divider style="margin: 4px 0" />
              <div
                style="display: flex; justify-content: space-between; align-items: center"
              >
                <div
                  style="padding: 4px 8px; cursor: pointer"
                  @mousedown="(e) => e.preventDefault()"
                  @click="getCustomerList('pre')"
                  v-show="nowPage"
                >
                  上一页
                </div>
                <div>当前第{
  { nowPage }}页</div>
                <div
                  style="padding: 4px 8px; cursor: pointer"
                  @mousedown="(e) => e.preventDefault()"
                  @click="getCustomerList('next')"
                  v-show="nowPage + 1 !== maxPage"
                >
                  下一页
                </div>
              </div>
            </div>
          </a-select>

2.data

//只展示必要字段

components: {
    VNodes: {
      functional: true,
      render: (h, ctx) => ctx.props.vnodes,
    },
  },
data() {
//防抖
    this.search = debounce(this.search, 800);
    return {
      nowPage: 1,
      maxPage: 1,

3.methods

//通过查询出来的名字,接口返回其他内容 来赋值给下面其他字段
getName(id) {
      this.$nextTick(() => {
        let obj = {};
        this.customerList.map((item) => {
          if (item.customerId == id) {
            obj = { ...item, age: item.customerAge };
            this.name = item.customerName;
          }
        });
        this.form.setFieldsValue(obj);
      });
    },
    search(val) {
      this.searchContent = val;
      this.nowPage = 1;
      this.getCustomerList();
    },
    getCustomerList(type) {
      if (type == "pre" && this.nowPage > 1) {
        this.nowPage -= 1;
      } else if (type == "next") {
        this.nowPage += 1;
      }
//搜索条件为空则不查询,因为后端数据太大
      if (!this.searchContent) {
        this.customerList = [];
        return;
      }
      getAllCustomerList({
        pageNo: this.nowPage,
        pageSize: 10,
        phone: this.searchContent,
      }).then((res) => {
        if (res.code == 200) {
          this.customerList = res.result.records;
          this.maxPage = res.result.pages;
        }
      });
    },

4.防抖

export function debounce(fn, delay) {
    let timer = null; // 形成闭包
    return function () {
      if (timer) {
        clearTimeout(timer); // 防抖
      }
      timer = setTimeout(() => {
        fn(); // 执行传入的函数
      }, delay);
    };
  }

5.效果

相关推荐

  1. vue系列:使用vue3、ant-d,a-select下拉搜索功能

    2023-12-18 03:18:01       36 阅读
  2. el-select二次封装实现可载数据

    2023-12-18 03:18:01       61 阅读
  3. select滑动请求数据

    2023-12-18 03:18:01       51 阅读
  4. Compose中使用paging3进行列表载Room中数据

    2023-12-18 03:18:01       63 阅读
  5. 【uniapp】Vue3移动端滚动组件封装

    2023-12-18 03:18:01       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-18 03:18:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-18 03:18:01       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-18 03:18:01       87 阅读
  4. Python语言-面向对象

    2023-12-18 03:18:01       96 阅读

热门阅读

  1. 构建一个简单的 npm 验证项目

    2023-12-18 03:18:01       59 阅读
  2. uniAPP里面有router吗

    2023-12-18 03:18:01       53 阅读
  3. PHP代码审计之实战审代码篇1

    2023-12-18 03:18:01       51 阅读
  4. leetcode 572. 另一颗树的子树

    2023-12-18 03:18:01       71 阅读
  5. arcgis图层样式应用geoserver问题

    2023-12-18 03:18:01       68 阅读
  6. unknown error 1146

    2023-12-18 03:18:01       55 阅读
  7. Mysql(事务)

    2023-12-18 03:18:01       79 阅读