uniapp简版滚动加载以及区域滚动加载

52 篇文章 0 订阅

一、滚动加载

    <order-item v-for="(item,index) in orderList" :item="item" :isHost="isHost" :key="index"></order-item>
    <view @tap="reload" class="loading-button" v-if="orderList.length == 0">  暂无订单~</view>

                pageIndex: 1,
                pageSize: 3,
                totalPage: 0,
                pages:{},

onReachBottom() {
            const {
                pageCount
            } = this.pages
            // 加载更多
            if (this.pageIndex < pageCount) {
                this.loading = true
                this.pageIndex = this.pageIndex + 1
                this.loadData()
            }
        },
methods:{    
// 加载数据
            loadData() {
                if (this.initLoading) {
                    uni.showLoading()
                }
                const {
                    pageIndex,
                    pageSize
                } = this
             
                    MemberOrderList({
                        pageIndex,
                        pageSize,
                        filter_fieldtwo: number
                    }).then(res => {
                        this.initLoading = false
                        const {
                            data,
                            ...pages
                        } = res.response

                        this.pages = pages // 记录本次请求的分页参数
                        this.loading = false
                        this.loadingFail = false
                        if(res.response!=null){
                            this.totalPage = res.response.pageCount
                            this.handlePageData(data)

                        }
                    }).catch(err => {
                        uni.hideLoading()
                        this.loading = false
                        this.loadingFail = true
                    })



            // 处理分页数据
            handlePageData(data) {
                const {
                    page = 0, pageSize = 3
                } = this.pages
                // 比较上一次获取的页码和当前获取数据的页码判断当前需要更新哪部分数据
                if (page >= this.pageIndex) {
                    this.orderList = this.orderList.slice(0, (this.pageIndex - 1) * 3).concat(data)
                } else {
                    this.orderList = this.orderList.concat(data)
                }

            },


      // 加载失败后重新加载
            reload() {
                this.loading = true
                this.loadingFail = false
                this.loadData()
            } ,

二、区域滚动加载

    <scroll-view scroll-y="true" @scrolltolower="lower()" style="height: 40vh;overflow: auto;">
            <view
                v-for="item in carList"
                :key="item.id" 
                class="car-list-item dis-flex justify-between align-center"
                @tap="chooseCar(item.id)">
                ...
            </view>
        </scroll-view> 


                carpageIndex: 1,
                carpageSize: 3,
                cartotalPage: 0,
                carpages:{},

methods:{    
            lower(e){
                const {
                    pageCount
                } = this.carpages
                // 加载更多
                if (this.carpageIndex < pageCount) {
                    this.carpageIndex = this.carpageIndex + 1
                  
 this.loadCarList()
                }
                console.log(this.carpageIndex,this.carpages)
            },


            loadCarList() {
                  uni.showLoading()    
                  const {
                        carpageIndex,
                        carpageSize
                    } = this
                    listCarInformationByPage({pageIndex: carpageIndex, pageSize: carpageSize, filter_fieldone: this.userId}).then(res => {
                      uni.hideLoading()
                        const {
                            data,
                            ...pages
                        } = res.response
                        let list = data.map(car => {
                          return {
                            ...car,
                            colourWord: (this.colorList.find(v => v.text == car.colour) || {}).color || '',
                                currentCar: (car.car_number==car_number?true:false)
                          }
                        })
                        this.carpages = pages // 记录本次请求的分页参数
                        this.loading = false
                        this.loadingFail = false
                        this.cartotalPage = res.response.pageCount
                        this.CarPageData(list)
                    }).catch(err => {
                      uni.hideLoading()
                    })
                }
           }

 

 


    // 处理分页数据
            CarPageData(data) {
                const {
                    page = 0, pageSize = 3
                } = this.carpages
                // 比较上一次获取的页码和当前获取数据的页码判断当前需要更新哪部分数据
                if (page >= this.carpageIndex) {
                    this.carList = this.carList.slice(0, (this.carpageIndex - 1) * 3).concat(data)
                } else {
                    this.carList = this.carList.concat(data)
                }
            },

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的uniapp列表滚动加载的实现: 1. 在template中定义列表和加载更多的按钮: ``` <template> <view> <scroll-view scroll-y="true" @scrolltolower="loadMore"> <view v-for="(item, index) in list" :key="index">{{item}}</view> </scroll-view> <button v-if="showLoadMoreBtn" @click="loadMore">加载更多</button> </view> </template> ``` 2. 在script中定义列表数据和是否显示加载更多按钮的变量: ``` <script> export default { data() { return { list: [], // 列表数据 showLoadMoreBtn: false, // 是否显示加载更多按钮 pageNum: 1, // 当前页码 pageSize: 10, // 每页数量 } }, methods: { // 加载更多 loadMore() { // 显示加载更多按钮 this.showLoadMoreBtn = true; // 发送请求获取数据 this.getData().then(res => { // 添加数据到列表 this.list = this.list.concat(res.data); // 隐藏加载更多按钮 this.showLoadMoreBtn = false; // 页码加1 this.pageNum++; }); }, // 发送请求获取数据 getData() { return uni.request({ url: 'your_api_url', data: { pageNum: this.pageNum, pageSize: this.pageSize, }, }); }, }, } </script> ``` 3. 在样式中设置scroll-view的高度,使其可以滚动: ``` <style> scroll-view { height: 500rpx; } </style> ``` 以上就是一个简单的uniapp列表滚动加载的实现。其中,loadMore方法会在滚动到页面底部或点击加载更多按钮时触发,发送请求获取数据并将数据添加到列表中。可以根据实际需求来调整pageSize等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值