• 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. }

  • 相关阅读:
    有一本零基础入门Python的编程书上架!
    ELK运维文档
    拼图小游戏
    Parallels Desktop 20 for Mac 正式发布,更新了哪些新功能(附下载链接)!
    浅述边缘计算场景下的云边端协同融合架构的应用场景示例
    GBase 8a语法格式
    一文看懂推荐系统:Gate网络2:百度GemNN(Gating-Enhanced Multi-Task Neural Networks)
    泰语同声翻译一天多少钱呢
    AI绘画的崛起与多平台对比
    通用结构化剪枝DepGraph
  • 原文地址:https://blog.csdn.net/m0_65607651/article/details/132981082