隨著小程序的大熱,作為一個程序猿,我也開始接觸并且大概了解了一個制作小程序的一些過程,為了提高自己的動手能力,于是乎,我開始來仿寫攜程的小程序,來實現(xiàn)一些基本功能,在仿寫的過程中,也遇到了一些難題,也有了一點收獲,希望可以通過這篇文章與大家共同交流,共同進步。 前言為了更好的開發(fā),我們需要準備我們需要的工具:
具體實現(xiàn)功能效果如下
查詢功能的實現(xiàn)1.首先需要在查詢之前獲取輸入的所在城市以及到的城市,以及時間的選擇,通過這些條件去篩選,所以需要在點擊查詢按鈕的時候綁定一個時間,需要攜帶參數(shù)去進行查詢 <navigator class="search" url="/pages/trainBuyContent/trainBuyContent?from={{from}}&to={{to}}&trainTime={{startDate}}">查詢</navigator> 2.需要到跳轉(zhuǎn)的頁面接收參數(shù)通過onload事件的options獲取 var from = options.from; var to = options.to; var trainTime = options.trainTime; 3.最重要的是篩選出相關(guān)數(shù)據(jù),這里需要一個for循環(huán)的判斷語句,在請求數(shù)據(jù)地址URL的時候,通過for循環(huán)和if語句找出相對應的數(shù)據(jù)文件里面所對應的json數(shù)據(jù)。 wx.request({ url: API_BASE, success: (res) => { for(var i=0;i<res.data.data.trainList.length;i++){ if (from == res.data.data.trainList[i].from && to == res.data.data.trainList[i].to && trainTime == res.data.data.trainList[i].trainTime){ temp.push(res.data.data.trainList[i]); } } this.setData({ searchedList:temp }) } }) 4.這時候再在頁面通過for循環(huán)輸出就可以了 wx:for="{{searchedList}}" wx:key="{{item.id}}" temp.push(res.data.data.trainList[i]); this.setData({ searchedList:temp }) *小程序頁面?zhèn)髦档姆绞剑?.url傳值2.本地儲存3.全局的app對象 訂單查詢的實現(xiàn)這里我采用了全局的app對象保存1.先獲取全局對象,然后在點擊確定購買的success回調(diào)函數(shù)的時,去獲取所有的信息,以一個json格式去獲取 const app = getApp(); var trainedList = app.globalData.trainedList; var trainItem = { from: this.data.from, to: this.data.to, trainNum: this.data.trainNum, trainTime: this.data.trainTime, totalPrice: this.data.totalPrice }; trainedList.push(trainItem); 2.然后在相應的頁面去獲取這個全局的數(shù)組 onLoad: function (options) { this.setData({ trainedList: app.globalData.trainedList }) }, 3.通過一個for循環(huán)讓其輸出在頁面 |