永利402com官方网站微信小程序之上拉加载更多,以及遭逢的坑

永利402com官方网站 1
永利402com官方网站

 

loadmore

<!-- wxml-->

<view>
  <view class="refreshTip" wx:if="{{refreshCompeleted==true}}">刷新成功</view>
  <news-list-item wx:for="{{newsList}}" news="{{item}}" wx:key="item.id"></news-list-item>
  <view class="weui-loadmore" wx:if="{{loading===true}}">
    <view class="weui-loading"></view>
    <view class="weui-loadmore__tips">加载中</view>
  </view>
  <view class="weui-loadmore" wx:if="{{loadingCompeleted===true}}">
    <view class="weui-loadmore__tips">加载完成</view>
  </view>
  <view class="weui-loadmore weui-loadmore_line" wx:if="{{noData===true}}">
    <view class="weui-loadmore__tips weui-loadmore__tips_in-line">暂无数据</view>
  </view>
</view>

加载越来越多(分页加载)

永利402com官方网站 1

 

不可能直接把后台重临的数组数据赋值到定义的空数组中,一定要concat连接,不然结果是近乎是个数组,可是获取到的该数组却为空,导致第壹遍上拉加载越多的时候是得到的前边的数组依然是空数组。

当用户展开二个页面时,假诺后台数据量庞大时,2遍性地赶回全部数据给客户端,页面包车型客车开拓速度就能够怀有降低,而且用户只看上边的开始和结果而无需看前边的始末时,也浪开销户流量,基于优化的角度来设想,后台不要一回性重回全体数据,当用户有亟待再往下翻的时候,再加载尤其数据出来。

 

 

var config = require(‘../../config’)

事务须求:

var util = require(‘../../utils/util.js’)

列表滚动到底层时,继续往上拉,加载更加多内容

