vue使用ant design Vue中的a-select组件实现下拉分页加载数据

<a-form-model-item

          :labelCol="labelCol"

          :wrapperCol="wrapperCol"

          prop="equipmentTypeId"

          label="所属设备种类">

          <a-select v-model="model.equipmentTypeId" @popupScroll="handlePopupScroll" placeholder="请选择所属设备种类 " mode="multiple">

            <a-select-option v-for="(item, index) in typeList" :value="item.id" :key="item.id">

              {{ item.equipmentTypeName }}

            </a-select-option>

          </a-select>

        </a-form-model-item>

import { getDeviceTypeList } from '@/api/home'

export default {

   data () {

        typeList: [],

        pageObj: {

          pageNo: 1,

          pageSize: 10,

          pages: 0, // 总页数

          total: 0, // 总条数

        }

   }

}

methods: {

      // 下拉列表滚动时的回调

      handlePopupScroll (e) {

        console.log(e)

        if (this.pageObj.pageNo >= this.pageObj.pages) return;

        const { target } = e;

        const { scrollTop, scrollHeight, clientHeight } = target;

        if (scrollTop + 2 + clientHeight >= scrollHeight) {

          // 已经到达底部,触发分页逻辑

          // todo 这里就可以触发加载下一页请求  具体函数根据当前业务需求来定

          this.pageObj.pageNo = ++this.pageObj.pageNo;

          this.pageObj.pageSize+1

          this.getTypeList(true)

        }

      },

      getTypeList(isScroll = false) {

        let param = {

          pageNo: this.pageObj.pageNo,

          pageSize: this.pageObj.pageSize

        }

        getDeviceTypeList(param).then((res) => {

          if (res.success) {

            this.pageObj.pages = res.result.pages;

            this.typeList = isScroll == false ? [] : this.typeList;

            res.result.records.forEach((e) => {

              // 成功之后的数据应该push进数组中,而不是直接等于,否则就是下拉换页的效果了。

              this.typeList.push(e);

            });

          }

        })

      },

}

 

### 关于 `ant-design-vue` Select 组件实现加载 在 `ant-design-vue` 中,Select 组件可以通过配置特定属性来支持数据的懒加载。这通常涉及到当用户输入或打开下拉菜单时动态获取选项列表。 #### 使用 `showSearch` 和 `filterOption` 为了触发懒加载行为,可以启用 `showSearch` 属性让组件能够响应用户的键盘输入事件,并通过自定义函数处理这些事件以发起异步请求获取远程数据[^1]。 ```vue <template> <a-select show-search :loading="loading" @search="handleSearch" placeholder="请输入关键字查询" option-label-prop="children" style="width: 200px;" > <!-- 动态渲染选项 --> <a-select-option v-for="(item, index) in options" :key="index">{{ item.label }}</a-select-option> </a-select> </template> <script> export default { data() { return { loading: false, options: [] }; }, methods: { handleSearch(value) { this.loading = true; setTimeout(() => { // 模拟网络延迟后的回调操作 const result = fetchOptionsFromServer(value); // 假设这是从服务器端取回的数据方法 this.options = result.map(item => ({ label: item.text })); this.loading = false; }, 300); } } }; </script> ``` 上述代码展示了如何监听搜索框内的变化并执行相应的逻辑去更新可选项目列表。这里假设存在一个名为 `fetchOptionsFromServer()` 的服务接口用于模拟向后端发送请求的过程,在实际应用中应当替换为此处省略的真实API调用[^2]。 另外需要注意的是,对于分页场景下的懒加载,则需额外关注页面切换时重新加载对应页码的数据集;而对于无限滚动模式,则要计算当前可视区域内最后一条记录的位置以便决定何时触发展开更多项的操作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值