微信小程序城市选择及搜索功能的方法


Posted in Javascript onMarch 22, 2019

实现搜索城市功能

参考 微信小程序 之『仿美团城市选择 城市切换』
https://github.com/cinoliu/-selectCity

微信小程序城市选择及搜索功能的方法

js文件

// pages/address/address.js
var app = getApp()

Page({
 data: {
  searchLetter: [],
  showLetter: "",
  winHeight: 0,
  cityList: [],
  isShowLetter: false,
  scrollTop: 0,//置顶高度
  scrollTopId: '',//置顶id
  city: "",
  cityList_search:[],
  address_show:false,
  search_city:[],
  is_data:true,
  empty:'',
 },
 onLoad: function (options) {
  console.log(options.currentcity)
  
  // 生命周期函数--监听页面加载
  let that = this;
  that.setData({
   city: options.currentcity
  })
  var searchLetter = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"];
  new Promise(function (resolve) {
   that.getCity(function (data) {
    console.log(data)
    let cityObj = data.cityList;
    var tempObj = [];
    for (var i = 0; i < searchLetter.length; i++) {
     var initial = searchLetter[i];
     var cityInfo = [];
     var tempArr = {};
     tempArr.initial = initial;
     for (var j = 0; j < cityObj.length; j++) {
      if (initial == cityObj[j].initial) {
       cityInfo.push(cityObj[j]);
      }
     }
     tempArr.cityInfo = cityInfo;
     tempObj.push(tempArr);
    }
    console.log(tempObj)
    that.setData({
     cityList: tempObj
    })
    resolve(tempObj); 
   })
   
  }).then(function(res){
   console.log(res)
   let cityObj = [];
   var sysInfo = wx.getSystemInfoSync();
   var winHeight = sysInfo.windowHeight;
   var itemH = winHeight / searchLetter.length;
   var tempObj = [];
   for (var i = 0; i < searchLetter.length; i++) {
    var temp = {};
    temp.name = searchLetter[i];
    temp.tHeight = i * itemH;
    temp.bHeight = (i + 1) * itemH;
    tempObj.push(temp)
   }
   that.setData({
    winHeight: winHeight,
    itemH: itemH,
    searchLetter: tempObj,
   })  
  })
 },
 getCity: function (callBack){
  let that = this;
  app.commonRequest('wxapp/public/getCityList', 'POST', {}, function (data) {
    console.log(data);
     if (data.status == '200') {
      that.setData({
       cityList: data.datainfo.list,
       // city: data.datainfo.getcode,     
      })
      callBack({
       cityList: data.datainfo.list
      })
     } else {
      callBack({
       cityList: data.datainfo.list
      })
     }
  })
 },
 set_current_city:function(set_city,callBack){
  let that = this;
  app.commonRequest('wxapp/public/getCityList', 'POST', {
   area_name: set_city,
   cityCheckType:1,
  }, function (data) {
   console.log(data)
   if (data.status == "200") {
    callBack({
     data: data
    })
   }else {
    callBack({
     data: data
    })
   }
  })  
 },
 search_city:function(e){
  let that =this;
  that.setData({
   address_show:true
  })

 },
 cancel_city:function(e){
  let that = this;
  that.setData({
   search_city:[],
   address_show: false,
   empty:'',
  })
 },
 seacrch_city:function(e){
  let that =this;
  let search_val = e.detail.value;
  console.log(search_val);
  app.commonRequest('wxapp/public/getCityList', 'POST', {
   area_name: search_val
  }, function (data) {
   console.log(data)
   if(data.status == "200"){
    if (data.datainfo.list.length >0){
     that.setData({
      search_city: data.datainfo.list,
      is_data: true
     })
    }
    else{
     that.setData({
      search_city: data.datainfo.list,
      is_data:false
     })
    }
   }   
  })  
  
 },
 clickLetter: function (e) {
  console.log(e.currentTarget.dataset.letter)
  var showLetter = e.currentTarget.dataset.letter;
  this.setData({
   showLetter: showLetter,
   isShowLetter: true,
   scrollTopId: showLetter,
  })
  var that = this;
  setTimeout(function () {
   that.setData({
    isShowLetter: false
   })
  }, 1000)
 },
 //选择城市
 bindCity: function (e) {
  let that = this;
  console.log("bindCity");
  that.set_current_city(e.currentTarget.dataset.city,function(data){
   console.log(data)
  });
  wx.setStorageSync('currentcity', e.currentTarget.dataset.city)
  // that.onLoad();
  this.setData({
   city: e.currentTarget.dataset.city,
   // scrollTop: 0, 
  })
// 回到首页
  wx.switchTab({
   url: '/pages/index/index' 
  })
 },
})

wxml文件

<!--pages/address/address.wxml-->
<view class="searchLetter touchClass">
 <view class="thishotText" bindtap="hotCity">
  <view style="margin-top:0;">当前</view>
  <!-- <view style="margin-top:0;">热门</view> -->
 </view>
 <view wx:for="{{searchLetter}}" style="color:#53985F;font-size:20rpx;" wx:key="index" data-letter="{{item.name}}" catchtouchend="clickLetter" >{{item.name}}</view>
</view>
<block wx:if="{{isShowLetter}}">
 <view class="showSlectedLetter">
  {{showLetter}}
 </view>
</block>
<scroll-view scroll-y="true" style="height:{{winHeight}}px" 
 scroll-into-view="{{scrollTopId}}" scroll-top="{{scrollTop}}">
 <view class='searchbox'>
   <view class='input_box'>
    <image class='search' src='/images/search.png'></image>
    <input placeholder='城市' onchange="seacrch_city" oninput="seacrch_city" onblur="seacrch_city" value='{{empty}}' bindtap='search_city'></input>
    <view class='close' bindtap='cancel_city'>×</view>
   </view>
   <view class='cancel' bindtap='cancel_city'>取消</view>
  </view>
 <view id='address' hidden='{{address_show}}'>
  <view class='current_city li_style'>当前:{{city}}</view>
  <view class='all_city'>
   <view class='li_style'>所有城市</view>  
  </view> 
  <view class="selection" wx:for="{{cityList}}" wx:key="{{item.initial}}">
   <view class="item_letter" id="{{item.initial}}">{{item.initial}}</view>
   <view class="item_city" wx:for="{{item.cityInfo}}" wx:for-item="ct" wx:key="{{ct.id}}" data-cityCode="{{ct.area_code}}" data-city="{{ct.area_name}}" bindtap="bindCity">
    {{ct.area_name}}
   </view>
  </view>
 </view>
 <view id='address_search' hidden='{{!address_show}}'>  
  <view>
   <view class="item_city" wx:for="{{search_city}}" wx:for-item="ct" wx:key="{{ct.id}}" data-cityCode="{{ct.area_code}}" data-city="{{ct.area_name}}" bindtap="bindCity">
    {{ct.area_name}}
   </view>
   <view class='noData' hidden='{{is_data}}'>暂无数据</view>
  </view>
 </view>
</scroll-view>

wxss文件

/* pages/address/address.wxss */

.searchbox{
 overflow: hidden;
 margin: 0 20rpx;
}
.search{
 width: 20px;
 height: 20px;
 float: left;
 margin:7rpx 10rpx;
}
.input_box{
 width: 630rpx;
 height: 50rpx;
 background: #efefef;
 border-radius: 30rpx;
 float: left;
}
.input_box input{
 font-size: 25rpx;
 width: 450rpx;
 float: left;
}
.input_box .close{
 width:30rpx;
 height:30rpx;
 background:#aaa;
 color:#fff;
 border-radius:50%;
 float:right;
 margin-right:20rpx;
 margin-top:10rpx;
 line-height:27rpx;
 font-size:30rpx;
 text-align:center;
}
.searchbox .cancel{
 font-size: 25rpx;
 color: #53985F;
 width: 80rpx;
 text-align: right;
 float: right;
 line-height: 50rpx;
}
.current_city{
 border-bottom: 1rpx solid #eee; 
}
.li_style{
 height: 50rpx;
 padding: 20rpx 0;
 width: 710rpx;
 line-height: 50rpx;
 font-size: 29rpx;
 margin:0 20rpx;
}

.searchLetter {
 position: fixed;
 right: 0;
 width: 50rpx;
 text-align: center;
 justify-content: center;
 display: flex;
 flex-direction: column;
 color: #666;
 z-index: 1;
}

.searchLetter view {
 margin-top: 20rpx;
}

.touchClass {
 background-color: #fff;
 color: #fff;
 top: 100rpx;
}

.showSlectedLetter {
 background-color: rgba(0, 0, 0, 0.5);
 color: #fff;
 display: flex;
 justify-content: center;
 align-items: center;
 position: fixed;
 top: 50%;
 left: 50%;
 margin: -100rpx;
 width: 200rpx;
 height: 200rpx;
 border-radius: 20rpx;
 font-size: 52rpx;
 z-index: 1;
}

.selection {
 display: flex;
 width: 100%;
 flex-direction: column;
 margin-top: 10rpx;
}

.item_letter {
 display: flex;
 background-color: #f5f5f5;
 height: 50rpx;
 padding-left: 34rpx;
 align-items: center;
 font-size: 24rpx;
 color: #666;
}

.item_city {
 display: flex;
 background-color: #fff;
 height: 100rpx;
 padding-left: 34rpx;
 align-items: center;
 border-bottom: 1rpx solid #ededed;
 font-size: 24rpx;
 color: #666;
}

.hotcity-common {
 font-size: 24rpx;
 color: #666;
 padding: 0 0 0 30rpx;
}

.thisCity {
 padding-top: 30rpx;
}

.thisCityName {
 display: inline-block;
 border: 1rpx solid #2ab4ff;
 border-radius: 8rpx;
 padding: 10rpx 0;
 font-size: 24rpx;
 color: #2ab4ff;
 text-align: center;
 min-width: 149.5rpx;
 margin: 20rpx 0 20rpx 30rpx;
}

.thishotText {
 color: #53985F;
 font-size: 20rpx;
 margin: 0 !important;
}

.slectCity {
 border-color: #2ab4ff !important;
}

.slectCity view {
 color: #2ab4ff !important;
}

.weui-grid {
 position: relative;
 float: left;
 padding: 10rpx 0;
 width: 149.5rpx;
 box-sizing: border-box;
 border: 1rpx solid #ececec;
 border-radius: 8rpx;
 margin: 10rpx 12rpx;
}

.weui-grid__label {
 display: block;
 text-align: center;
 color: #333;
 font-size: 24rpx;
 white-space: nowrap;
 text-overflow: ellipsis;
 overflow: hidden;
}
.noData{
 text-align: center;
 font-size: 30rpx;
 color: #aaa;
 line-height: 60rpx;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js 颜色选择器(兼容firefox)
Mar 05 Javascript
js 代码集(学习js的朋友可以看下)
Jul 22 Javascript
jQuery的each终止或跳过示例代码
Dec 12 Javascript
JS实现左右拖动改变内容显示区域大小的方法
Oct 13 Javascript
Bootstrap每天必学之导航条(二)
Mar 01 Javascript
Bootstrap3学习笔记(三)之表格
May 20 Javascript
快速移动鼠标触发问题及解决方法(ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave)
Aug 29 Javascript
d3.js实现简单的网络拓扑图实例代码
Nov 06 Javascript
利用Vue.js+Node.js+MongoDB实现一个博客系统(附源码)
Apr 24 Javascript
Node.js 进程平滑离场剖析小结
Jan 24 Javascript
vue-cli 为项目设置别名的方法
Oct 15 Javascript
二维码条形码生成的JavaScript脚本库
Jul 07 Javascript
使用node搭建自动发图文微博机器人的方法
Mar 22 #Javascript
如何从零开始手写Koa2框架
Mar 22 #Javascript
Vue服务端渲染实践之Web应用首屏耗时最优化方案
Mar 22 #Javascript
详解ES6中的Map与Set集合
Mar 22 #Javascript
js控制随机数生成概率代码实例
Mar 21 #Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
Mar 21 #Javascript
详解基于React.js和Node.js的SSR实现方案
Mar 21 #Javascript
You might like
WordPress中用于更新伪静态规则的PHP代码实例讲解
2015/12/18 PHP
JQuery+JS实现仿百度搜索结果中关键字变色效果
2011/08/02 Javascript
js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)
2014/01/14 Javascript
jQuery中;function($,undefined) 前面的分号的用处
2014/12/17 Javascript
jQuery中next()方法用法实例
2015/01/07 Javascript
JavaScript实现当网页加载完成后执行指定函数的方法
2015/03/21 Javascript
Javascript之String对象详解
2016/06/08 Javascript
微信小程序-详解数据缓存
2016/11/24 Javascript
JavaScript用JSONP跨域请求数据实例详解
2017/01/06 Javascript
详解Vue监听数据变化原理
2017/03/08 Javascript
Vue2.0 实现单选互斥的方法
2018/04/13 Javascript
VUE基于NUXT的SSR 服务端渲染
2018/11/30 Javascript
Vue实现类似Spring官网图片滑动效果方法
2019/03/01 Javascript
详解Vue源码中一些util函数
2019/04/24 Javascript
微信小程序获取用户绑定手机号方法示例
2019/07/21 Javascript
分享JS表单验证源码(带错误提示及密码等级)
2020/01/05 Javascript
[49:11]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第二场 12.20
2020/12/23 DOTA
[01:16:50]DOTA2-DPC中国联赛 正赛 Phoenix vs CDEC BO3 第一场 3月7日
2021/03/11 DOTA
CentOS中使用virtualenv搭建python3环境
2015/06/08 Python
Python使用pydub库对mp3与wav格式进行互转的方法
2019/01/10 Python
python内置函数sorted()用法深入分析
2019/10/08 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
奥地利网上现代灯具和灯饰店:Lampenwelt.at
2018/01/29 全球购物
澳大利亚设计的婴儿和女孩的衣服:Oobi
2018/12/16 全球购物
Vuori官网:运动服装的终级表现
2021/01/27 全球购物
MYSQL支持事务吗
2013/08/09 面试题
十佳教师事迹材料
2014/01/11 职场文书
2014年大班元旦活动方案
2014/02/26 职场文书
美术兴趣小组活动总结
2014/07/07 职场文书
领导干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
2014年民警工作总结
2014/11/25 职场文书
2015国庆66周年宣传语
2015/07/14 职场文书
驻村工作简报
2015/07/20 职场文书
毕业生自荐求职信书写的技巧
2019/08/26 职场文书
导游词之重庆钓鱼城
2019/09/19 职场文书
Spring Boot 的创建和运行示例代码详解
2022/07/23 Java/Android