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 ...
随机推荐
- WebSocket 处理事件
WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 W ...
- android studio友盟分享demo运行报错Gradle's dependency cache may be corrupt解决方法
gradle-wrapper.properties里修改了gradle的版本,与之前没有报错的项目gradle版本一致.
- java常用设计模式六:适配器模式
一.定义 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作.比如以下的场景: 用手机充电为例,有一个手机的插孔是TypeC口,现在只 ...
- c# 二维list排序和计时
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- IP和网段及子网掩码基础知识
IP地址由网络号和主机号两部分组成,网络号的最高位必须是"0",IP地址和子网掩码求"与"算出网络地址,只有网络地址相同才可直接通信,否则需要借助路由. 主机标 ...
- AngularJS $eval $parse
$eval $parse都可以解析或计算Angular表达式的值. 一.$parse 是一个独立的可以注入的服务,注入就可以使用,它返回一个函数,我们需要显式将表达式求值的上下文传递给该函数.$par ...
- 受邀与微软售前技术团队参与TFS技术沟通会议
微软正式发布Azure DevOps Server 2019的第一个版本,作为Team Foundation Server (TFS)2018的升级版本和替代产品. 作为开发运维一体化平台的主打产品, ...
- 简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析
简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析 虽然经常用 OAuth 2.0,但是原理却不曾了解,印象里觉得很简单,请求跳来跳去,今天看完相关介绍,就来捋一捋 ...
- Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
Decimal类型截取保留N位小数向上取Decimal类型截取保留N位小数并且不进行四舍五入操作 封装静态方法 public class DecimalHelper { /// <summary ...
- 关于Winform下DataGridView中实现checkbox全选反选、同步列表项的处理
近期接手一个winform 项目,虽然之前有.net 的经验,但是对一些控件的用法还不是很熟悉. 这段时间将会记录一些在工作中遇到的坎坷以及对应的解决办法,写出来与大家分享并希望大神提出更好解决方法来 ...