当前位置: 首页 > news >正文

Vue3搭配Element Plus 实现候选搜索框效果

直接上代码

    <el-col :span="14" class="ipt-col"><el-input v-model="projectName" class="w-50 m-2"  @input="inputChange" @focus="inputFocusFn" @blur="inputBlurFn" placeholder="请输入项目名称" clearable><template #suffix><el-icon class="el-input__icon"><search /></el-icon></template></el-input><!-- 搜索候选框 --><div v-show="isShow" class="hou-bu-box">//selectedCity这个是选中候选框数据的处理方法 <p v-for="(item,index) in cityArr" :key="index" @click="selectedCity(item.id)" st    yle="cursor: pointer">{{ item.label }}</p></div></el-col>

 <el-input v-model="projectName" class="w-50 m-2"  @input="inputChange" @focus="inputFocusFn" @blur="inputBlurFn" placeholder="请输入项目名称" clearable>

解释一下 在值改变时 将数据进行过滤 失去焦点隐藏候选框 获取焦点将完整数据渲染到候选框中

// 搜索框数据
const projectName = ref(null)//控制候选框显示隐藏
const isShow = ref(false)// 渲染到候选框的数据
const cityArr = ref(null)// 搜索框Change事件
const inputChange = () => { //搜索框值为空 候选框关闭if (projectName.value == '') {isShow.value = false} else {//输入框输入的时候 遍历总数据 将过滤出来的数据放入其中if (cityOptions.value.length > 0) { cityArr.value = []cityOptions.value.forEach((item, index, array) => {if (item.label.indexOf(projectName.value) >= 0) { cityArr.value.push(item)}})// cityOptions.value = cityArr}isShow.value = true// getTreeListFn()}
}
// 搜索框聚焦事件 请求跟下面获取总数据请求一样 不要问我为什么不直接调用下面的方法 因为我这个是项目里方法 好多数据我都删除了 下面的请求里面有好多逻辑处理 不好直接调用 我就又书写了一遍
const inputFocusFn = () => { isShow.value = truelet params = {name: projectName.value}getTreeList(params).then(res => {if (res.code == 200) {cityArr.value = res.data}})//   getTreeListFn()
}
// 搜搜框失焦事件
const inputBlurFn = () => { isShow.value = false
}// 获取总的项目树数据
function getTreeListFn() { let params = {name: projectName.value}getTreeList(params).then(res => { if (res.code == 200) { //候选框总数据cityOptions.value = res.data}})
}
//selectedCity这个是选中候选框数据的处理方法 
const selectedCity = (id) => { //处理逻辑}

http://www.lryc.cn/news/170007.html

相关文章:

  • 进程间的通信方式
  • 分类预测 | Matlab实现基于MIC-BP-Adaboost最大互信息系数数据特征选择算法结合Adaboost-BP神经网络的数据分类预测
  • phpcms v9对联广告关闭左侧广告
  • 7.2.4 【MySQL】匹配范围值
  • 1400*C. No Prime Differences(找规律数学)
  • Python基础之装饰器
  • IDEA设置Maven 镜像
  • 项目评定等级L1、L2、L3、L4
  • 一个基于SpringBoot+Vue前后端分离学生宿舍管理系统详细设计实现
  • 工作相关----《配置bond》
  • Nacos、ZooKeeper和Dubbo的区别
  • 刷一下算法
  • three.js——GUI的使用
  • LeetCode 332. Reconstruct Itinerary【欧拉回路,通路,DFS】困难
  • 236. 二叉树的最近公共祖先 Python
  • WPF中DataGrid控件绑定数据源
  • Linux arm64 set_memory_ro/rw函数
  • 安达发|APS排单软件中甘特图的应用
  • 快速上手Linux基础开发工具
  • 【开发工具】idea 的全局搜索快捷键(Ctrl+shift+F)失效
  • 港联证券:“火箭蛋”来袭 蛋价涨势能否延续?
  • Vue3_vite
  • python-字符串去掉空格的常见方法
  • 如何写出一个成熟的线上线下结合的营销方案?
  • Vc - Qt - “扩张“的窗口
  • vue学习-02vue入门之组件
  • 解决Pycharm使用Conda激活环境失败的问题
  • SpringSecurity 核心组件
  • 【Vue】快速入门和生命周期
  • JVM架构和内存管理优化