VUE下拉选择分页,远程搜索

实现效果

实现思路

  1. 初始化加载第一页;
  2. 监听下拉框的滚动事件,当滚动到底部的时候加载下一页;
  3. 输入搜索时,重置为第一页加载;
  4. 关闭下拉选择框时,判断如果存在搜索值,要清空搜索值、并加载第一页。

实现代码

html

html 复制代码
<a-select
    v-model:value="values"
    :mode="multiple"
    :show-search="true"
    :filter-option="false"
    :default-active-first-option="false"
    @popupScroll="onPopupScroll"
    @search="onSearch"
    @dropdownVisibleChange="onDropdownVisibleChange"
>
    <a-select-option v-for="item in optionList" :key="`${item.value}-${refreshFlag}`" :value="item.value">
        <div :title="item.label">{{ item.label }}</div>
    </a-select-option>
    <a-select-option v-if="loading" disabled value="loading">
        <a-spin :spinning="true"></a-spin>
    </a-select-option>
</a-select>

ts

typescript 复制代码
const optionList = ref<{ label: string; value: string }[]>([]);
const loading = ref<boolean>(false);
const keyWord = ref<string>('');
const curPage = ref<number>(0);
const total = ref<number>(0);
const refreshFlag = ref<number>(1);

// 最终搜索接口
const onRelationSearch = debounce(async (page = 1, curlist = []) => {
    try {
         const params: any = {
             condition: {
             	keyWord: keyWord.value,
             },
             paging: {
                 pageIndex: page,
                 pageSize: 20,
             },
         };
         const res = await oneFunction(params);
         const dataList = res?.result || [];
         optionList.value = [
             ...curlist,
             ...dataList
                 .map((item: any) => {
                     return {
                         label: item.name,
                         value: item.id,
                     };
                 });
         ];
         refreshFlag.value++;
         curPage.value = page;
         total.value = res?.result?.pageResult?.totalCount || 0;
    } finally {
        loading.value = false;
    }
}, 300);

// 搜索前置判断
const getListBefore = (page: number, list: { label: string; value: string }[]) => {
    // _props传入 分页, 现有列表,输入框内容
    // 当不是第一页切 列表数量大于等于总数时终止掉
    if (page !== 1 && list.length >= total.value) {
        message.warning('没有更多数据了');
    } else if (loading.value) {
        // 如果处于加载中也终止掉
        return;
    }
    // 请求,加载中
    loading.value = true;
    // 请求接口
    onRelationSearch(page, list);
};

// 滚动事件
const onPopupScroll = (event: any) => {
    const { target } = event;
    if (target.scrollTop + target.offsetHeight === target.scrollHeight) {
        // 调用加载数据,传入需求请求的分页
        getListBefore(curPage.value + 1, optionList.value);
    }
};

// 搜索事件
const onSearch = (value: any) => {
    loading.value = false;
    optionList.value = [];
    keyWord.value = value;
    getListBefore(1, []);
};

const onDropdownVisibleChange = debounce((open: boolean) => {
    if (!open && keyWord.value) {
        loading.value = false;
        keyWord.value = '';
        optionList.value = [];
        getListBefore(1, []);
    }
}, 300);
相关推荐
xiaofeichaichai40 分钟前
Webpack
前端·webpack·node.js
问心无愧05131 小时前
ctf show web入门111
android·前端·笔记
唐某人丶1 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界1 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌2 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel3 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3113 小时前
https连接传输流程
前端·面试
徐小夕3 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
梦梦代码精3 小时前
2026年PHP开源商城系统实测对比:架构、多商户、商用授权,谁才是真·省心?
vue.js·docker·架构·开源·代码规范