百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

uniapp高性能下拉刷新下拉加载使用mescroll-uni-vue2快速实现

suiw9 2024-11-24 21:39 15 浏览 0 评论

前情提示

vue2

hubilderx4.29最新版本

mescroll-uni-1.3.8目前最新版本

使用mescroll-body支持原生组件,性能更好,非常容易接入,官方也提供了很多demo示例。

快速安装

使用uni_modules直接下载导入。不需要配置其他任何地方,不需要配置不必配置pages.json。

业务接口示例

返回要有总页码、或者总条数,这里用的总条数

{
    "success": true,
    "message": "",
    "code": 200,
    "result": {
        "records": [
            {
                "id": "1849444662765731841",
                "fatteningPigNo": "333",
                "filingDate": null,
                "plantName": null,
                "buildingName": null,
                "fieldName": null,
                "frailWeight": null,
                "whetherManual": null,
                "responsiblePersonnel": null,
                "createTime": "2024-10-24 21:35:43",
                "createBy": "admin",
                "updateTime": null,
                "updateBy": null,
                "delFlag": 0,
                "sysOrgCode": "A01",
                "tenantId": null
            },
            {
                "id": "1849450786642509825",
                "fatteningPigNo": "请问请问",
                "filingDate": null,
                "plantName": null,
                "buildingName": null,
                "fieldName": null,
                "frailWeight": null,
                "whetherManual": null,
                "responsiblePersonnel": null,
                "createTime": "2024-10-24 22:00:03",
                "createBy": "admin",
                "updateTime": null,
                "updateBy": null,
                "delFlag": 0,
                "sysOrgCode": "A01",
                "tenantId": null
            }
        ],
        "total": 2,
        "size": 10,
        "current": 1,
        "orders": [

        ],
        "optimizeCountSql": true,
        "searchCount": true,
        "maxLimit": null,
        "countId": null,
        "pages": 1
    },
    "timestamp": 1729778822564
}

效果图

业务界面代码

如果你引入了uview,基本可以完全复制使用

<template>
  <view>
    <!--标题和返回-->
    <cu-custom :bgColor="NavBarColor" isBack>
      <block slot="backText">返回</block>
      <block slot="content">育肥猪信息</block>
    </cu-custom>
    <view class="search">
      <u-search v-model="keywords" @custom="search" @search="search"></u-search>
    </view>

    <!--滚动加载列表-->
    <view class="btn-plus" @click="navTo('pdtjPigYfInfoForm')">
      <u-icon name="plus-circle-fill" size="90" color="#3d87ff"></u-icon>
    </view>
    <mescroll-body ref="mescrollRef" top="105rpx" @init="mescrollInit" :up="upOption" :down="downOption"
                   @down="downCallback" @up="upCallback">
      <u-cell-group class="list" :border="false">
        <u-swipe-action :options="options2" v-for="(item, index) in list" :key="item.id" :index="index"
                        @click="optionsClick">
          <u-cell-item :arrow="true" @click="navTo('pdtjPigYfInfoForm?id='+item.id)">
            <text slot="title">{{item.name || item.id}}</text>
            <text slot="label">创建者:{{item.createBy}}  |  时间:{{item.createTime}}</text>
          </u-cell-item>
        </u-swipe-action>
      </u-cell-group>

    </mescroll-body>

  </view>
</template>

<script>
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import Mixin from "@/common/mixin/Mixin.js";

export default {
  mixins: [MescrollMixin, Mixin],
  data() {
    return {
      keywords: '',
      CustomBar: this.CustomBar,
      NavBarColor: this.NavBarColor,
      purl: "/pball/pdtjPigYfInfo/list",
      options2: [{
        text: '收藏',
        style: {
          backgroundColor: '#3c9cff'
        }
      }, {
        text: '删除',
        style: {
          backgroundColor: '#f56c6c'
        }
      }]
    };
  },
  methods: {
    goHome() {
      this.$Router.push({
        name: "index"
      })
    },
    search(value) {
      console.log('search', value)
    },
    optionsClick(rowIndex, btnIndex) {

    },
    navTo(url) {
      uni.navigateTo({
        url: url
      });
    }
  }
}
</script>
<style lang="scss">
</style>

