麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 課堂 > 小程序 > 正文

微信小程序首頁的分類功能和搜索功能的實現思路及代碼詳解

2020-03-21 16:10:47
字體:
來源:轉載
供稿:網友

就在昨天,微信宣布了微信小程序開發者工具新增“云開發”功能

下載最新的開發者工具,現在無需服務器即可實現小程序的快速迭代!

分類功能和搜素功能的效果圖

微信小程序,分類

1.首頁分類功能的實現

boxtwo方法(.js文件)

boxtwo: function (e) {  var index = parseInt(e.currentTarget.dataset.index)  this.setData({   HomeIndex: index  }) },

當在首頁點擊 分類導航時,會觸發這個方法,并傳回當前點擊時的index值。

這個方法實現的是將.wxml文件傳來的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}

這樣就實現了首頁 當前點擊的分類 呈現出 被選中的樣式。

然后在視圖層根據HomeIndex的不同,加載對應的數據。

<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>組件實現的是點擊當前文章時傳出id到詳情頁面(detail)。這樣就把首頁的文章列表和文章的詳情頁面一一對應起來了。

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.搜索功能的實現

.wxml文件

<view class='search-view'>  <input class='input' confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='請輸入搜索內容'></input>  <button class='search' bindtap="wxSearchFn" hover-class='button-hover'>搜索</button></view>

JavaScript indexOf() 方法

   indexOf() 方法可返回某個指定的字符串值在字符串中首次出現的位置。

key為搜索的關鍵字,res.data[i].title為首頁列表的標題。使用indexOf()方法時,當滿足了(res.data[i].title.indexOf(key) >= 0)說明說明輸入的關鍵字在文章列表中

也有相同的關鍵字,然后arr.push(res.data[i]),這樣就把篩選出來的文章放在了臨時數組arr中了

//搜索方法 key為用戶輸入的查詢字段 search: function (key) {  /*console.log('搜索函數觸發')*/  var that = this;  var newsList = wx.getStorage({   key: 'newsList',   success: function (res) {//從storage中取出存儲的數據*/   /*console.log(res)*/    if (key == '') {//用戶沒有輸入 全部顯示     that.setData({      newsList: res.data     })     return;    }    var arr = [];//臨時數組 用于存放匹配到的數據    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//在頁面顯示找到的數據     })    }   }  })  }//搜素時觸發,調用search: function (key),傳入輸入的e.detail.value值wxSearchInput: function (e) { this.search(e.detail.value);}

index.wxml(首頁)完整代碼

<view class='search-view'>  <input class='input' confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='請輸入搜索內容'></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">首頁</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">網賺小項目</view><view class="boxtwo-tab-nav {{HomeIndex == 4 ?'on':''}}" bindtap="boxtwo" data-index="4">共享經濟</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(對應的樣式文件)

.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('搜索函數觸發')*/  var that = this;  var newsList = wx.getStorage({   key: 'newsList',   success: function (res) {//從storage中取出存儲的數據*/   /*console.log(res)*/    if (key == '') {//用戶沒有輸入 全部顯示     that.setData({      newsList: res.data     })     return;    }    var arr = [];//臨時數組 用于存放匹配到的數據    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//在頁面顯示找到的數據     })    }   }  })  }, //事件處理函數 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  }) },

總結

以上所述是小編給大家介紹的微信小程序首頁的分類功能和搜索功能的實現思路及代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人性视频在线 | 色中色在线播放 | 久久精品首页 | 久草在线综合 | 日日操夜夜操视频 | 久久嗨 | 国产二区三区视频 | 欧美一级高潮片免费的 | 精品一区二区三区免费看 | 免费a级黄色片 | 国产色91| 亚洲欧美国产视频 | 日本网站一区 | 日韩视频高清 | lutube成人福利在线观看污 | 一区二区久久电影 | 亚洲综合视频网 | 国产男女爽爽爽爽爽免费视频 | 久久99亚洲精品 | 成年人在线视频免费 | 久久精品视频12 | 久久精品亚洲一区二区 | 久久国产一级片 | 黄色一级片在线观看 | 免费激情网址 | 看免费5xxaaa毛片 | 国产亚洲精品综合一区91 | 天天草天天爱 | 国产分类视频 | 成人精品一区二区三区中文字幕 | 久久国产一 | 亚洲精品无码不卡在线播放he | 天天操天天碰 | 老女人碰碰在线碰碰视频 | 九一国产精品 | 欧美精品日日鲁夜夜添 | 精品一区二区亚洲 | 成人福利视频导航 | 91短视频在线视频 | 欧美精品一区自拍a毛片在线视频 | 在线观看网址av |