微信小程序-下拉滚动加载数据

1.实现流程图

所谓工欲善其事,必先利其器,做程序之前咱们把思路整理一下 ,画流程图是比较好的方式

2.前端JS 和 WXML

视图绑定 scrollToLower 方法 ,当离底部还有100时候触发;

WXML:

复制代码
<scroll-view scroll-y lower-threshold="100" bindscrolltolower="scrollToLower" style="height: 100vh;">
    <view class="items" wx:for="{{list}}" wx:key="{{item.id}}">
        <image src="{{item.img}}"/>
        <view class="datas">
            <view class="beauty">订单金额:{{item.price}}元</view>
            <view class="times">2022-09-30</view>
        </view>
    </view>
</scroll-view>
<view class='text-style'>{{text}}</view>

datalock 等于1 时, 不发出网络请求, 已免不必要的网络开销;

数据返回时 使用 concat 拼接

JS:

html 复制代码
    data: {
        list:[],
        page:1,
        text:'',
        datalock:0
    },
    onLoad: function (options) {
        this.getDatas();
    },
    scrollToLower:function(){
        if(this.data.datalock==1){
            return false;
        }else{
            wx.showToast({
                title: '加载中...',
                icon:"loading",
                duration:600
            });
            this.getDatas();
        }  
    },getDatas:function(){
        var that = this;
        wx.request({
            url:'https://www.test.com/loaddata',
            method:"GET",
            data:{
                token:wx.getStorageSync('userid'),
                page:that.data.page,
            },
            success:function(res){
                //console.log(res.data);
                if(res.data.data.length > 0 ){
                    let newlist = that.data.list.concat(res.data.data); //拼接数据
                    for(let i=0;i<newlist.length;i++){
                        newlist[i].jsonformat =  JSON.parse(newlist[i].json);
                        newlist[i].timeformat =  tools.formatTime(newlist[i].addtime,'Y-M-D h:m');
                    }
                    that.setData({
                        list : newlist,
                        page : that.data.page +1,
                        text: '加载更多'
                    });
                    //console.log(that.data.list);
                }else{
                    that.setData({
                        text: '到底啦!',
                        datalock:1
                    });
                }
                
            },fail:function(res){
                console.log(res);
                
            }
        });
    }
复制代码
    

3.后端代码

这里使用tp6.1 来做接口通讯

php 复制代码
use think\Request;
use think\Db;
use think\Config;
...
public function loaddata(Request $request){
	$token = trim($request->param('token'));
	$page = intval($request->param('page'));
	$pageNum = 5;
	if(!$page){
		$page=1;
	}
	$res_data = Db::name('usercolor')->where("uidtoken='".$token."'")->order('id desc')->limit(($page-1)*$pageNum,$pageNum)->select();
	if($res_data){
		make_jsons($res_data);
		exit(); 
	}else{
		make_jsons([],400,'error');
		exit();
	}
}
...

顺带附上make_jsons方法:

php 复制代码
function make_jsons($dataArr=[],$code=200,$message='suc'){

	$codedata = [  
		'status' => $code,  
		'message' => $message,  
		'data' =>$dataArr,  
	];  
	echo  json_encode($codedata);
	exit();
	
}
相关推荐
疯狂小猫咪7 分钟前
中小培训机构教务数字化踩坑复盘|从 Excel 过渡到教务系统经验总结
小程序·excel
wxwx_bscxy3225 小时前
基于Python新冠疫情可视化分析系统的设计与实现
java·数据库·spring boot·python·微信小程序·sqlite
www_aiyuanma_vip11 小时前
信息安全小卫士
小程序
www_aiyuanma_vip15 小时前
家乡介绍小程序
小程序·notepad++
www_aiyuanma_vip15 小时前
信息安全小卫士(深色系版本)
小程序
博、、17 小时前
智慧场馆小程序系统实战指南:从架构设计到落地部署
小程序
冠品科技-小程序开发17 小时前
做实体的,盲盒玩法别只停留在线下
小程序·软件构建·个人开发·软件需求
2501_9151063218 小时前
iOS数据采集技术详解:从性能监控到崩溃分析的全链路实践
android·ios·小程序·https·uni-app·iphone·webview
黑马程序员毕设1 天前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
数字游民95271 天前
史诗级更新:微信小程序支持个人主体接入虚拟支付
ai·微信小程序·小程序·opc·数字游民9527