当前位置:   article > 正文

uni-app tab切换和下拉刷新,上拉加载更多实现;_uniapp 多tab切换 下拉刷新 加载更多

uniapp 多tab切换 下拉刷新 加载更多

使用uni-app 组件实现下拉刷新,上拉加载更多的功能效果:

代码实现如下:

  1. <template>
  2. <view>
  3. <!-- 搜索导航跳转 -->
  4. <view class="btn-area">
  5. <navigator open-type="navigate" animation-type="pop-in" animation-duration="300" url="./test/HMsearch">
  6. <view class="uni-form-item uni-column"><icon class="iconsearc" type="search" size="18"/> 搜索</view>
  7. </navigator>
  8. </view>
  9. <!-- 加载的内容 -->
  10. <wuc-tab :tab-list="tabList" :tabCur.sync="TabCur" :textFlex.sync="textFlex" tab-class="text-center text-black bg-white" select-class="text-orange text-xl" @change="tabChange">
  11. </wuc-tab>
  12. <div class="cu-bar bg-white solid-bottom">
  13. <div class="action">
  14. <text class="cuIcon-titles text-orange"></text>
  15. <div class="bg-white padding margin text-center text-block">
  16. <!-- 分页的数据列表 -->
  17. <view>
  18. <view class="list" v-for="(item,index) in newsList" :key="index">
  19. <view class="list-left">
  20. <image :src="item.img"></image>
  21. </view>
  22. <view class="list-right">
  23. <view class="list-top">
  24. <text class="elli">{{item.title}}</text>
  25. <text>{{item.public_time}}</text>
  26. </view>
  27. <view class="list-con elli3">{{item.content}}</view>
  28. </view>
  29. </view>
  30. <text class="loading-text">
  31. {{loadingType === 0 ? contentText.contentdown : (loadingType === 1 ? contentText.contentrefresh : contentText.contentnomore)}}</text>
  32. </view>
  33. </div>
  34. </div>
  35. </div>
  36. </view>
  37. </template>
  38. <script>
  39. import uniDrawer from "@/components/uni-drawer/uni-drawer"
  40. import WucTab from '@/components/wuc-tab/wuc-tab.vue'
  41. import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
  42. var _self,page = 1;
  43. export default {
  44. components: {uniDrawer,WucTab,uniLoadMore},
  45. data() {
  46. return {
  47. title: 'navigator',
  48. TabCur: 0,
  49. textFlex:true,//是否平分
  50. tabList: [{ name: '精选' }, { name: '订阅' }],
  51. newsList:[],
  52. loadingType: 0,
  53. contentText: {
  54. pullRefreshStatus:false,//下拉刷新
  55. contentdown: "上拉显示更多",
  56. contentrefresh: "正在加载...",
  57. contentnomore: "没有更多数据了"
  58. }
  59. }
  60. },
  61. onLoad: function (options) {
  62. _self = this;
  63. this.getNewsList();
  64. },
  65. // 下拉刷新
  66. onPullDownRefresh:function () {
  67. _self = this;
  68. uni.startPullDownRefresh();//得到数据后停止下拉刷新
  69. _self.getNewsList();
  70. },
  71. // 上拉加载
  72. onReachBottom: function() {
  73. //console.log(_self.newsList.length);
  74. if (_self.loadingType != 0) {//loadingType!=0;直接返回
  75. return false;
  76. }
  77. _self.loadingType = 1;
  78. uni.showNavigationBarLoading();//显示加载动画
  79. uni.request({
  80. url:'/api/items?page='+page,
  81. success: function(res) {
  82. if (_self.newsList.length==res.data.pages_count) {//没有数据
  83. _self.loadingType = 2;
  84. uni.hideNavigationBarLoading();//关闭加载动画
  85. return false;
  86. }
  87. page++;//每触底一次
  88. for(var i=_self.newsList.length;i<res.data.pages_count;i++){
  89. _self.newsList = _self.newsList.concat(res.data.data[i]);//将数据拼接在一起
  90. }
  91. _self.loadingType = 0;//将loadingType归0重置
  92. uni.hideNavigationBarLoading();//关闭加载动画
  93. }
  94. });
  95. },
  96. methods: {
  97. tabChange(index) {
  98. this.TabCur = index;
  99. },
  100. getNewsList: function() {//第一次回去数据
  101. _self.loadingType = 0;
  102. uni.showNavigationBarLoading();
  103. uni.request({
  104. url: '/api/items?page=1',
  105. success: function(res) {
  106. let newsList =[];
  107. for(var i=0;i<6;i++){
  108. newsList.push(res.data.data[i]);
  109. }
  110. _self.contentText.pullRefreshStatus=false;
  111. _self.newsList = newsList;
  112. if(res.data.pages_count==res.data.data.length){
  113. uni.showToast({
  114. title: '已是最新',
  115. duration: 2000
  116. });
  117. }
  118. uni.hideNavigationBarLoading();
  119. uni.stopPullDownRefresh();//得到数据后停止下拉刷新
  120. }
  121. });
  122. }
  123. }
  124. };
  125. </script>
  126. <style lang="scss">
  127. .uni-column{
  128. margin-top: 15px;
  129. margin-bottom: 15px;
  130. padding-left: 20px;
  131. padding-right: 10px;
  132. padding-top: 10px;
  133. padding-bottom: 10px;
  134. border-radius: 25px;
  135. background-color: #eee;
  136. color:#333;
  137. font-size: 14px;
  138. text-align: center;
  139. }
  140. .uni-column .iconsearc{
  141. margin-right: 10px;
  142. vertical-align: middle;
  143. }
  144. .text-orange,.active{
  145. color:#ff6c00;
  146. }
  147. .elli{
  148. overflow:hidden;
  149. text-overflow:ellipsis;
  150. white-space:nowrap;
  151. }
  152. .elli2{
  153. overflow: hidden;
  154. text-overflow: ellipsis;
  155. display: -webkit-box;
  156. -webkit-line-clamp: 2;
  157. -webkit-box-orient: vertical;
  158. }
  159. .elli3{
  160. overflow: hidden;
  161. text-overflow: ellipsis;
  162. display: -webkit-box;
  163. -webkit-line-clamp: 3;
  164. -webkit-box-orient: vertical;
  165. }
  166. .list{
  167. width: 94%;
  168. display: flex;
  169. flex-direction: row;
  170. padding: 3%;
  171. border-bottom: 1upx solid #ebe8e8;
  172. .list-left{
  173. width: 35%;
  174. margin-right: 3%;
  175. image{
  176. width: 220upx;
  177. height: 140upx;
  178. padding: 3%;
  179. border: 1upx solid #ebe8e8;
  180. box-sizing: border-box;
  181. }
  182. }
  183. .list-right{
  184. width: 65%;
  185. color: #999999;
  186. font-size: 24upx;
  187. .list-top{
  188. display: flex;
  189. flex-direction: row;
  190. justify-content: space-between;
  191. padding-bottom: 5upx;
  192. text{
  193. &:nth-child(1){
  194. color: #333;
  195. font-size: 30upx;
  196. width: 65%;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. .loading-text{
  203. height: 80upx;
  204. line-height: 80upx;
  205. font-size: 26upx;
  206. color:#ccc;
  207. display: flex;
  208. flex-direction: row;
  209. justify-content: space-around;
  210. }
  211. </style>

注:以上内容仅供项目参考!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/221437
推荐阅读
相关标签