使用Vant如何實(shí)現(xiàn)數(shù)據(jù)分頁(yè),下拉加載
更新時(shí)間:2022年06月28日 15:24:11 作者:倘若hfl
這篇文章主要介紹了使用Vant實(shí)現(xiàn)數(shù)據(jù)分頁(yè)及下拉加載方式。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Vant-ui的van-list實(shí)現(xiàn)數(shù)據(jù)分頁(yè)加載
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>vant數(shù)據(jù)分頁(yè),下拉加載</title> <link rel="stylesheet" rel="external nofollow" /> </head> <style> </style> <body> <div id='app'> <van-list class="lazy" v-model="loading" :finished="finished" finished-text="沒(méi)有更多了" @load="onLoad" :immediate-check="false"> <div v-for="(item,index) in list" :key="index">{{item}}</div> </van-list> </div> </body> <script src="https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vant@2.11/lib/vant.min.js"></script> <script> var Vue = new Vue({ el: '#app', data: { list: [], page: 1, loading: false, finished: false, num: 0 }, created() { this.getList() }, mounted() { }, methods: { // 請(qǐng)求公共方法 ajax(url, params, cb) { $.ajax({ type: 'post', url: url, data: params, dataType: "json", success: function (response) { cb(response) } }); }, onLoad() { this.getList() }, getList() { let that = this that.ajax('url', { kay: 'value' }, function (res) { if (res.errcode != 0) { that.$toast(res.msg) return false } if (that.page == 1) { that.list = res.data.list } else { that.list = that.list.concat(res.data.list) } that.loading = false; that.page++ //最后一次請(qǐng)求返回的數(shù)據(jù)為空或小于10條,不在請(qǐng)求,finished = true //根據(jù)業(yè)務(wù)需求更改 if (res.data.list.length == 0 || res.data.list == null || res.data.list.length < 10) { that.finished = true return } }) } } }) </script> </html>
主要三個(gè)屬性
注意:
v-model
每次數(shù)據(jù)加載完成要置為falsefinished
置為false后將不再觸發(fā)下拉加載immediate-check
置為false后,每次進(jìn)入頁(yè)面將不會(huì)觸發(fā)load方法,防止進(jìn)入頁(yè)面多次加載
vant上拉加載更多,下拉刷新
1.html
? ?<van-pull-refresh v-model="isLoading" @refresh="onRefresh"> ? ? ? ? ? ? <van-list ? ? ? ? ? ? ? v-model="loading" ? ? ? ? ? ? ? :finished="finished" ? ? ? ? ? ? ? :immediate-check="false" ? ? ? ? ? ? ? finished-text="沒(méi)有更多了呦" ? ? ? ? ? ? ? @load="onLoad" ? ? ? ? ? ? >? ? ? ? ? ? ? ? ? ? ? ? </van-list> ? ? ? ? ? </van-pull-refresh>
2.js
?return {? ?? ? ? ? isLoading: false, ? ? ? loading: false,? ? ? ?? ? ? ? page: 1, ? ? ? limit: 10, ? ? ? finished: false, ? ? ? total: 0, // 總共的數(shù)據(jù)條數(shù) ? ? ? List: [],? ? ? } ? ? ?getHistory() { ? ? ? const historyData = { ? ? ? ? page: this.page, ? ? ? ? limit: this.limit ? ? ? } ? ? ? return new Promise((resolve, reject) => { ? ? ? ? getHistory(historyData) ? ? ? ? ? .then(res => { ? ? ? ? ? ? if (res.code === 0) { ? ? ? ? ? ? ? console.log(res, '歷史記錄') ? ? ? ? ? ? ? this.total = res.data.total ? ? ? ? ? ? ? this.finished = !res.data.hasNext ? ? ? ? ? ? ? if (res.data.list && res.data.list.length > 0) { ? ? ? ? ? ? ? ? const tempList = res.data.list ? ? ? ? ? ? ? ? // console.log(this.page) ? ? ? ? ? ? ? ? if (this.page > 1) { ? ? ? ? ? ? ? ? ? this.list = this.list.concat(tempList) ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? this.list = tempList // 第一次加載 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? this.page += 1 ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? this.list = [] ? ? ? ? ? ? ? } ? ? ? ? ? ? ? this.loading = false ? ? ? ? ? ? ? resolve() ? ? ? ? ? ? } ? ? ? ? ? }) ? ? ? ? ? .catch(error => { ? ? ? ? ? ? reject(error) ? ? ? ? ? }) ? ? ? }) ? ? },? ? ? onLoad() { ? ? ? this.getHistory() ? ? }, ? ? onRefresh() { ? ? ? this.page = 1 ? ? ? setTimeout(() => { ? ? ? ? this.getHistory() ? ? ? ? Toast('刷新成功') ? ? ? ? this.isLoading = false ? ? ? }, 1000) ? ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vant的Loading加載動(dòng)畫(huà)組件的使用(通過(guò)接口拿數(shù)據(jù)時(shí)顯示加載狀態(tài))
- vant-list組件觸發(fā)多次onload事件導(dǎo)致數(shù)據(jù)亂序的解決方案
- Vue3+vant+ts 上滑加載解決上滑調(diào)用多次數(shù)據(jù)的問(wèn)題(推薦)
- vant的Uploader?文件上傳,圖片數(shù)據(jù)回顯問(wèn)題
- vant遇到van-sidebar數(shù)據(jù)超出不能滑動(dòng)的問(wèn)題
- vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式
相關(guān)文章
vue動(dòng)態(tài)綁定v-model屬性名方式
這篇文章主要介紹了vue動(dòng)態(tài)綁定v-model屬性名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08基于Vue-cli的一套代碼支持多個(gè)項(xiàng)目
這篇文章主要介紹了基于Vue-cli的一套代碼支持多個(gè)項(xiàng)目的方案,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-03-03vue element 關(guān)閉當(dāng)前tab 跳轉(zhuǎn)到上一路由操作
這篇文章主要介紹了vue element 關(guān)閉當(dāng)前tab 跳轉(zhuǎn)到上一路由操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue-cli3單頁(yè)構(gòu)建大型項(xiàng)目方案
這篇文章主要介紹了vue-cli3單頁(yè)構(gòu)建大型項(xiàng)目方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04