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


Posted in Javascript onSeptember 11, 2018

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

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

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

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

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

总结

以上所述是小编给大家介绍的微信小程序首页的分类功能和搜索功能的实现思路及代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
CSS+Table图文混排中实现文本自适应图片宽度(超简单+跨所有浏览器)
Feb 14 Javascript
jquery 关于event.target使用的几点说明介绍
Apr 26 Javascript
当鼠标移动到图片上时跟随鼠标显示放大的图片效果
Jun 06 Javascript
JavaScript var声明变量背后的原理示例解析
Oct 12 Javascript
jQuery中trigger()方法用法实例
Jan 19 Javascript
JQuery工具函数汇总
Jun 15 Javascript
JS html时钟制作代码分享
Mar 03 Javascript
如何解决vue与传统jquery插件冲突
Mar 20 Javascript
手把手教你搭建ES6的开发运行环境
Jul 11 Javascript
微信小程序如何利用getCurrentPages进行页面传值
Jul 01 Javascript
layer扩展打开/关闭动画的方法
Sep 23 Javascript
vue项目初始化到登录login页面的示例
Oct 31 Javascript
vue项目中使用tinymce编辑器的步骤详解
Sep 11 #Javascript
VUE 实现滚动监听 导航栏置顶的方法
Sep 11 #Javascript
vue中的watch监听数据变化及watch中各属性的详解
Sep 11 #Javascript
vue axios数据请求get、post方法及实例详解
Sep 11 #Javascript
js监听html页面的上下滚动事件方法
Sep 11 #Javascript
vue 使用html2canvas将DOM转化为图片的方法
Sep 11 #Javascript
Vue项目数据动态过滤实践及实现思路
Sep 11 #Javascript
You might like
php数组对百万数据进行排除重复数据的实现代码
2010/06/08 PHP
php魔术变量用法实例详解
2014/11/13 PHP
浅析THINKPHP的addAll支持的最大数据量
2015/02/03 PHP
PHP使用trim函数去除字符串左右空格及特殊字符实例
2016/01/07 PHP
jQuery$命名冲突怎么办如何解决
2014/01/16 Javascript
标题过长使用javascript按字节截取字符串
2014/04/24 Javascript
node.js中的fs.futimesSync方法使用说明
2014/12/17 Javascript
AngularJS的一些基本样式初窥
2015/07/27 Javascript
深入解析JavaScript框架Backbone.js中的事件机制
2016/02/14 Javascript
基于JSON格式数据的简单jQuery幻灯片插件(jquery-slider)
2016/08/10 Javascript
canvas绘制环形进度条
2017/02/23 Javascript
nodejs同步调用获取mysql数据时遇到的大坑
2019/03/02 NodeJs
[06:07]刀塔密之二:攻之吾命受之吾幸
2014/07/03 DOTA
Python的自动化部署模块Fabric的安装及使用指南
2016/01/19 Python
Python处理json字符串转化为字典的简单实现
2016/07/07 Python
Python中对象的引用与复制代码示例
2017/12/04 Python
python如何对实例属性进行类型检查
2018/03/20 Python
对numpy.append()里的axis的用法详解
2018/06/28 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
基于python历史天气采集的分析
2019/02/14 Python
eclipse创建python项目步骤详解
2019/05/10 Python
Python_查看sqlite3表结构,查询语句的示例代码
2019/07/17 Python
python numpy 反转 reverse示例
2019/12/04 Python
python GUI库图形界面开发之PyQt5滑块条控件QSlider详细使用方法与实例
2020/02/28 Python
使用Python画了一棵圣诞树的实例代码
2020/11/27 Python
html5+css3之CSS中的布局与Header的实现
2014/11/21 HTML / CSS
CSS3实现全景图特效示例代码
2018/03/26 HTML / CSS
为什么如下的代码int a=100,b=100;long int c=a * b;不能工作
2013/11/29 面试题
将一个文本文件的内容按倒序打印出来
2015/01/05 面试题
大学生职业生涯规划范文——找准自我,定位人生
2014/01/23 职场文书
幼儿园家长评语
2014/02/10 职场文书
车辆工程专业求职信
2014/04/28 职场文书
中等生评语大全
2014/05/04 职场文书
2015年植树节活动总结
2015/02/06 职场文书
python编写五子棋游戏
2021/05/25 Python
Python将CSV文件转化为HTML文件的操作方法
2021/06/30 Python