@/common/mixin/Mixin.js主要用了请求业务接口,和配置mescroll的options。

/**
 * Copyright (c) 2013-Now http://pusdn.com All rights reserved.
 * Author: PGZ Club
 */
const ListMixin = {
	data() {
		return {
			downOption: {
				auto: false, //是否在初始化完毕之后自动执行下拉回调callback; 默认true
			},
			upOption: {
				page: {
					num: 0,
					size: 10,
					time: null
				},
				noMoreSize: 1, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty: {
					tip: '~ 暂无更多数据 ~', // 提示
					// btnText: '返回首页'
				},
				textNoMore: '-- 暂无更多数据 --',
				toTop: {
					src: null,
					offset: 1000,
					duration: 300,
					zIndex: 9990,
					right: "50rpx",
					bottom: "165rpx",
					safearea: false,
					width: "40px",
					radius: "50%",
					left: null
				}
			},
			queryParam: {
				pageNo: 1,
				pageSize: 10
			},
			list: [],
			pageNo: 1,
			pageSize: 10,
		}
	},
	onShow() {
		this.canReset && this.mescroll.resetUpScroll()
		this.canReset && this.mescroll.scrollTo(0, 0)
		this.canReset = true
	},
	methods: {
		/*下拉刷新的回调 */
		downCallback() {
			//加载列表数据
			this.loadList('down');
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			let param = this.queryParam
			param.pageNo = page.num,
				param.pageSize = page.size

			if (page.num == 1) {
				this.list = [];
			}
			console.log("upCallback==param::", param)
			this.$http.get(this.purl, {
				params: param
			}).then(res => {
				console.log("upCallback请求返回res", res)
				if (res.data.success) {
					let rec = res.data.result.records;
					let totalSize = res.data.result.total;
					let hasNext = true;
					if (!rec || rec.length < this.pageSize) {
						console.log("加载完成!没有更多了")
						hasNext = false;
					}
					console.log("hasNext", hasNext)
					//方法四 (不推荐),会存在一个小问题:比如列表共有20条数据,每页加载10条,共2页.如果只根据当前页的数据个数判断,则需翻到第三页才会知道无更多数据.
					// this.mescroll.endSuccess(rec.length);
					this.mescroll.endBySize(rec.length, totalSize);
					//设置列表数据
					this.list = this.list.concat(rec);
					this.$forceUpdate();
				} else {
					this.mescroll.endErr();
				}
			}).catch(() => {
				//加载失败, 结束
				this.mescroll.endErr();
			})
		},
		loadList(flag) {
			let param = this.queryParam
			param.pageNo = this.pageNo,
				param.pageSize = this.pageSize
			console.log("请求参数", param)
			if (flag == 'down') {
				if (this.mescroll.optUp.use) {
					this.mescroll.resetUpScroll()
				} else {
					setTimeout(() => {
						this.mescroll.endSuccess();
					}, 500)
				}
			}
			return false;
			this.$http.get(this.purl, {
				params: param
			}).then(res => {
				if (res.data.success) {
					console.log("请求返回res.data", res.data)
					let rec = res.data.result.records
					if (flag == 'down') {
						//下拉刷新成功的回调,隐藏下拉刷新的状态
						// this.mescroll.endSuccess();
						// if(this.mescroll.optUp.use){
						// 	this.mescroll.resetUpScroll()
						// }else{
						// 	setTimeout(()=>{
						// 		this.mescroll.endSuccess();
						// 	}, 500)
						// }
					}
					//添加新数据
					this.list = rec;
					/* if(!rec || rec.length<this.pageSize){
					  console.log("加载完成!")
					} */
				} else {
					console.log("请求返回else", res)
					this.mescroll.endErr();
				}
			}).catch((err) => {
				console.log("请求返回err", err)
				//加载失败, 结束
				this.mescroll.endErr();
			})
		},
	}

}

