zoukankan      html  css  js  c++  java
  • Vue2.5开发去哪儿网App 搜索功能完成

    效果展示:

    Search.vue:

        <div class="search-content" ref="search" v-show="keyword">
          <!--双向绑定keyword-->
          <ul>
            <!--遍历找到的城市-->
            <li class="search-item border-bottom" v-for="(city,index) in cityList" :key="index">{{city}}</li>
            <!--没有找到时的显示-->
            <li class="search-item border-bottom" v-show="hasCity">
              没有找到匹配数据
            </li>
          </ul>
        </div>

    当有输入时:       

    const result = []
    //this.cities格式: {
    // "id": 933,
    // "spell": "zhangpu",
    // "name": "漳浦"
    // }
    ...
    for (let i in this.cities) {
    this.cities[i].forEach((value) => {
    if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
    result.push(value.name)
    }
    })
    this.cityList = result
    }

    没有输入时:

    if (!this.keyword) {
    this.cityList = []
    return
    }

    添加 computed 计算属性:

      computed: {
        hasCity () {
          return !this.cityList.length
        }
      }
    //负责显示与否:
    <li class="search-item border-bottom" v-show="hasCity">
    没有找到匹配数据
    </li>
    <template>
      <div>
        <div class="search">
          <input type="text" v-model="keyword" class="search-input" placeholder="输入城市名或拼音">
        </div>
        <div class="search-content" ref="search" v-show="keyword">
          <!--双向绑定keyword-->
          <ul>
            <!--遍历找到的城市-->
            <li class="search-item border-bottom" v-for="(city,index) in cityList" :key="index">{{city}}</li>
            <!--没有找到时的显示-->
            <li class="search-item border-bottom" v-show="hasCity">
              没有找到匹配数据
            </li>
          </ul>
        </div>
      </div>
    </template>
    
    <script>
    import BScroll from 'better-scroll'
    export default {
      name: 'CitySearch',
      props: ['cities'],
      data: function () {
        return {
          // 关键字
          keyword: '',
          // 城市列表
          cityList: [],
          // 函数节流
          timer: null
        }
      },
      computed: {
        hasCity () {
          return !this.cityList.length
        }
      },
      watch: {
        keyword: function () {
          if (this.timer) {
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(() => {
            if (!this.keyword) {
              this.cityList = []
              return
            }
            const result = []
            for (let i in this.cities) {
              this.cities[i].forEach((value) => {
                if (value.spell.indexOf(this.keyword) > -1 || value.name.indexOf(this.keyword) > -1) {
                  result.push(value.name)
                }
              })
              this.cityList = result
            }
          }, 100)
        }
      },
      mounted () {
        this.scroll = new BScroll(this.$refs.search)
      }
    }
    </script>
    
    <!--组件样式,不影响其他组件-->
    <!--1rem = html front-size = 50px-->
    <style lang="stylus" scoped>
    @import "~styles/varibles.styl"
      .search
        height .72rem
        padding 0 .1rem
        background $bgColor
        .search-input
          padding 0 .1rem
          box-sizing border-box
          height .62rem
          line-height .62rem
          width 100%
          text-align center
          border-radius .06rem
      .search-content
        overflow hidden
        background #eee
        position absolute
        top 1.58rem
        left 0
        right 0
        z-index: 1
        bottom 0
        .search-item
          line-height .62rem
          padding-left .2rem
          color #666
          background #fff
    </style>
    Search.vue

    项目地址:

    https://github.com/1417766861/Vue2.5-App/tree/city-search-logic/Travel

  • 相关阅读:
    回溯、递归、DFS方法
    3-11日学习记录
    文本清洗总结
    归并排序学习
    3-9日学习笔记
    P3182 [HAOI2016]放棋子 错排问题
    P2880 [USACO07JAN]平衡的阵容Balanced Lineup 线段树 树状数组
    P3469 [POI2008]BLO-Blockade 强连通
    P2756 飞行员配对方案问题 网络流 二分图匹配
    P1823 [COI2007] Patrik 音乐会的等待 单调栈
  • 原文地址:https://www.cnblogs.com/donghaoblogs/p/10443306.html
Copyright © 2011-2022 走看看