您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“微信小程序音樂(lè)播放器的檢索頁(yè)如何制作”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
這個(gè)函數(shù)很簡(jiǎn)單,我們?cè)趯?xiě)歷史記錄頁(yè)面時(shí),已經(jīng)用到了historySearchs這個(gè)數(shù)組,所以添加時(shí)我們只有獲取這個(gè)數(shù)組,然后將要添加的詞push到數(shù)組里,然后用setData更新頁(yè)面就可以了。
addHistorySearchs: function (key) {
var historySearchs = this.data.historySearchs;
historySearchs.push(key);
this.setData({
historySearchs: historySearchs
})
},
復(fù)制代碼
但是這樣做的問(wèn)題是當(dāng)用戶多次搜索相同內(nèi)容時(shí),數(shù)組內(nèi)就會(huì)多次加入同樣的詞,導(dǎo)致我們的歷史記錄列表里出現(xiàn)重復(fù)內(nèi)容,這顯然是不合理的,所以我們?cè)诿看蝡ush前,需要判斷數(shù)組內(nèi)是否已經(jīng)含有這個(gè)詞。
findHistorySearchs: function (key) {
var historySearchs = this.data.historySearchs;
for (var i = 0; i < historySearchs.length; i++) {
if (historySearchs[i] == key) { return false; }
}
return true;
},
創(chuàng)建新的函數(shù),這個(gè)函數(shù)會(huì)遍歷historySearchs數(shù)組,如果存在相同項(xiàng)則返回false,沒(méi)有相同的返回true。
然后我們更改我們的addHistorySearchs方法:
addHistorySearchs: function (key) {
var historySearchs = this.data.historySearchs;
if (this.findHistorySearchs(key)) {
historySearchs.push(key);
this.setData({
historySearchs: historySearchs
})
}
},
有個(gè)這個(gè)方法后,我們開(kāi)始逐條完成我們的事件代碼。
將所有更新頁(yè)面有關(guān)變量添加到data里:
data: {
slider: [],
indicatorDots: true,
autoplay: true,
interval: 5000,
duration: 1000,
radioList: [],
currentView: 1,
topList: [],
hotkeys: [],
showSpecial: false,
special: { key: '', url: '' },
searchKey: '',
searchSongs: [],
zhida: {},
showSearchPanel: 1,
historySearchs: [],
},
熱門關(guān)鍵詞的點(diǎn)擊事件:
hotKeysTap: function (e) {
var dataSet = e.currentTarget.dataset;
var key = dataSet.key; //獲取點(diǎn)擊的關(guān)鍵詞
var self = this;
if (key != '') { //判斷是否為空
self.addHistorySearchs(key); //調(diào)用我們寫(xiě)好的方法,加入歷史記錄
self.setData({
searchKey: key, //為輸入框內(nèi)添加文字
showSearchPanel: 3, //顯示內(nèi)容切換為搜索結(jié)果
});
MusicService.getSearchMusic(key, function (data) { //請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)
if (data.code == 0) {
var songData = data.data;
self.setData({ //將獲得的數(shù)據(jù)添加到相應(yīng)數(shù)組里
searchSongs: songData.song.list,
zhida: songData.zhida
});
}
});
}
},
輸入框獲取焦點(diǎn)事件:
bindFocus: function (e) {
var self = this;
if (this.data.showSearchPanel == 1) { //判斷內(nèi)容是否為熱門關(guān)鍵詞
self.setData({
showSearchPanel: 2 //切換到歷史記錄
})
}
},
歷史記錄文字的點(diǎn)擊事件:
historysearchTap: function (e) {
var dataSet = e.currentTarget.dataset;
var key = dataSet.key; //獲取點(diǎn)擊的歷史記錄文字
var self = this;
self.setData({
searchKey: key, //輸入框添加文字
showSearchPanel: 3 //顯示搜索結(jié)果
});
MusicService.getSearchMusic(key, function (data) { //請(qǐng)求網(wǎng)絡(luò),獲取搜索結(jié)果
if (data.code == 0) {
var songData = data.data;
self.setData({
searchSongs: songData.song.list,
zhida: songData.zhida
});
}
});
},
歷史記錄結(jié)尾的“X”與“清除歷史記錄”的點(diǎn)擊事件:
delHistoryItem: function (e) {
var historySearchs = this.data.historySearchs;
var dataSet = e.currentTarget.dataset; //獲取點(diǎn)擊的條目
if (dataSet.index != 'undefined') {
var _index = parseInt(dataSet.index); //獲取點(diǎn)擊條目為數(shù)組的第幾項(xiàng)
historySearchs.splice(_index, 1); //從數(shù)組里刪除對(duì)應(yīng)的條目
this.setData({ //更新頁(yè)面
historySearchs: historySearchs
});
if(historySearchs.length==0){ //如果歷史記錄里沒(méi)有數(shù)據(jù)了
this.setData({
showSearchPanel: 1 //切換到熱門關(guān)鍵字
})
}
}
},
clearHistorySearchs: function () {
this.setData({
historySearchs: [], //清空歷史記錄數(shù)組
showSearchPanel: 1 //切換到熱門關(guān)鍵字
})
},
輸入框輸入事件:
bindKeyInput: function (e) {
var self = this;
self.setData({ //更新searchKey的值
searchKey: e.detail.value
});
if (e.detail.value == "") { //如果值為空且當(dāng)前未顯示熱門關(guān)鍵字
if (this.data.showSearchPanel != 1) {
self.setData({
showSearchPanel: 1 //切換為熱門關(guān)鍵字
})
}
}
},
確認(rèn)按鈕的點(diǎn)擊事件:
searchOk: function (e) {
var self = this;
var searchKey = this.data.searchKey; //獲取searchKey的值
if (searchKey != "") {
self.setData({
showSearchPanel: 3 //顯示搜索結(jié)果
});
self.addHistorySearchs(searchKey); //添加到歷史記錄
MusicService.getSearchMusic(searchKey, function (data) {
if (data.code == 0) {
var songData = data.data;
self.setData({
searchSongs: songData.song.list,
zhida: songData.zhida
});
}
});
}
},
搜索結(jié)果item的點(diǎn)擊事件,分為專輯與歌曲兩種:
zhidaTap: function (e) { //專輯的跳轉(zhuǎn)事件
var dataSet = e.currentTarget.dataset;
var mid = dataSet.id;
app.setGlobalData({ 'zhidaAlbummid': mid }); //將專輯id保存為全局變量
wx.navigateTo({ //頁(yè)面跳轉(zhuǎn)
url: '../cdinfo/cdinfo'
})
},
musuicPlay: function (e) { //歌曲的跳轉(zhuǎn)事件
var dataSet = e.currentTarget.dataset;
//TODO
}
},
“微信小程序音樂(lè)播放器的檢索頁(yè)如何制作”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。