Component({

 

properties: {

须求参数:

category: {

(一)pageindex: 1 //第四遍加载

type: String,

(二)callbackcount: 一5 //须求回到数据的个数

value: “index”

任何参数:

},

据悉接口的所需参数

params: {

 

type: String,

落到实处原理:

value: “”

当第3回访问接口时,传递3个要求参数(第二回加载,供给再次回到数据的个数为一多个),和其余参数(需求探索的字符串)给后台,后台再次回到第三遍数据复苏。在伸手成功的的回调函数中,剖断重临的数目是不是>0,是,则抽出数据,渲染视图层,并把“上拉加载”展现在列表尾部;否,则尚未数据可取,并把“未有更加多”展现在列表尾部,同时把“上拉加载”隐藏掉。

},

当用户已经滚动到列表后面部分(这里运用到小程序提供的scroll-view组件的bindscrolltolower事件),触发bindscrolltolower事件,参数pageindex+1,再把1个必备参数(第二次加载,必要再次回到数据的个数为1五个)和任何参数(需求探求的字符串)给后台,后台把别的的数额重返给前台,前台在原来数据的底子上添增加少。

},

 

data: {

wxml:

newsList: [],

<view class=”search”>
<view class=”search-bar”>
<view class=”search-wrap”>
<icon type=”search” size=”16″ class=”icon-search” />
<input type=”text” placeholder=”请输入寻找内容” class=”search-input”
name=”searchKeyword” bindinput=”bindKeywordInput”
value=”{{searchKeyword}}” />
</view>
<view class=”search-cancel”
bindtap=”keywordSearch”>搜索</view>
</view>
<view class=”search-result”>
<scroll-view scroll-y=”true”
bindscrolltolower=”searchScrollLower”>
<view class=”result-item” wx:for=”{{searchSongList}}” wx:key=”unique”
data-data=”{{item}}” >
<view class=”icon{{item.isonly==’0′ ? ‘ nocopyright’ :
”}}”></view>
<text class=”title”>{{item.songname}}</text>
<view class=”subtitle”>
<text wx:for=”{{item.singer}}”
wx:key=”unique”>{{item.name}}</text>
</view>
</view>
<view class=”loading”
hidden=”{{!searchLoading}}”>正在载入越多…</view>
<view class=”loading complete”
hidden=”{{!searchLoadingComplete}}”>已加载全体</view>
</scroll-view>
</view>
</view>

page: 0,

 

size: 10,

JS:

totalPages: 0,

var util = require(‘../../utils/util.js’)
Page({
data: {
searchKeyword: ”, //供给搜求的字符
searchSongList: [], //放置再次来到数据的数组
isFromSearch: true, //
用于判定searchSongList数组是不是空数组,暗许true,空的数组
searchPageNum: 1, // 设置加载的第一回,暗中同意是第三遍
callbackcount: 一五, //重临数据的个数
searchLoading: false, //”上拉加载”的变量,暗中认可false,隐藏
searchLoadingComplete: false //“未有多少”的变量,默许false,隐藏
},
//输入框事件,每输入贰个字符,就能触发二遍
bindKeywordInput: function(e){
console.log(“输入框事件”)
this.setData({
searchKeyword: e.detail.value
})
},
//寻找,访问互联网
fetchSearchList: function(){
let that = this;
let searchKeyword = that.data.searchKeyword,//输入框字符串作为参数
searchPageNum = that.data.searchPageNum,//把第几回加载次数作为参数
callbackcount =that.data.callbackcount; //重临数据的个数
//访问互连网
util.getSearchMusic(searchKeyword, searchPageNum,callbackcount,
function(data){
console.log(data)
//剖断是不是有数量,有则取多少
if(data.data.song.curnum != 0){
let searchList = [];
//要是isFromSearch是true从data中收取数据,不然先从原本的多少持续丰裕
that.data.isFromSearch ? searchList=data.data.song.list :
searchList=that.data.searchSongList.concat(data.data.song.list)
that.setData({
searchSongList: searchList, //获取数据数组
zhida: data.data.zhida, //存放歌唱家属性的对象
searchLoading: true //把”上拉加载”的变量设为false,展现
});
//未有数量了,把“未有数量”呈现,把“上拉加载”隐藏
}else{
that.setData({
searchLoadingComplete: true, //把“未有多少”设为true,展现
searchLoading: false //把”上拉加载”的变量设为false,隐藏
});
}
})
},
//点击找出按键,触发事件
keywordSearch: function(e){
this.setData({
searchPageNum: 一, //第1回加载,设置一
searchSongList:[], //放置重回数据的数组,设为空
isFromSearch: true, //第3遍加载,设置true
searchLoading: true, //把”上拉加载”的变量设为true,突显
searchLoadingComplete:false //把“未有多少”设为false,隐藏
})
this.fetchSearchList();
},
//滚动到底层触发事件
searchScrollLower: function(){
let that = this;
if(that.data.searchLoading && !that.data.searchLoadingComplete){
that.setData({
searchPageNum: that.data.searchPageNum+壹,
//每一回触发上拉事件,把searchPageNum+壹
isFromSearch: false //触发到上拉风云,把isFromSearch设为为false
});
that.fetchSearchList();
}
}
})

refreshCompeleted: false,

 

loadingCompeleted: false,

util.js:

loading: false,

function getSearchMusic(keyword, pageindex, callbackcount, callback){
wx.request({
永利402com官方网站,url: ”,
data: {
g_tk: 5381,
uin: 0,
format: ‘json’,
inCharset: ‘utf-8’,
outCharset: ‘utf-8’,
notice: 0,
platform: ‘h5’,
needNewCode: 1,
w: keyword,
zhidaqu: 1,
catZhida: 1,
t: 0,
flag: 1,
ie: ‘utf-8’,
sem: 1,
aggr: 0,
perpage: 20,
n: callbackcount, //再次来到数据的个数
p: pageindex,
remoteplace: ‘txt.mqq.all’,
_: Date.now()
},
method: ‘GET’,
header: {‘content-Type’: ‘application/json’},
success: function(res){
if(res.statusCode == 200){
callback(res.data);
}
}
})
}

noData: false

module.exports = {
getSearchMusic: getSearchMusic
}

},

 

methods: {

wxss:

getList() {

page{
display: flex;
flex-direction: column;
height: 100%;
}

let that = this;

/*搜索*/
.search{
flex: auto;
display: flex;
flex-direction: column;
background: #fff;
}
.search-bar{
flex: none;
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx;
background: #f4f4f4;
}
.search-wrap{
position: relative;
flex: auto;
display: flex;
align-items: center;
height: 80rpx;
padding: 0 20rpx;
background: #fff;
border-radius: 6rpx;
}
.search-wrap .icon-search{
margin-right: 10rpx;
}
.search-wrap .search-input{
flex: auto;
font-size: 28rpx;
}
.search-cancel{
padding: 0 20rpx;
font-size: 28rpx;
}

that.setData({

/*搜索结果*/
.search-result{
flex: auto;
position: relative;
}
.search-result scroll-view{
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
}
.result-item{
position: relative;
display: flex;
flex-direction: column;
padding: 20rpx 0 20rpx 110rpx;
overflow: hidden;
border-bottom: 2rpx solid #e5e5e5;
}

page: 0

.result-item .media{
position: absolute;
left: 16rpx;
top: 16rpx;
width: 80rpx;
height: 80rpx;
border-radius: 999rpx;
}
.result-item .title,
.result-item .subtitle{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
line-height: 36rpx;
}
.result-item .title{
margin-bottom: 4rpx;
color: #000;
}
.result-item .subtitle{
color: #808080;
font-size: 24rpx;
}
.result-item:first-child .subtitle text{
margin-right: 20rpx;
}
.result-item:not(:first-child) .subtitle
text:not(:first-child):before{
content: ‘/’;
margin: 0 8rpx;
}
.loading{
padding: 10rpx;
text-align: center;
}
.loading:before{
display: inline-block;
margin-right: 5rpx;
vertical-align: middle;
content: ”;
width: 40rpx;
height: 40rpx;
background: url(../..icon-loading.png) no-repeat;
background-size: contain;
animation: rotate 1s linear infinite;
}
.loading.complete:before{
display: none;
}

})

if (that.data.category === “index”) {

util.get(`${config.service.host}/web/news/list_with_pub_info?channelId=${config.channelId}&page=0&size=${that.data.size}`,
function(res)
{

res.data.content.filter((value) => {

return value.createAt = util.formatTimeDistance(new
Date(value.createAt))

})

let newsList = that.data.newsList.concat(res.data.content);//这里要尤其注意,无法平昔that.setData({newsList:res.data.content})
,见上边注释

that.setData({

newsList: newsList,

totalPages: res.data.totalPages,

})

if (res.data.content.length = 0)
{

that.setData({

noData: true

})

}

if (res.data.content.length !== 0
&& that.data.page + 1
== that.data.totalPages) {

that.setData({

loadingCompeleted: true,

loading: false,

noData: false

})

}

setTimeout(function() {

that.setData({

refreshCompeleted: true

})

}, 1000)

setTimeout(function() {

that.setData({

refreshCompeleted: false

})

}, 2000)

}, function(e)
{

console.log(JSON.stringify(e));

});

} else if (that.data.category === “search”) {

util.get(`${config.service.host}/web/news/search_in_channel_with_pub_info?channelId=${config.channelId}&page=${that.data.page}&size=${that.data.size}&q=${that.data.params}`,
function(res)
{

res.data.content.filter((value) => {

value.createAt = util.formatTimeDistance(new
Date(value.createAt));

let keyword = that.data.params;

let re = new
RegExp(keyword, “g”);

value.title = value.title.replace(re,
`<span
class=”keyword”>${keyword}</span>`);

return value

})

let newsList = that.data.newsList.concat(res.data.content);

that.setData({

newsList: newsList,

totalPages: res.data.totalPages,

})

console.log(res.data.content.length)

if (res.data.content.length === 0)
{

that.setData({

noData: true

})

}

if (res.data.content.length !== 0
&& that.data.page + 1
== that.data.totalPages) {

that.setData({

loadingCompeleted: true,

loading: false,

noData: false

})

}

setTimeout(function() {

that.setData({

refreshCompeleted: true

})

}, 1000)

setTimeout(function() {

that.setData({

refreshCompeleted: false

})

}, 2000)

}, function(e)
{

console.log(JSON.stringify(e));

});

} else if (that.data.category === “professor”) {

util.get(`${config.service.host}/web/news/list_by_follow_with_pub_info?pubId=${that.data.params}&page=${that.data.page}&size=${that.data.size}&q=${that.data.params}`,
function(res)
{

res.data.content.filter((value) => {

return value.createAt = util.formatTimeDistance(new
Date(value.createAt))

})

let newsList = that.data.newsList.concat(res.data.content);

that.setData({

newsList: newsList,

totalPages: res.data.totalPages,

})

if (res.data.content.length === 0)
{

that.setData({

noData: true

})

}

if (res.data.content.length !== 0
&& that.data.page + 1
== that.data.totalPages) {

that.setData({

loadingCompeleted: true,

loading: false,

noData: false

})

}

setTimeout(function() {

that.setData({

refreshCompeleted: true

})

}, 1000)

setTimeout(function() {

that.setData({

refreshCompeleted: false

})

}, 2000)

}, function(e)
{

console.log(JSON.stringify(e));

});

}

},

getMoreList: function() {

let that = this;

setTimeout(function() {

if (that.data.page + 1
< that.data.totalPages) {

that.setData({

page: that.data.page + 1,

loading: true

})

if (that.data.category === “index”) {

util.get(`${config.service.host}/web/news/list_with_pub_info?channelId=${config.channelId}&page=${that.data.page}&size=${that.data.size}`,
function(res)
{

res.data.content.filter((value) => {

return value.createAt = util.formatTimeDistance(new
Date(value.createAt))

})

that.setData({

newsList: that.data.newsList.concat(res.data.content),

totalPages: res.data.totalPages,

loading: false

})

}, function(e)
{

console.log(JSON.stringify(e));

});

} else if (that.data.category === “search”) {

util.get(`${config.service.host}/web/news/search_in_channel_with_pub_info?channelId=${config.channelId}&page=${that.data.page}&size=${that.data.size}&q=${that.data.params}`,
function(res)
{

res.data.content.filter((value) => {

value.createAt = util.formatTimeDistance(new
Date(value.createAt));

let keyword = that.data.params;

let re = new
RegExp(keyword, “g”);

value.title = value.title.replace(re,
`<text
class=”keyword”>${keyword}</text>`);

return value

})

that.setData({

newsList: that.data.newsList.concat(res.data.content),

totalPages: res.data.totalPages,

loading: false

})

}, function(e)
{

console.log(JSON.stringify(e));

});

} else if (that.data.category === “professor”) {

util.get(`${config.service.host}/web/news/list_by_follow_with_pub_info?pubId=${that.data.params}&page=${that.data.page}&size=${that.data.size}&q=${that.data.params}`,
function(res)
{

res.data.content.filter((value) => {

return value.createAt = util.formatTimeDistance(new
Date(value.createAt))

})

that.setData({

newsList: that.data.newsList.concat(res.data.content),

totalPages: res.data.totalPages,

loading: false

})

}, function(e)
{

console.log(JSON.stringify(e));

});

}

} else {

that.setData({

loading: false,

loadingCompeleted: true

})

}

}, 500)

},

},

})

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图