1. 程式人生 > 實用技巧 >【UNI-APP】分頁請求資料列表

【UNI-APP】分頁請求資料列表

// 首先在對應的pages.json檔案中,配置重新整理效果
{
            "path" : "pages/list/list",
            "style" :                                                                                    
            {
                "navigationBarTitleText": "房源列表",
                "enablePullDownRefresh": true
            }
            
        }
// 在methods裡面定義請求資料介面

// 請求第1頁的資料
getList() {
                this.listData = []
                uni.request({
                    url: 'http://localhost:8000/api/house/list?page=1&size=7',
                    method: "POST",
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                        style: 
1, }, success: (ret) => { uni.stopPullDownRefresh(); // 停止當前頁面重新整理 if (ret.data.code == 200) { ret.data.data.some((item, i) => { this.listData.unshift({ title: item.title, price: item.price, id: item.id, image:
"http://localhost:8000" + item.image, time: item.time, }) }) // this.banner = data.data; } }, fail: (data, code) => { console.log('fail' + JSON.stringify(data)); } }) }, // 請求第N頁的資料 /* 分頁請求 */ getPageList() { uni.request({ url: 'http://localhost:8000/api/house/list?page=' + this.page + '&size=7', method: "POST", header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { style: 1, }, success: (ret) => { console.log(ret) uni.stopPullDownRefresh(); // 停止當前頁面重新整理 if (ret.data.code == 200) { this.page = this.page + 1 // 頁面+1 ret.data.data.some((item, i) => { this.listData.push({ title: item.title, price: item.price, id: item.id, image: "http://localhost:8000" + item.image, time: item.time, }) }) } else { /* 如果介面報錯 就是沒資料 載入消失 */

                  //  <!-- 下拉載入 --> 載入HTML
                  //  <view style="text-align: center;width: 100%;">
                  //   <u-loading mode="circle" :show="show" size="36">載入中...</u-loading>
                  //  </view>

                            this.show = false
                        }
                    },
                    fail: (data, code) => {

                    }
                })
            },
// 下面是下拉上啦的配置函式
onLoad() {
            /* 列表 */
            this.getList();
        },

        onPullDownRefresh() {
            /* 下拉的時候更新 */
            this.getList();
        },

        onReachBottom() {
            // console.log("向下拉")
            //此處使用定時器可查看出下拉重新整理效果
            setTimeout(() => {
                this.getPageList(() => {
                    uni.stopPullDownRefresh()
                })
            }, 1000)
        },