效果展示:

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

Vue2.5开发去哪儿网App 搜索功能完成的更多相关文章

  1. Vue2.5 开发去哪儿网App

    Vue2.5开发去哪儿网App 技术栈和主要框架

  2. Vue2.5开发去哪儿网App 首页开发

    主页划 5 个组件,即 header  icon  swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...

  3. Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用

    一,数据共享 1.  安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...

  4. Vue2.5开发去哪儿网App 从零基础入门到实战项目

    第1章 课程介绍本章主要介绍课程的知识大纲,学习前提,讲授方式及预期收获. 1-1 课程简介 试看第2章 Vue 起步本章将快速讲解部分 Vue 基础语法,通过 TodoList 功能的编写,在熟悉基 ...

  5. Vue2.5开发去哪儿网App 第五章笔记 上

    1.css动画原理 .fade-enter{ opacity: 0; } .fade-enter-active{ transition: opacity 2s; } .fade-leave-to{ o ...

  6. Vue2.5开发去哪儿网App 城市列表开发之 兄弟组件间联动及列表性能优化

    一,  兄弟组件间联动 1.  点击城市字母,左侧对应显示 给遍历的 字母 添加一个点击事件: Alphabet.vue @click="handleLetterClick" ha ...

  7. Vue2.5开发去哪儿网App 城市列表开发

     一,城市选择页面路由配置                                                                                        ...

  8. Vue2.5开发去哪儿网App 第五章笔记 下

    1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...

  9. Vue2.5开发去哪儿网App 第四章笔记 下

    1.解决非父子组件之间的传值问题 非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性 Vue.prototype.bus ...

随机推荐

  1. 百度地图经纬度和地址互转(Java代码)

    这是基于springmvc+mybatis 的一个controller.如果不是这个框架,可以把方法实体抽到自己写的一个类中,然后再测试 package com.uwitec.controller.s ...

  2. vue 开发系列(五) 调用原生API

    概要 我们在开发手机端程序的时候了,我们经常需要使用到拍照,二维码的功能.数字天堂公司提供了大量的原生API支持. http://www.html5plus.org/doc/ 实现 1.在hbuild ...

  3. ERR_CACHE_MISS 上一页提示确认重新提交表单

    SITUATION: 设备搜索后,根据返回结果 list.php 进入特定设备的详细页面 one.php,但点击后退按钮(上一页)返回 list.php,会出现确认重新提交表单的错误页面 ERR_CA ...

  4. WORD文档中插入页码的问题

    原文链接:http://www.360doc.com/content/11/0216/15/849254_93539436.shtml 一.页码从第二页开始1.选择“插入-页码”,打开“页码”对话框. ...

  5. oss 上传文件夹-cloud2-泽优软件

    说明: 1. 修复同时上传多个文件夹崩溃的问题. 2. 修复阿里云(OSS)特殊文件名称无法上传的问题. 3. 文件夹MD5提供配置项(默认关闭).

  6. 原生JS一些操作

    很久没写原生的JS了,上周做了一个小东西让我又重新了解了一下原生JS,以下记录一些常见的原生JS var canvArrow = document.getElementById('js-canv_ar ...

  7. html5+javascript的管廊监控页面

    这周二做的,支持苹果手机,安卓手机,电脑,平板访问

  8. Alpha阶段敏捷冲刺(三)

    1.提供当天站立式会议照片一张. 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 吴玲:一边学习,一边参考别人的代码. 王兴:完成了数据库的初步搭 ...

  9. SSM_CRUD新手练习(5)测试mapper

    上一篇我们使用逆向工程生成了所需要的bean.dao和对应的mapper.xml文件,并且修改好了我们需要的数据库查询方法. 现在我们来测试一下DAO层,在test包下新建一个MapperTest.j ...

  10. js-倒计时应用

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...