微信小程序首页的分类功能和搜索功能的实现思

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

下载最新的开发者工具,现在无需服务器即可实现小程序的快速迭代!

分类功能和搜素功能的效果图

微信小程序首页的分类功能和搜索功能的实现思

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> <image src="https://www.jb51.net/{{item.img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{item.title}}</view> <view>{{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=https://www.jb51.net/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> <input confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='请输入搜索内容'></input> <button 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> <input confirm-type="search" maxlength="30" bindinput='wxSearchInput' value='{{keyword}}' bindconfirm='wxSearchFn' bindfocus="wxSerchFocus" bindblur="wxSearchBlur" placeholder='请输入搜索内容'></input> <button bindtap="wxSearchFn" hover-class='button-hover'>搜索</button> </view> <view bindtap="boxtwo" data-index="0">首页</view> <view bindtap="boxtwo" data-index="1">资源分享</view> <view bindtap="boxtwo" data-index="2">微信小程序</view> <view bindtap="boxtwo" data-index="3">网赚小项目</view> <view bindtap="boxtwo" data-index="4">共享经济</view> <view> <template> <navigator url='../../pages/detail/detail?id={{id}}' hover-class="navigator-hover"> <view> <image src="https://www.jb51.net/{{img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{title}}</view> <view>{{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> <image src="https://www.jb51.net/{{item.img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{item.title}}</view> <view>{{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> <image src="https://www.jb51.net/{{item.img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{item.title}}</view> <view>{{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> <image src="https://www.jb51.net/{{item.img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{item.title}}</view> <view>{{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> <image src="https://www.jb51.net/{{item.img}}" background-size="cover" mode="scaleToFill"></image> </view> <view> <view>{{item.title}}</view> <view>{{item.cTime}}</view> </view> </navigator> </block> </view>

index.wxss(对应的样式文件)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/92202e9bbb86cf94d480b474f3471bfa.html