uniapp中使用u-loadmore,loadText內(nèi)容不隨status改變刷新方式
uniapp中使用u-loadmore,loadText內(nèi)容不隨status改變刷新
uniapp中使用u-loadmore,使用情況比較復(fù)雜,出現(xiàn)loadText內(nèi)容不隨status改變刷新的情況,即當(dāng)status="loading"時,顯示的內(nèi)容是loadmore或nomore的文字。
解決辦法
添加key參數(shù)
<u-loadmore :status="loadStatus" :load-text="loadText" :key="3"/>
uni-app上拉加載 使用uni-ui的LoadMore組件
上拉加載
我在代碼里是配合list使用的LoadMore 組件實現(xiàn)下拉加載,貼一個官網(wǎng)組件地址 LoadMore
下拉加載的原理大概是:
- 設(shè)置好每頁展示多少條數(shù)據(jù),加載第一頁。
- 加載完后判斷當(dāng)前狀態(tài),如果數(shù)據(jù)列表的長度=全部數(shù)據(jù)長度,則將狀態(tài)設(shè)置為noMore,否則為more。
- 從第二頁開始,每加載一頁就在數(shù)據(jù)列表中拼接下一頁內(nèi)容。重復(fù)進行(2)直到加載完全部數(shù)據(jù)。
- 當(dāng)數(shù)據(jù)加載完畢后頁碼pageNum不再++。
下拉刷新
使用onPullDownRefresh
- 在 js 中定義 onPullDownRefresh 處理函數(shù)(和onLoad等生命周期函數(shù)同級),監(jiān)聽該頁面用戶下拉刷新事件。
- 需要在 pages.json 里,找到的當(dāng)前頁面的pages節(jié)點,并在 style 選項中開啟 enablePullDownRefresh。
- 當(dāng)處理完數(shù)據(jù)刷新后,uni.stopPullDownRefresh可以停止當(dāng)前頁面的下拉刷新。
//pages.json { "path": "pages/beiliao/beiliao", "style": { "navigationBarTitleText": "備料單", "navigationStyle": "custom", "enablePullDownRefresh": true, "app-plus": { "titleNView": false } } },
具體代碼:
<template> <view style="background-color: #F0F0F0;"> <view class="box" style="padding:10px 10px;margin:70px 10px -10px 10px"> <uni-list style="background-color: #F0F0F0;"> <view v-for="(item,index) in tableList" :key="index"> //list內(nèi)容省略啦~ <uni-list-item showArrow :note="'xxx'" /> </view> <view class="example-body"> <uni-load-more :status="status" :content-text="contentText" @clickLoadMore="clickLoadMore"/> </view> </uni-list> </view> </view> </template>
<script> import util from '../../util/util.js'; import uniPagination from '@/components/uni-pagination/uni-pagination.vue' import uniListItem from "@/components/uni-list-item/uni-list-item.vue" import uniList from "@/components/uni-list/uni-list.vue" import uniSection from "@/components/uni-section/uni-section.vue" import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue" export default { components: { uniPagination, uniListItem, uniList, uniSection, uniLoadMore }, data() { return { total: 0, pageNum: 1, pageSize: 10, tableList: [], status: 'more', contentText: { contentdown: '查看更多', contentrefresh: '加載中', contentnomore: '沒有更多' } } }, onLoad() { this.queryByInput(); }, //上拉加載 onReachBottom(){ if (this.status == 'noMore'){ return; } this.pageNum ++; console.log(this.pageNum) this.getTableList(); }, //下拉刷新 onPullDownRefresh(){ uni.stopPullDownRefresh(); this.tableList = []; this.queryByInput() }, methods: { queryByInput:function(){ this.pageNum = 1; this.getTableList(); }, //條件查詢 getTableList: function() { var that = this; var params = { current: this.pageNum, size: this.pageSize }this.$http.get('/workshop/productionmaterialorder/page', params, { }).then(function(response) { //這里只會在接口是成功狀態(tài)返回 that.total = response.total //第一次加載時,若只有一頁數(shù)據(jù)(這里寫的簡直if語句之王,大家懂的都懂,怪我太菜了!?。? if(that.tableList.length == 0) { that.status = 'more' that.tableList = that.tableList.concat(response.records) if(that.tableList.length == that.total) { that.status = 'noMore' return false; } } else { if(that.tableList.length == that.total) { that.status = 'noMore' return false; } else { that.status = 'more' that.tableList = that.tableList.concat(response.records) } } }).catch(function(error) { //這里只會在接口是失敗狀態(tài)返回,不需要去處理錯誤提示 console.log(error); }); }, //點擊查看更多觸發(fā)事件 clickLoadMore(e) { // console.log('加載更多') } } } </script>
遇到的問題
1.循環(huán)拼接,最后一頁結(jié)束后又開始拼接第一頁,主要是由于沒有限制pageNum,當(dāng)狀態(tài)變成noMore不再進行頁碼的增加即可。
解決辦法:
//上拉加載 onReachBottom(){ //解決上述問題 if (this.status == 'noMore'){ return; } this.pageNum ++; console.log(this.pageNum) this.getTableList(); },
2.新增數(shù)據(jù) ,如果要新增一條列表數(shù)據(jù),我這里進行的操作是從A跳轉(zhuǎn)頁面B輸入新息,新增后回到A頁面,此時需要A重新加載頁面。(加載頁面在onShow中調(diào)用)而如果在從A跳轉(zhuǎn)B時,頁面狀態(tài)可能是處于第三頁,無法保留住此狀態(tài)。
目前想到的解決方法:不刷新A,新增時返回新增數(shù)據(jù)的id,將新增信息添加至原本的列表下即可。
3.修改數(shù)據(jù) ,A跳B修改,修改成功后返回A頁面,存在情況和新增時一樣,可能你在第三頁選中某條數(shù)據(jù)進行修改,如果修改成功后重新加載A頁面就會很麻煩,又要翻下去查看剛才修改的數(shù)據(jù),考慮到這個問題所以我選擇成功后不刷新A頁面。
解決方法:修改時將對應(yīng)數(shù)據(jù)的index傳遞給B頁面,在B修改完數(shù)據(jù)后再把index返回給A,這樣就可以把修改數(shù)據(jù)所在的位置記錄下來。(AB頁面怎么傳參我之前寫過,不再重復(fù)記錄)
//如果返回的數(shù)據(jù)有index,則替換掉該位置修改前數(shù)據(jù),沒有則是新增操作進行刷新 if(this.index) { this.tableList[this.index].xx= this.xx } else { this.tableList = []; this.queryByInput(); }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue預(yù)渲染:prerender-spa-plugin生成靜態(tài)HTML與vue-meta-info更新meta
Vue.js中,prerender-spa-plugin和vue-meta-info插件的結(jié)合使用,提供了解決SEO問題的方案,prerender-spa-plugin通過預(yù)渲染技術(shù)生成靜態(tài)HTML,而vue-meta-info則能動態(tài)管理頁面元數(shù)據(jù),本文將探討如何使用這兩個工具優(yōu)化Vue.js項目的SEO表現(xiàn),包括安裝、配置及注意事項2024-10-10Vue Element前端應(yīng)用開發(fā)之開發(fā)環(huán)境的準(zhǔn)備工作
這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之開發(fā)環(huán)境的準(zhǔn)備工作,對Vue感興趣的同學(xué),可以來學(xué)習(xí)一下2021-05-05