欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序首页的分类功能和搜索功能的实现思路及代码详解

程序员文章站 2022-04-10 20:07:44
就在昨天,微信宣布了微信小程序开发者工具新增“云开发”功能 下载最新的开发者工具,现在无需服务器即可实现小程序的快速迭代! 分类功能和搜素功能的效果图 1.首页分...

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

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

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

微信小程序首页的分类功能和搜索功能的实现思路及代码详解

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
  })
 },

总结

以上所述是小编给大家介绍的微信小程序首页的分类功能和搜索功能的实现思路及代码详解,希望对大家有所帮助