vue+element实现下拉框的远程搜索功能

本文介绍了如何使用Vue.js结合ElementUI组件库,实现下拉框的远程搜索功能。通过定义template、computed属性、data以及methods,实现了从服务器获取并展示搜索结果的功能。
摘要由CSDN通过智能技术生成

template

<el-dialog title="添加用户" class="adduser" :visible.sync="showaddUser" width="500px" @close='close("addUser")' :close-on-click-modal="false"
      :close-on-press-escape="false">
  <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
    <el-form-item prop="nickNameList">
      <el-select
      ref="select"
        v-model="ruleForm.nickNameList"
        multiple
        filterable
        collapse-tags
        remote
        @focus="focus"
       :remote-method="remoteMethod"
        style="width:90%;margin-left:20px"
        placeholder="请选择用户名,
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Vue Element远程搜索下拉框虚拟列表组件。此组件包括以下功能: - 支持远程搜索 - 支持虚拟滚动 - 支持选项 ```html <template> <el-select v-model="selected" filterable remote reserve-keyword remote-method="remoteMethod" :loading="loading" :options="options" popper-class="virtual-list-popper" class="virtual-list-select" > <template #default="{option}"> <div class="virtual-list-item">{{ option.label }}</div> </template> </el-select> </template> <script> export default { name: "VirtualListSelect", data() { return { loading: false, options: [], selected: null, pageSize: 50, pageNum: 1, total: 0, keyword: "", }; }, methods: { async remoteMethod(keyword) { this.loading = true; this.keyword = keyword; this.pageNum = 1; await this.loadData(); this.loading = false; }, async loadData() { const { data } = await this.fetchData(); this.options = data; this.total = data.length; }, async fetchData() { const start = (this.pageNum - 1) * this.pageSize; const end = start + this.pageSize; const response = await fetch( `https://api.example.com/search?q=${this.keyword}&start=${start}&end=${end}` ); const json = await response.json(); return json; }, handleScrollToEnd() { if (this.options.length < this.total) { this.pageNum++; this.loadMoreData(); } }, async loadMoreData() { const { data } = await this.fetchData(); this.options.push(...data); }, }, mounted() { const popper = document.querySelector(".virtual-list-popper"); popper.addEventListener("scroll", () => { const scrollTop = popper.scrollTop; const scrollHeight = popper.scrollHeight; const clientHeight = popper.clientHeight; if (scrollHeight - scrollTop === clientHeight) { this.handleScrollToEnd(); } }); }, }; </script> <style> .virtual-list-select .el-input__suffix { right: 10px; } .virtual-list-item { height: 50px; line-height: 50px; padding: 0 20px; border-bottom: 1px solid #ebeef5; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .virtual-list-popper { max-height: 300px; overflow-y: auto; } </style> ``` 使用示例: ```html <virtual-list-select /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值