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


    直接上代码

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

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

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

    1. // 搜索框数据
    2. const projectName = ref(null)
    3. //控制候选框显示隐藏
    4. const isShow = ref(false)
    5. // 渲染到候选框的数据
    6. const cityArr = ref(null)
    7. // 搜索框Change事件
    8. const inputChange = () => {
    9. //搜索框值为空 候选框关闭
    10. if (projectName.value == '') {
    11. isShow.value = false
    12. } else {
    13. //输入框输入的时候 遍历总数据 将过滤出来的数据放入其中
    14. if (cityOptions.value.length > 0) {
    15. cityArr.value = []
    16. cityOptions.value.forEach((item, index, array) => {
    17. if (item.label.indexOf(projectName.value) >= 0) {
    18. cityArr.value.push(item)
    19. }
    20. })
    21. // cityOptions.value = cityArr
    22. }
    23. isShow.value = true
    24. // getTreeListFn()
    25. }
    26. }
    27. // 搜索框聚焦事件 请求跟下面获取总数据请求一样 不要问我为什么不直接调用下面的方法 因为我这个是项目里方法 好多数据我都删除了 下面的请求里面有好多逻辑处理 不好直接调用 我就又书写了一遍
    28. const inputFocusFn = () => {
    29. isShow.value = true
    30. let params = {
    31. name: projectName.value
    32. }
    33. getTreeList(params).then(res => {
    34. if (res.code == 200) {
    35. cityArr.value = res.data
    36. }
    37. })
    38. // getTreeListFn()
    39. }
    40. // 搜搜框失焦事件
    41. const inputBlurFn = () => {
    42. isShow.value = false
    43. }
    44. // 获取总的项目树数据
    45. function getTreeListFn() {
    46. let params = {
    47. name: projectName.value
    48. }
    49. getTreeList(params).then(res => {
    50. if (res.code == 200) {
    51. //候选框总数据
    52. cityOptions.value = res.data
    53. }
    54. })
    55. }
    56. //selectedCity这个是选中候选框数据的处理方法
    57. const selectedCity = (id) => {
    58. //处理逻辑
    59. }

  • 相关阅读:
    嵌入式系统关于晶振的问题汇总
    华为云弹性云服务器有哪些优势?
    元宇宙“新基建”时代与不赚快钱的Cocos
    Linux系统移植三:移植Kernel生成zImage和dtb文件
    【SQL刷题】Day13----SQL分组数据专项练习
    基于Springboot+vue开发实现自行车租赁管理系统
    STM32 LWIP Server、Client如何判断网络异常
    node.js 使用 express-jwt 报错:expressJWT is not a function
    基于geotools24.0的创建自动增长主键id字段的方法
    [附源码]Python计算机毕业设计Django高校商铺管理系统论文
  • 原文地址:https://blog.csdn.net/m0_65607651/article/details/132981082