步骤:
1. 参数: 第几页,每页多少条数据,前端get方式传递数据 2. 后台接收参数并获取数据,同时返回表记录总条数,用于判断最后一页
代码1:
// 获取音乐列表-controllers public function getMusicList($page, $num) { $data = $this->AlbumMusic_model->get($page, $num); $this->output->set_content_type('application/json')->set_output(json_encode($data)); } // 获取音乐列表-models public function get($page, $num) { $data = array( 'count' => $this->db->count_all('album_music'), 'data' => $this->db->get('album_music', $num, ($page-1)*$num)->result() ); return $data; }
代码2:
loadMusic (page, num) { let that = this // 获取音乐歌曲列表 wx.request({ url: app.globalData.host + '/album/getMusicList/' + page + '/' + num, success: res => { let musicList = res.data.data that.data.count = res.data.count // 添加设置 let music = wx.getStorageSync('music') for (let i = 0; i < musicList.length; i++) { musicList[i].play = false musicList[i].select = false if (musicList[i].id == music.id) { musicList[i].select = true } } that.setData({ musicList: musicList }) } }) }, beforeBtn (e) { this.data.page-- if(this.data.page < 1) { wx.showToast({ title: '已经是第一页了', icon: 'success', duration: 2000 }) this.data.page = 1 return } this.loadMusic(this.data.page, this.data.num_page) }, afterBtn (e) { this.data.page++ // console.log(this.data.page) if (this.data.page > Math.ceil(this.data.count / this.data.num_page)){ wx.showToast({ title: '没有更多音乐了', icon: 'success', duration: 2000 }) this.data.page = Math.ceil(this.data.count / this.data.num_page) return } this.loadMusic(this.data.page, this.data.num_page) }