1.在wxml页面添加搜索框 并添加事件
<input placeholder="搜索" bindinput="search"/>
2.触发事件之后 获取value数据 判断value是否有值,如果有值发list请求 如果没有发search请求
| > search(e){ |
| let that = this |
| |
| let value = e.detail.value; |
| let token = wx.getStorageSync('token'); |
| |
| if(!value){ |
| wx.request({ |
| url: 'http://www.www.com/index.php/list', |
| data:{ |
| pagesize:10, |
| page:page |
| }, |
| header:{ |
| token:token |
| }, |
| success(e){ |
| that.setData({ |
| data:e.data.data.data, |
| last_page:e.data.data.last_page |
| }) |
| } |
| }) |
| }else{ |
| that.setData({searchs:value}) |
| clearTimeout(this.TimeID); |
| this.TimeID = setTimeout(()=>{ |
| |
| wx.request({ |
| url: 'http://www.week.com/index.php/search', |
| data:{ |
| value:value, |
| pagesize:10 |
| }, |
| header:{ |
| token:token |
| }, |
| success(e){ |
| console.log(e) |
| that.setData({ |
| data:e.data.data.data, |
| last_page:e.data.data.last_page, |
| value:value |
| }) |
| } |
| }) |
| },1000); |
| } |
| }, |
3.加载更多
| onReachBottom: function () { |
| let that = this; |
| let searchs = this.data.searchs; |
| let value = this.data.value |
| let token = wx.getStorageSync('token'); |
| |
| let page = that.data.page+1; |
| if(page>this.data.last_page){ |
| wx.showToast({ |
| title: '到底了', |
| icon: 'error', |
| }) |
| } |
| if(!searchs){ |
| wx.request({ |
| url: 'http://www.week.com/index.php/list', |
| data:{ |
| pagesize:10, |
| page:page |
| }, |
| header:{ |
| token:token |
| }, |
| success(e){ |
| that.setData({ |
| page:that.data.page+1, |
| data:that.data.data.concat(e.data.data.data) |
| }) |
| } |
| }) |
| }else{ |
| wx.request({ |
| url: 'http://www.week.com/index.php/search', |
| data:{ |
| pagesize:10, |
| page:page, |
| value:value |
| }, |
| header:{ |
| token:token |
| }, |
| success(e){ |
| that.setData({ |
| page:that.data.page+1, |
| data:that.data.data.concat(e.data.data.data) |
| }) |
| } |
| }) |
| } |
| }, |