小程序使用scroll-view实现分页功能

wxml

<!-- pages/applyRecord/applyRecord.wxml -->
<view class="container">
    <scroll-view
        scroll-y
        bindscrolltolower="onScrollToLower"
        style="height: 100vh; -webkit-overflow-scrolling: touch;" 
        lower-threshold="50"
        scroll-with-animation
    >
        <view class="list">
            <block wx:for="{{list}}" wx:key="id">
                <view class="list-item">
                    <view class="list-item__header">{{item.showDate}}</view>
                    <view class="list-item__content">
                        <view class="list-item__row">
                            <text class="list-item__row-title">用餐人姓名</text>
                            <text>{{item.empName || '-'}}</text>
                        </view>
                        <view class="list-item__row">
                            <text class="list-item__row-title">餐厅名称</text>
                            <text>{{item.cantName || '-'}}</text>
                        </view>
                        <view class="list-item__row">
                            <text class="list-item__row-title">餐别</text>
                            <text>{{item.mealName || '-'}}</text>
                        </view>
                        <view class="list-item__row">
                            <text class="list-item__row-title">套餐</text>
                            <text>{{item.mealSetName || '-'}}</text>
                        </view>
                        <view class="list-item__row">
                            <text class="list-item__row-title">价格()</text>
                            <text>{{(item.famount || 0) / 100}}</text>
                        </view>
                    </view>
                </view>
            </block>
            <view wx:if="{{loading}}" class="loading">加载中...</view>
            <view wx:if="{{finished}}" class="finished">没有更多了</view>
        </view>
    </scroll-view>
</view>

js

const { getOrders: getList } = require('../../api/meal');
const moment = require('../../utils/moment');

Page({
    data: {
        condition: {
            payStatus: 1, // 0为待支付,1为已支付,目前固定传1
        },
        queryForm: {
            page: 1, // 分页
            pageSize: 10,
        },
        loading: false, // 是否处在加载状态
        finished: false, // 是否已加载完成
        error: false, // 是否加载失败
        list: [], // 列表
        total: 0, // 数据总条数
    },

    onLoad() {
        console.log('userInfo', getApp().globalData.userInfo);
        this.loadList();
    },

    tranDate(dateStr) {
        if (!dateStr) return '-';
        return moment(dateStr, "YYYYMMDD").format("YYYY年MM月DD日");
    },

    loadList() {
        if (this.data.loading || this.data.finished) return;

        const params = Object.assign({}, this.data.queryForm, {
            condition: JSON.stringify(this.data.condition),
        });
        this.setData({ loading: true });
        getList(params).then((data) => {
            const { res } = data;
            const list = res.list.map((ele)=>{
                ele.showDate = this.tranDate(ele.mealDate)
                return ele 
            }) || [];
            this.setData({
                list: this.data.list.concat(list),
                finished: res.pages == this.data.queryForm.page || res.pages == 0,
                queryForm: { ...this.data.queryForm, page: this.data.queryForm.page + 1 }
            });
        }).finally(() => {
            this.setData({ loading: false });
        });
    },

    onScrollToLower() {
        this.loadList();
    },

    onRefresh() {
        this.setData({
            finished: false, // 清空列表数据
            queryForm: { ...this.data.queryForm, page: 1 }, // 分页数赋值为1
            list: [] // 清空数组
        });
        this.loadList(); // 重新加载数据
    }
});

wxss

.container {
    padding: 0 16px;
}

.list-item {
    background-color: #fff;
    border-radius: 12px;
    margin: 12px 0 0 0;
    padding: 12px 16px;
}

.list-item:last-child {
    margin: 0;
}

.list-item__header {
    font-size: 14px;
    margin: 4px 0 8px;
    color: #333;
    font-weight: bold;
}

.list-item__content {
    font-size: 12px;
}

.list-item__row {
    display: flex;
    justify-content: space-between;
    color: #555;
    line-height: 20px;
}

.list-item__row-title {
    min-width: 100px;
    font-weight: bold;
}

.list-item__footer {
    text-align: right;
}

.loading {
    text-align: center;
    padding: 20px 0;
    color: #999;
}

.finished {
    text-align: center;
    padding: 20px 0;
    color: #999;
}
是的,可以使用swiper和scroll-view组件来实现微信小程序的左右分页功能。 首先,在wxml文件中添加swiper和scroll-view组件,并设置相应的属性。 ```html <swiper class="swiper-container" current="{{current}}" duration="{{500}}" style="height:{{pageHeight}}px"> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第一页内容--> </scroll-view> </swiper-item> <swiper-item> <scroll-view class="scroll-view" scroll-x="{{true}}" scroll-y="{{false}}" style="width:{{windowWidth}}px;height:{{pageHeight}}px;"> <!--第二页内容--> </scroll-view> </swiper-item> <!--更多swiper-item--> </swiper> ``` 其中,swiper组件设置了current属性,用于控制当前显示的页码;scroll-view组件设置了scroll-x属性,用于开启水平方向的滚动,并设置了样式宽度为屏幕宽度,高度为页面高度。 接着,在js文件中,监听swiper组件的change事件,用于动态更新当前页码。 ```javascript Page({ data: { current: 0, windowWidth: wx.getSystemInfoSync().windowWidth, pageHeight: wx.getSystemInfoSync().windowHeight }, onLoad: function () { //... }, onPageChange: function (e) { this.setData({ current: e.detail.current }) } }) ``` 最后,在样式文件中,设置swiper-container和scroll-view的样式,用于实现左右分页效果。 ```css .swiper-container { overflow: hidden; position: relative; } .scroll-view { white-space: nowrap; } .swiper-item { display: inline-block; vertical-align: top; width: 100%; } ``` 以上就是使用swiper和scroll-view组件实现微信小程序左右分页功能的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值