博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序首页获取数据给数组赋值,实现加载更多,以及遇到的坑
阅读量:7197 次
发布时间:2019-06-29

本文共 6163 字,大约阅读时间需要 20 分钟。

 
刷新成功
加载中
加载完成
暂无数据

不能直接把后台返回的数组数据赋值到定义的空数组中,一定要concat连接,否则结果是看似是个数组,但是获取到的该数组却为空,导致第一次上拉加载更多的时候是拿到的之前的数组依然是空数组。

 

var config
=
require
(
'../../config'
)
var util
=
require
(
'../../utils/util.js'
)
Component
({
properties
:
{
category
:
{
type
: String
,
value
:
"index"
},
params
:
{
type
: String
,
value
:
""
},
},
data
:
{
newsList
:
[],
page
:
0
,
size
:
10
,
totalPages
:
0
,
refreshCompeleted
:
false
,
loadingCompeleted
:
false
,
loading
:
false
,
noData
:
false
},
methods
:
{
getList
()
{
let that
=
this
;
that
.setData
({
page
:
0
})
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
)
},
},
})

转载于:https://www.cnblogs.com/beileixinqing/p/9468099.html

你可能感兴趣的文章
java中的常量定义
查看>>
Linux中目录操作命令
查看>>
Linux下的touch命令及时间戳
查看>>
安装如Epson LQ-300K等老式打印机方法和心得
查看>>
菜鸟的第一次编程感受
查看>>
对代码命名的一点思考和理解
查看>>
shell监测mysql是否启动
查看>>
redis的单机安装与配置以及生产环境启动方案
查看>>
centos 安装raid驱动及部署实战
查看>>
程序员保值的4个秘密
查看>>
apache安装完成后,添加模块
查看>>
Linux中查看CPU信息
查看>>
网络性能测试工具Iperf介绍
查看>>
基本粒子群优化算法(PSO)的matlab实现
查看>>
我的友情链接
查看>>
go 生成随机字符串和获得定长字符串
查看>>
设置固定IP
查看>>
Sent Items 不见了
查看>>
Style3D属性面板的开发指南
查看>>
Mac OS X 从零开始系列教程10-刻录光盘
查看>>