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
Vue2.5开发去哪儿网App 搜索功能完成的更多相关文章
- Vue2.5 开发去哪儿网App
Vue2.5开发去哪儿网App 技术栈和主要框架
- Vue2.5开发去哪儿网App 首页开发
主页划 5 个组件,即 header icon swiper recommend weekend 一. header区域开发 1. 安装 stylus npm install stylus --s ...
- Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用
一,数据共享 1. 安装: npm install vuex --save 2. 在src目录下 新建state文件夹,新建index.js文件 3. 创建一个 store import Vue f ...
- Vue2.5开发去哪儿网App 从零基础入门到实战项目
第1章 课程介绍本章主要介绍课程的知识大纲,学习前提,讲授方式及预期收获. 1-1 课程简介 试看第2章 Vue 起步本章将快速讲解部分 Vue 基础语法,通过 TodoList 功能的编写,在熟悉基 ...
- Vue2.5开发去哪儿网App 第五章笔记 上
1.css动画原理 .fade-enter{ opacity: 0; } .fade-enter-active{ transition: opacity 2s; } .fade-leave-to{ o ...
- Vue2.5开发去哪儿网App 城市列表开发之 兄弟组件间联动及列表性能优化
一, 兄弟组件间联动 1. 点击城市字母,左侧对应显示 给遍历的 字母 添加一个点击事件: Alphabet.vue @click="handleLetterClick" ha ...
- Vue2.5开发去哪儿网App 城市列表开发
一,城市选择页面路由配置 ...
- Vue2.5开发去哪儿网App 第五章笔记 下
1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...
- Vue2.5开发去哪儿网App 第四章笔记 下
1.解决非父子组件之间的传值问题 非父子组件传值(Bus/总线/发布订阅模式/观察者模式) 给 Vue类上挂在一个属性,然后创建vue实例时,实例就拥有了这个属性 Vue.prototype.bus ...
随机推荐
- yii2 控制器渲染
render() : 渲染一个 视图名并使用一个 布局返回到渲染结果. renderPartial() : 渲染一个 视图名并且不使用布局. renderAjax() : 渲染一个 视图名并且不使用布 ...
- C++中string的使用
概述 这篇博文为了记录C++中string的使用,用到一点补充一点. 预备 使用string之前需要包含头文件 #include<iostream> #include<string& ...
- 从客户端(f="<zhaoyuntang.com")中检测到有潜在危险的 Request.Form 值。
从客户端(f="<yi733.com")中检测到有潜在危险的 Request.Form 值. 解决办法1:在aspx页面头部加 ValidateRequest="f ...
- std::string的find问题研究
https://files-cdn.cnblogs.com/files/aquester/std之string的find问题研究.pdf 目录 目录 1 1. 前言 1 2. find字符串 1 3. ...
- classmethod,staticmethod
'''1 绑定方法: 在类内部定义的函数,默认就是给对象来用,而且是绑定给对象用的,称为对象的绑定方法 绑定对象的方法特殊之处: 应该由对象来调用,对象来调用,会自动将对象当作第一个参数传入 绑定到类 ...
- How to fix "FAILURE DURING CONVERSION TO COFF: FILE INVALID OR CORRUPT"
Error LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt appear ...
- FatMouse's Speed (hdu 1160)
#include <iostream> #include <cstdio> #include <cstring> #include <algori ...
- mysql_触发器
mysql触发器 触发器:trigger,事先为某张表绑定好一段代码,当表中某些内容发生改变的时候(增删改),系统会自动触发代码,执行 触发器:事件类型,触发时间,触发对象 事件类型:增删改,三种类型 ...
- 在Ubuntu上安装pyenv
因为找到一个域名枚举的脚本使用Python3编写的,而我一直习惯的是使用Python2.7,在自己的Windows7上再安装个Python怕混了,于是想着在VPS上装个Python的版本管理工具,也方 ...
- postgresql 数据库路径迁移
迁移方法有两种:(1)重新初始化postgresql数据库,初始化时指定新的数据路径---PGDATA,然后在新的环境下将原有的数据库备份恢复一下.不过这个方法有点麻烦(2)直接将现有的数据库文件全部 ...