這里先寫一下布局的代碼和js效果代碼,細節有空再補充
<block wx:if="{{condition}}">
<view class="content">
<view class="current_box center">
<view class="current_city center">
<text>當前城市:</text>
<text>{{city}}</text>
</view>
</view>
<view class="start_box text_center" bindtap="startEvent">
<text class="start">起點</text>
<text class="set_out">{{startData}}</text>
</view>
<view class="end_box text_center" bindtap="endEvent">
<text>終點</text>
<text>{{endData}}</text>
</view>
<view class="find_box center">
<view class="find center" bindtap="findEvent">
<text class="find_text">查詢</text>
</view>
</view>
</view>
</block>
<block wx:if="{{start}}">
<view class="start_search_box">
<view class="searches center">
<view class="search text_center">
<text class="search_text center">search</text>
<input class="search_input center" type="text" placeholder="請輸入起點" bindinput="startInputEvent"/>
<text class="search_back center" bindtap="startBackEvent">back</text>
</view>
</view>
<view class="location_box center">
<view class="location">
<text class="location_text align_center">我的位置:</text>
<text class="location_city align_center">{{address}}</text>
</view>
</view>
</view>
</block>
<block wx:if="{{isShow}}">
<view class="start_show_box center">
<view class="start_show_content">
<view class="start_content align_center" wx:key="unique" bindtap="startItemEvent" wx:for="{{array}}" data-index="{{index}}">
{{index}}:{{item.name}}
</view>
</view>
</view>
</block>
<block wx:if="{{end}}">
<view class="end_search_box center">
<view class="end_search text_center">
<text class="search_text">search</text>
<input type="text" placeholder="請輸入終點" bindinput="endInputEvent" />
<text class="search_back" bindtap="endBackEvent">back</text>
</view>
</view>
</block>
<block wx:if="{{isTrue}}">
<view class="end_show_box center">
<view class="end_show_content">
<view class="end_content align_center" bindtap="endItemEvent" wx:for="{{arrays}}" wx:key="unique" data-index="{{index}}">
{{index}}:{{item.name}}
</view>
</view>
</view>
</block>
<block wx:if="{{isInquiry}}">
<view class="inquiry_box center">
<view class="inquiry_content">
<view class="inquiry_content_list center">
{{startData}} -> {{endData}}
</view>
</view>
</view>
</block>
js代碼:
var app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
city: '北京',
startData: '從哪兒出發',
endData: '要去哪兒',
address: '同城街',
condition: true,
start: false,
end: false,
isShow: false,
isTrue: false,
isInquiry: false,
city: "深圳市",
inputData: "",
array: [],
arrays: []
},
/**
* 函數名:findEvent
* 功能:當點擊主頁查詢按鈕時,
* 如果起點和終點沒有數據的話,需要提示請輸入地點
* 如果有起點和終點的話,需要隱藏主頁,顯示新的頁面(提供路線的頁面)
*/
findEvent: function(){
let _this = this;
console.log(this.data.array.length);
if( this.data.array.length > 0 && this.data.arrays.length > 0){
_this.setData({
condition: false,
isInquiry: true,
})
}else{
wx.showLoading({
title: '請選擇地點',
})
setTimeout(function () {
wx.hideLoading()
}, 2000)
}
},
/**
* 函數名:startEvent
* 功能:主頁面模塊隱藏
* 起點頁面顯示
*/
startEvent: function(){
this.setData({
condition: false,
start: true,
})
},
/**
* 函數名:endEvent
* 功能: 主頁面模塊隱藏
* 終點頁面顯示
*/
endEvent: function(){
this.setData({
condition: !this.data.condition,
end: !this.data.end
})
},
/**
* 函數名:startDeleteEvent
* 功能:點擊起點輸入框中的back按鈕返回到主頁面
*
*/
startBackEvent: function(){
this.setData({
condition: true,
start: false,
isShow: false
})
},
/**
* 函數名:endBackEvent
* 功能:點擊終點輸入框中的back按鈕終返回主頁面
*
*/
endBackEvent: function(){
this.setData({
condition: true,
end: false
})
},
/**
* 函數名:startInputEvent
* 功能: 在請輸入起點框中輸入內容時,觸發此函數
*/
startInputEvent: function(e){
console.log("startInputEvent");
var _this = this;
let data = e.detail.value;
this.setData({
isShow: true,
})
wx.request({
url: "https://w.mmm.com/search2016/search/keywords",
data: {
keywords: data,
city: 110000
},
method: 'GET',
success: function(res){
console.log(res.data.pois);
_this.setData({
array: res.data.pois
})
},
fail: function(){
console.log("請求失敗");
}
})
},
/**
* 函數名:endInputEvent
* 功能:在請輸入終點框中輸入內容時觸發此函數
*/
endInputEvent: function(e){
console.log("endInputEvent");
var _this = this;
let data = e.detail.value;
console.log(data);
this.setData({
isTrue: true,
})
wx.request({
url: 'https://w.mmm.com/search2016/search/keywords',
data: {
keywords: data,
city: 110000
},
method: 'GET',
success: function(res){
_this.setData({
arrays: res.data.pois
})
},
fail: function(){
console.log("請求失敗!");
}
})
},
startItemEvent: function(e){
console.log(e);
var i = e.target.dataset.index;
var itemList = this.data.array;
console.log("itemList",itemList[i].name);
var startItemData = itemList[i].name;
this.setData({
isShow: false,
start: false,
condition: true,
startData: startItemData
})
},
/**
* 函數:endItemEvent
* 功能:當點擊渲染出來的每一項時,讓去哪兒改變數據
*/
endItemEvent: function(e){
console.log(e);
var i = e.target.dataset.index;
var itemList = this.data.arrays;
console.log("itemList", itemList[i].name);
var endItemData = itemList[i].name;
this.setData({
condition: true,
end: false,
isTrue: false,
endData: endItemData
})
},
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
testEvent: function(){
wx.navigateTo({
url: "/pages/test/test"
})
},
onLoad: function () {
console.log('onLoad')
var that = this;
app.getUserInfo(function(userInfo){
that.setData({
userInfo:userInfo
})
})
}
})
樣式代碼:
.content{
width: 100%;
height: 100%;
}
.current_box{
width: 100%;
height: 60rpx;
border-bottom: 2rpx solid #ccc;
}
.start_box{
width: 100%;
height: 80rpx;
border-bottom: 2rpx solid #ccc;
}
.start_show_box{
width: 100%;
height: auto;
background: #f99;
}
.start_show_content{
width: 90%;
height: auto;
}
.start_content{
width: 100%;
height: 60rpx;
background: yellow;
margin: 10rpx 0;
}
.end_box{
width: 100%;
height: 80rpx;
border-bottom: 2rpx solid #ccc;
}
.end_show_box{
width: 100%;
height: auto;
background: #f99;
}
.end_show_content{
width: 90%;
height: auto;
}
.end_content{
width: 100%;
height: 60rpx;
background: yellow;
margin: 10rpx 0;
}
.find_box{
width: 100%;
height: 100rpx;
}
.find{
width: 80%;
height: 60rpx;
background: #3cc51f;
}
.find_text{
color: #fff;
}
.start_search_box{
width: 100%;
height: 180rpx;
margin-bottom: 30rpx;
}
.searches{
width: 100%;
height: 120rpx;
border: 2rpx solid #ccc;
}
.search{
width: 90%;
height: 60rpx;
border: 2rpx solid #ccc;
border-radius: 15px;
}
.search_text{
background: #9f9;
}
.search_back{
background: #9f9;
}
.location_box{
width: 100%;
height: 50rpx;
border: 2rpx solid #ccc;
}
.location{
width: 90%;
height: 50rpx;
display: flex;
}
.end_search_box{
width: 100%;
height: 120rpx;
border-bottom: 2rpx solid #ccc;
}
.end_search{
width: 90%;
height: 60rpx;
border-radius: 30rpx;
border: 2rpx solid #ccc;
}
.inquiry_box{
width: 100%;
height: auto;
background: #f99;
}
.inquiry_content{
width: 90%;
height: auto;
}
.inquiry_content_list{
width: 100%;
height: 60rpx;
}
效果圖,可以查看微信小程序中的圖吧公交