export default ListMixin;

相关推荐

俄罗斯的 HTTPS 也要被废了?(俄罗斯网站关闭)

发布该推文的ScottHelme是一名黑客,SecurityHeaders和ReportUri的创始人、Pluralsight作者、BBC常驻黑客。他表示,CAs现在似乎正在停止为俄罗斯域名颁发...

如何强制所有流量使用 HTTPS一网上用户

如何强制所有流量使用HTTPS一网上用户使用.htaccess强制流量到https的最常见方法可能是使用.htaccess重定向请求。.htaccess是一个简单的文本文件,简称为“.h...

https和http的区别(https和http有何区别)

“HTTPS和HTTP都是数据传输的应用层协议,区别在于HTTPS比HTTP安全”。区别在哪里,我们接着往下看:...

快码住!带你十分钟搞懂HTTP与HTTPS协议及请求的区别

什么是协议?网络协议是计算机之间为了实现网络通信从而达成的一种“约定”或“规则”,正是因为这个“规则”的存在,不同厂商的生产设备、及不同操作系统组成的计算机之间,才可以实现通信。简单来说,计算机与网络...

简述HTTPS工作原理(简述https原理,以及与http的区别)

https是在http协议的基础上加了一层SSL(由网景公司开发),加密由ssl实现,它的目的是为用户提供对网站服务器的身份认证(需要CA),以至于保护交换数据的隐私和完整性,原理如图示。1、客户端发...

21、HTTPS 有几次握手和挥手?HTTPS 的原理什么是(高薪 常问)

HTTPS是3次握手和4次挥手,和HTTP是一样的。HTTPS的原理...

一次安全可靠的通信——HTTPS原理

为什么HTTPS协议就比HTTP安全呢?一次安全可靠的通信应该包含什么东西呢,这篇文章我会尝试讲清楚这些细节。Alice与Bob的通信...

为什么有的网站没有使用https(为什么有的网站点不开)

有的网站没有使用HTTPS的原因可能涉及多个方面,以下是.com、.top域名的一些见解:服务器性能限制:HTTPS使用公钥加密和私钥解密技术,这要求服务器具备足够的计算能力来处理加解密操作。如果服务...

HTTPS是什么?加密原理和证书。SSL/TLS握手过程

秘钥的产生过程非对称加密...

图解HTTPS「转」(图解http 完整版 彩色版 pdf)

我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取。所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议。...

HTTP 和 HTTPS 有何不同?一文带你全面了解

随着互联网时代的高速发展,Web服务器和客户端之间的安全通信需求也越来越高。HTTP和HTTPS是两种广泛使用的Web通信协议。本文将介绍HTTP和HTTPS的区别,并探讨为什么HTTPS已成为We...

HTTP与HTTPS的区别,详细介绍(http与https有什么区别)

HTTP与HTTPS介绍超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的...

一文让你轻松掌握 HTTPS(https详解)

一文让你轻松掌握HTTPS原文作者:UC国际研发泽原写在最前:欢迎你来到“UC国际技术”公众号,我们将为大家提供与客户端、服务端、算法、测试、数据、前端等相关的高质量技术文章,不限于原创与翻译。...

如何在Spring Boot应用程序上启用HTTPS?

HTTPS是HTTP的安全版本,旨在提供传输层安全性(TLS)[安全套接字层(SSL)的后继产品],这是地址栏中的挂锁图标,用于在Web服务器和浏览器之间建立加密连接。HTTPS加密每个数据包以安全方...

一文彻底搞明白Http以及Https(http0)

早期以信息发布为主的Web1.0时代,HTTP已可以满足绝大部分需要。证书费用、服务器的计算资源都比较昂贵,作为HTTP安全扩展的HTTPS,通常只应用在登录、交易等少数环境中。但随着越来越多的重要...

取消回复欢迎 发表评论: