微信小程序城市选择插件

实现搜索城市功能和通过首字母选择城市

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;
}

原文地址:https://www.cnblogs.com/jiayeyuan/p/10540372.html

时间: 2024-08-29 17:18:58

微信小程序城市选择插件的相关文章

微信小程序下订单插件(日历)

最近做小程序开发,出于练手,也是工作需要,就做了个微信小程序的类似于酒店预订的日历插件.先上图: 这个插件分为上下两部分,上边是tab栏,会根据当前的日期自动定位到当前,并展示以后7天的日期,下边为内容展示,随tab栏变化而变化.思路:首先用`new Data()`时间对象初始化时间,获取当前的日期,用`new Date(Date.UTC(year, month - 1, 1)).getDay()`获取每个月的第一天是星期几. // 计算每月第一天是星期几 function getFirstDa

微信小程序实现地图插件的调用方法

目前很多微信小程序都具备地图查看的功能,那么对于在微信小程序中如何实现地图插件的调用方法,可能很多初学小程序开发的技术人员还存在很多疑问,那么下面就来跟大家详细解答一下. 首先肯定是借助小程序自带的map组件 .wxml代码: <map id="myMap" style="width: 100%; height: 300px;" latitude="{{latitude}}" longitude="{{longitude}}&qu

微信小程序 富文本插件 循环渲染方式

感谢GitHub https://github.com/icindy/wxParse/wiki/wxParse%E5%A4%9A%E6%95%B0%E6%8D%AE%E5%BE%AA%E7%8E%AF%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95 介绍 介绍如何使用wxParse在回复等多条HTML共同渲染的方法 方法介绍 /** * WxParse.wxParseTemArray(temArrayName,bindNameReg,total,that) * 1.tem

微信小程序开源项目库汇总

微信小程序开源项目库汇总,里面集合了OpenDigg 上的优质的微信小程序开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wemark ★85 - 微信小程序Markdown渲染库 WeZRender ★36 - 微信小程序Canvas增强组件 wet

微信小程序UI组件、开发框架、实用库

UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wemark ★85 - 微信小程序Markdown渲染库 WeZRender ★36 - 微信小程序Canvas增强组件 wetoast ★21 - 仿照微信小程序提供的showToast功能 wxapp-charts ★20 - 微信小程序图表charts组件 WeiXinProject ★18 - 列

微信小程序学习指南

作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 微信小程序正式公测, 张小龙全面阐述小程序,定档1月9日上线(附90分钟演讲全文) ... 前言:新人第一坑,跳坑指南:修改后,必须保存:ctrl+S: 1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教

微信小程序框架集合

UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wemark ★85 - 微信小程序Markdown渲染库 WeZRender ★36 - 微信小程序Canvas增强组件 wetoast ★21 - 仿照微信小程序提供的showToast功能 wxapp-charts ★20 - 微信小程序图表charts组件 WeiXinProject ★18 - 列

分享一下微信小程序的实例【转】

wx-gesture-lock  微信小程序的手势密码 WXCustomSwitch 微信小程序自定义 Switch 组件模板 WeixinAppBdNovel 微信小程序demo:百度小说搜索 shitoujiandaobu 小程序:石头剪刀布(附代码说明) audiodemo 微信小程序开发之视频播放器 Video 弹幕 弹幕颜色自定义 star 微信小程序开发之五星评分 switchCity 微信小程序开发之城市选择器 城市切换 huadong_del  微信小程序滑动删除效果 jianh

微信小程序源码案例大全

微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo:文章列表demo 微商城(含微信小程序)完整源码+配置指南 微信小程序Demo:一个简单的工作系统 微信小程序Demo:用于聚会的小程序 微信小程序Demo:Growth 是一款专注于Web开发者成长的应用,- 微信小程序Demo: Music-Player 微信小程序Demo:团贷网(投资) 微信