⼩程序⾸页的分类功能和搜索功能的实现思路及代码详解就在昨天,宣布了⼩程序开发者⼯具新增“云开发”功能
下载最新的开发者⼯具,现在⽆需服务器即可实现⼩程序的快速迭代!
分类功能和搜素功能的效果图
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
url: '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;
url: '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
})
}
})
url: '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
})
}
})
url: '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
})
}
})
url: '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
})
}
})
url: '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
})
},
总结
以上所述是⼩编给⼤家介绍的⼩程序⾸页的分类功能和搜索功能的实现思路及代码详解,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。