上回遗留的两个问题,首先要接收参数,在上回说了穿参使用url?xxx=xxx&xxx=xxx,然后在接受参数的页面的onLoad方法中可以直接获取,比如我传递的参数是search,我需要在onLoad中获取search并保存,大概代码如下:
- Page({
- data: {
-
- },
- onLoad: function(options) {
- var that = this;
-
- that.setData({
- <span style="white-space:pre"> </span> search : options.search
- });
- }
-
- })
然后在onLoad的时候还要获取第一页数据,使用wx.request来加载数据:
- onLoad: function(options) {
- var that = this;
-
- that.setData({
- search : options.search
- });
-
- wx.request({
- url: 'http://localhost:8080/getProductListByJson.do?search='+options.search,
- method: 'GET',
- data: {},
- header: {
- 'Accept': 'application/json'
- },
- success: function(res) {
- that.setData({
- list: res.data.result
- });
- }
- })
- }
因为我在后台判断没有带页数的话,默认为第一页,所以在这我并没有传递页码过去。
接下来就只剩下移动端经常用的下拉加载!
这里我不知道是不是有人跟我一样对下拉的理解似乎跟微信不一样?
微信的下拉刷新,是手指往下滑,需要在配置中开启:enablePullDownRefresh:true,然后在js文件中写
- onPullDownRefresh: function(){
- console.log('do something');
- },
而我们需要做的是手指往上滑,数据到底的时候加载更多数据,在js中的方法是
- onReachBottom: function () {
- console.log('get more');
- }
然后我们在onReachBottom中获取更多数据,添加在数据最下方:
- onReachBottom: function () {
- var that = this;
- var pageNum = that.data.pageNum;
- console.log('get more:'+pageNum);
- wx.request({
- url: 'http://localhost:8080/getProductListByJson.do?search='+that.data.search+'&pageNum='+pageNum,
- method: 'GET',
- data: {},
- header: {
- 'Accept': 'application/json'
- },
- success: function(res) {
- pageNum++;
- that.setData({
- list: that.data.list.concat(res.data.result),
- pageNum : pageNum
- });
- }
- })
- }
然后差不多就这样可以收工了,细节啥的嘛,用的时候再加吧!