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

  • 相关阅读:
    Spark 1.1.0 安装测试 (分布式 Yarn-cluster模式)
    HBase Mac OSX 安装笔记
    hbase centOS生产环境配置笔记 (1 NameNode, 1 ResourceManager, 3 DataNode)
    ssh 配置自动登录
    CentOS 修改线程数限制等(limits.conf)
    Hadoop Mac OSX 安装笔记
    centos7安装及部署zabbix监控
    设置linux中Tab键的宽度(可永久设置)
    基于NFS共享存储实现KVM虚拟机动态迁移
    KVM虚拟化平台环境部署
  • 原文地址:https://www.cnblogs.com/donghaoblogs/p/10443306.html
Copyright © 2011-2022 走看看