小程序实现城市搜索功能
生活随笔
收集整理的這篇文章主要介紹了
小程序实现城市搜索功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
wxml:
<view class="location">推薦</view><button class="getLocation" bindtap="getLocation" size='mini'>定位</button><view class="hotCity">熱門城市</view> <block wx:for="{{hotCitys}}" wx:key="index"> <button class="btn" bindtap="selectcity" data-val="{{item}}" size="mini" >{{item}}</button> </block>wxss:
/* pages/citys/citys.wxss */ .location,.hotCity{padding:20rpx;font-weight: bold;font-size: 36rpx; } .getLocation{color: green;margin-top:20rpx;background: #eee; } .btn{margin: 10rpx;font-weight: normal;border-radius: 10rpx;background: #fff;border: 1px solid #999;font-size: 30rpx !important;padding: 4rpx 16rpx !important; }js:
// pages/citys/citys.js var app=getApp();Page({/*** 頁面的初始數(shù)據(jù)*/data: { hotCitys:["北京", "上海", "深圳", "廣州", "武漢", "荊州", "荊門"]},//切換熱門城市 ......1.點擊獲取熱門城市 2.跳轉(zhuǎn)也買你傳遞熱門城市。。。。。selectcity: function(e){console.log(e);var citys=e.currentTarget.dataset.val;console.log("獲取的城市:"+citys);wx.setStorageSync('cityName', citys);wx.switchTab({url: '../food/food',})},getLocation:function(){console.log(app);wx.getLocation({ success:res=>{console.log(res);wx.request({url: 'http://iwenwiki.com:3002/api/lbs/location',data:{latitude:res.latitude,longitude:res.longitude }, success:result=>{console.log(result);var cityName=result.data.result.ad_info.city.slice(0,2);console.log(cityName);//跳轉(zhuǎn)----食療坊界面 --把數(shù)據(jù)傳遞過去。// wx.switchTab({// url: '../food/food',// })//1.url地址傳遞參數(shù)// wx.reLaunch({// url: '../food/food?cityName='+cityName,// })//2. 把獲取的變量存儲給全局app.js里面的全局變量 // app.globalData.cityName=cityName;// wx.switchTab({// url: '../food/food',// })//3.本地存儲wx.setStorageSync('cityName', cityName);console.log("獲取存儲的值:",wx.getStorageSync('cityName'));wx.switchTab({url: '../food/food',})}})}})},/*** 生命周期函數(shù)--監(jiān)聽頁面加載*/onLoad: function (options) {// wx.request({// url: 'http://iwenwiki.com:3002/api/hot/city',// success:res=>{// console.log(res.data);// this.setData({// hotCitys:res.data.data// })// }// })},/*** 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成*/onReady: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面顯示*/onShow: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面隱藏*/onHide: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面卸載*/onUnload: function () {},/*** 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作*/onPullDownRefresh: function () {},/*** 頁面上拉觸底事件的處理函數(shù)*/onReachBottom: function () {},/*** 用戶點擊右上角分享*/onShareAppMessage: function () {} })搜索頁:
wxml:
js:
// pages/search/search.js Page({/*** 頁面的初始數(shù)據(jù)*/data: {},//獲取表單input輸入的內(nèi)容searchinput:function(e){console.log("輸入的內(nèi)容",e.detail);if(e.detail.value){wx.request({url: 'http://iwenwiki.com:3002/api/foods/select',data:{name:e.detail.value,city:""},success:res=>{if(res.data.status==200){this.setData({list:res.data.data})}else{console.log(res.data.msg);this.setData({list:[]})}console.log(res.data);}})}else{console.log("輸入內(nèi)容不能為空!");//進行清空數(shù)據(jù)this.setData({list:[]})}},/*** 生命周期函數(shù)--監(jiān)聽頁面加載*/onLoad: function (options) {},/*** 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成*/onReady: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面顯示*/onShow: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面隱藏*/onHide: function () {},/*** 生命周期函數(shù)--監(jiān)聽頁面卸載*/onUnload: function () {},/*** 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作*/onPullDownRefresh: function () {},/*** 頁面上拉觸底事件的處理函數(shù)*/onReachBottom: function () {},/*** 用戶點擊右上角分享*/onShareAppMessage: function () {} })效果如圖:
總結(jié)
以上是生活随笔為你收集整理的小程序实现城市搜索功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: <学习笔记>从零开始自学Python-之
- 下一篇: 庞果答题:亿阳信通:不可表示的数 的一个