微信小程序首頁(yè)的分類功能和搜索功能的實(shí)現(xiàn)思路及代碼詳解
就在昨天,微信宣布了微信小程序開(kāi)發(fā)者工具新增“云開(kāi)發(fā)”功能
下載最新的開(kāi)發(fā)者工具,現(xiàn)在無(wú)需服務(wù)器即可實(shí)現(xiàn)小程序的快速迭代!
分類功能和搜素功能的效果圖

1.首頁(yè)分類功能的實(shí)現(xiàn)
boxtwo方法(.js文件)
boxtwo: function (e) {
var index = parseInt(e.currentTarget.dataset.index)
this.setData({
HomeIndex: index
})
},
當(dāng)在首頁(yè)點(diǎn)擊 分類導(dǎo)航時(shí),會(huì)觸發(fā)這個(gè)方法,并傳回當(dāng)前點(diǎn)擊時(shí)的index值。
這個(gè)方法實(shí)現(xiàn)的是將.wxml文件傳來(lái)的index值賦給HomeIndex。
class="boxtwo-tab-nav {{HomeIndex == 0 ?'on':''}}"
.wxss樣式文件
.boxtwo-tab-nav{
display: inline-block;
width: 20%;
height: 90rpx;
line-height: 90rpx;
border-bottom: 1rpx solid #ededed;
box-sizing: border-box;
text-align: center;
color: black;
font-size: 30rpx
}
這樣就實(shí)現(xiàn)了首頁(yè) 當(dāng)前點(diǎn)擊的分類 呈現(xiàn)出 被選中的樣式。
然后在視圖層根據(jù)HomeIndex的不同,加載對(duì)應(yīng)的數(shù)據(jù)。
<view wx:if="{{HomeIndex == 1}}" >
<block wx:for="{{shareList}}" wx:key="*this">
<navigator url='../../pages/shareDetail/shareDetail?id={{item.id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{item.img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{item.title}}</view>
<view class="date">{{item.cTime}}</view>
</view>
</navigator>
</block>
</view>
<navigator></navigator>組件實(shí)現(xiàn)的是點(diǎn)擊當(dāng)前文章時(shí)傳出id到詳情頁(yè)面(detail)。這樣就把首頁(yè)的文章列表和文章的詳情頁(yè)面一一對(duì)應(yīng)起來(lái)了。
detail.js文件
onLoad: function (options) {
var that = this
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/School/School/getDetail',
data: {id:options.id},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({
key: 'info',
data: res.data,
})
that.setData({
info: res.data
})
}
})
}
2.搜索功能的實(shí)現(xiàn)
.wxml文件
<view class='search-view'>
<input class='input' confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='請(qǐng)輸入搜索內(nèi)容'></input>
<button class='search' bindtap="wxSearchFn" hover-class='button-hover'>搜索</button>
</view>
JavaScript indexOf() 方法
indexOf() 方法可返回某個(gè)指定的字符串值在字符串中首次出現(xiàn)的位置。
key為搜索的關(guān)鍵字,res.data[i].title為首頁(yè)列表的標(biāo)題。使用indexOf()方法時(shí),當(dāng)滿足了(res.data[i].title.indexOf(key) >= 0)說(shuō)明說(shuō)明輸入的關(guān)鍵字在文章列表中
也有相同的關(guān)鍵字,然后arr.push(res.data[i]),這樣就把篩選出來(lái)的文章放在了臨時(shí)數(shù)組arr中了
//搜索方法 key為用戶輸入的查詢字段
search: function (key) {
/*console.log('搜索函數(shù)觸發(fā)')*/
var that = this;
var newsList = wx.getStorage({
key: 'newsList',
success: function (res) {//從storage中取出存儲(chǔ)的數(shù)據(jù)*/
/*console.log(res)*/
if (key == '') {//用戶沒(méi)有輸入 全部顯示
that.setData({
newsList: res.data
})
return;
}
var arr = [];//臨時(shí)數(shù)組 用于存放匹配到的數(shù)據(jù)
for (let i in res.data) {
if (res.data[i].title.indexOf(key) >= 0) {//查找
arr.push(res.data[i])
}
}
if (arr.length == 0) {
that.setData({
newsList:[]
})
} else {
that.setData({
newsList: arr//在頁(yè)面顯示找到的數(shù)據(jù)
})
}
}
})
}
//搜素時(shí)觸發(fā),調(diào)用search: function (key),傳入輸入的e.detail.value值
wxSearchInput: function (e) {
this.search(e.detail.value);
}
index.wxml(首頁(yè))完整代碼
<view class='search-view'>
<input class='input' confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='請(qǐng)輸入搜索內(nèi)容'></input>
<button class='search' bindtap="wxSearchFn" hover-class='button-hover'>搜索</button>
</view>
<view class="boxtwo-tab-nav {{HomeIndex == 0 ?'on':''}}" bindtap="boxtwo" data-index="0">首頁(yè)</view>
<view class="boxtwo-tab-nav {{HomeIndex == 1 ?'on':''}}" bindtap="boxtwo" data-index="1">資源分享</view>
<view class="boxtwo-tab-nav {{HomeIndex == 2 ?'on':''}}" bindtap="boxtwo" data-index="2">微信小程序</view>
<view class="boxtwo-tab-nav {{HomeIndex == 3 ?'on':''}}" bindtap="boxtwo" data-index="3">網(wǎng)賺小項(xiàng)目</view>
<view class="boxtwo-tab-nav {{HomeIndex == 4 ?'on':''}}" bindtap="boxtwo" data-index="4">共享經(jīng)濟(jì)</view>
<view class="wrap">
<template name="lists">
<navigator url='../../pages/detail/detail?id={{id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{title}}</view>
<view class="date">{{cTime}}</view>
</view>
</navigator>
</template>
</view>
<view wx:if="{{HomeIndex == 0}}">
<block wx:for="{{newsList}}" wx:key="*this">
<template is="lists" data="{{...item}}"/>
</block>
</view>
<view wx:if="{{HomeIndex == 1}}" >
<block wx:for="{{shareList}}" wx:key="*this">
<navigator url='../../pages/shareDetail/shareDetail?id={{item.id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{item.img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{item.title}}</view>
<view class="date">{{item.cTime}}</view>
</view>
</navigator>
</block>
</view>
<view wx:if="{{HomeIndex == 2}}" >
<block wx:for="{{weixinList}}" wx:key="*this">
<navigator url='../../pages/weixinDetail/weixinDetail?id={{item.id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{item.img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{item.title}}</view>
<view class="date">{{item.cTime}}</view>
</view>
</navigator>
</block>
</view>
<view wx:if="{{HomeIndex == 3}}" >
<block wx:for="{{netearnList}}" wx:key="*this">
<navigator url='../../pages/netearnDetail/netearnDetail?id={{item.id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{item.img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{item.title}}</view>
<view class="date">{{item.cTime}}</view>
</view>
</navigator>
</block>
</view>
<view wx:if="{{HomeIndex == 4}}" >
<block wx:for="{{economyList}}" wx:key="*this">
<navigator url='../../pages/economyDetail/economyDetail?id={{item.id}}' hover-class="navigator-hover">
<view class='imgs'>
<image src="{{item.img}}" background-size="cover" mode="scaleToFill"></image>
</view>
<view class='infos'>
<view class="title">{{item.title}}</view>
<view class="date">{{item.cTime}}</view>
</view>
</navigator>
</block>
</view>
index.wxss(對(duì)應(yīng)的樣式文件)
.wrap{
height: 100%;
display:flex;
flex-direction: column;
padding: 20rpx
}
navigator{overflow: hidden}
.list{
margin-bottom: 20rpx;
height: 200rpx;
position: relative;
}
.imgs{
float: left;
}
.imgs image{
display: block;
width: 210rpx;
height: 180rpx;
}
.boxtwo-tab-nav{
display: inline-block;
width: 20%;
height: 90rpx;
line-height: 90rpx;
border-bottom: 1rpx solid #ededed;
box-sizing: border-box;
text-align: center;
color: black;
font-size: 30rpx
}
.on{
color:#405F80;
border-bottom: 5rpx solid #405F80;
}
.infos{
float: left;
width: 480rpx;
height: 200rpx;
padding: 20rpx 0 0 20rpx;
}
.date{
font-size:13px;color:#aaa;position: absolute;
}
.title{font-size: 15px;}
.search{
float: left;
width: 130rpx;
height: 70rpx;
margin-left: 0;
background-color: blueviolet;
font-size: 28rpx;
color: #fff;
border: none;
}
.input{
float: left;
width: 500rpx;
height: 70rpx;
font-size: 35rpx;
background-color: white;
}
.search-view{
position: relative;
overflow: hidden;
height: 70rpx;
padding: 20rpx 20rpx 25rpx 60rpx;
background-color: #6699FF;
}
.button-hover {
background-color: red;
}
.js文件(邏輯層)
Page({
data:{
newsList:[],
HomeIndex: 0
},
onLoad: function () {
var that = this;
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/School/School/getList',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res.data)
wx.setStorage({
key: 'newsList',
data: res.data,
})
that.setData({
newsList: res.data
})
}
})
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/Share/Share/getList',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({
key: 'sharesList',
data: res.data,
})
that.setData({
shareList: res.data
})
}
})
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/Weixin/Weixin/getList',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({
key: 'weixinList',
data: res.data,
})
that.setData({
weixinList: res.data
})
}
})
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/Netearn/Netearn/getList',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({
key: 'netearnList',
data: res.data,
})
that.setData({
netearnList: res.data
})
}
})
wx.request({
url: 'http://localhost:81/weicms/index.php?s=/addon/Economy/Economy/getList',
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
wx.setStorage({
key: 'economyList',
data: res.data,
})
that.setData({
economyList: res.data
})
}
})
},
//搜索方法 key為用戶輸入的查詢字段
search: function (key) {
/*console.log('搜索函數(shù)觸發(fā)')*/
var that = this;
var newsList = wx.getStorage({
key: 'newsList',
success: function (res) {//從storage中取出存儲(chǔ)的數(shù)據(jù)*/
/*console.log(res)*/
if (key == '') {//用戶沒(méi)有輸入 全部顯示
that.setData({
newsList: res.data
})
return;
}
var arr = [];//臨時(shí)數(shù)組 用于存放匹配到的數(shù)據(jù)
for (let i in res.data) {
if (res.data[i].title.indexOf(key) >= 0) {//查找
arr.push(res.data[i])
}
}
if (arr.length == 0) {
that.setData({
newsList:[]
})
} else {
that.setData({
newsList: arr//在頁(yè)面顯示找到的數(shù)據(jù)
})
}
}
})
},
//事件處理函數(shù)
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
wxSearchInput: function (e) {
this.search(e.detail.value);
console.log(e.detail.value)
},
wxSerchFocus: function (e) {
this.search(e.detail.value);
},
wxSearchBlur: function (e) {
this.search(e.detail.value);
},
wxSearchFn: function (e) {
/*console.log(e)*/
},
boxtwo: function (e) {
var index = parseInt(e.currentTarget.dataset.index)
this.setData({
HomeIndex: index
})
},
總結(jié)
以上所述是小編給大家介紹的微信小程序首頁(yè)的分類功能和搜索功能的實(shí)現(xiàn)思路及代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript函數(shù)擴(kuò)展與箭頭函數(shù)超詳細(xì)講解
這篇文章主要介紹了JavaScript函數(shù)擴(kuò)展與箭頭函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11
Javascript單元測(cè)試框架QUnitjs詳細(xì)介紹
這篇文章主要介紹了Javascript單元測(cè)試框架QUnitjs詳細(xì)介紹,需要的朋友可以參考下2014-05-05
同步異步動(dòng)態(tài)引入js文件的幾種方法總結(jié)
下面小編就為大家?guī)?lái)一篇同步異步動(dòng)態(tài)引入js文件的幾種方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
挺實(shí)用的20個(gè)JavaScript簡(jiǎn)化寫(xiě)法代碼技巧
掌握一些JavaScript的精簡(jiǎn)書(shū)寫(xiě)方式,有助增強(qiáng)代碼的閱讀性,提升代碼質(zhì)量,任何一種編程語(yǔ)言的簡(jiǎn)寫(xiě)小技巧都是為了幫助你寫(xiě)出更簡(jiǎn)潔、更完善的代碼,讓你用更少的編碼實(shí)現(xiàn)你的需求2023-08-08
JavaScript中this的用法及this在不同應(yīng)用場(chǎng)景的作用解析
由于其運(yùn)行期綁定的特性,JavaScript 中的 this 含義要豐富得多,它可以是全局對(duì)象、當(dāng)前對(duì)象或者任意對(duì)象,這完全取決于函數(shù)的調(diào)用方式,這篇文章主要給大家介紹了JavaScript中this的用法及this在不同應(yīng)用場(chǎng)景的作用解析,一起看看吧2017-04-04
不用typsescript如何使用類型增強(qiáng)功能
這篇文章主要給大家介紹了關(guān)于不用typsescript如何使用類型增強(qiáng)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
echarts 移動(dòng)端豎著顯示效果實(shí)現(xiàn)
這篇文章主要為大家介紹了echarts 移動(dòng)端豎著顯示效果實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

