vue前端分页代码实现

本文介绍了如何在Vue应用中实现页面分页功能,包括使用HTML构建页面布局,通过axios进行API调用获取数据,并展示首尾页、上一页、下一页的控制。重点讲解了`findAll`方法和`toPage`方法的实现以及数据更新的处理。
摘要由CSDN通过智能技术生成

Html:

<div class="pageNum">
    <button @click="firstPage">首页</button>
    <button @click="backPage">上一页</button>
    <ul>
       <li :class="{active:page===currentPage}" v-for="page in totalPage" @click="toPage(page)">{{page}}
       /li>
     </ul>
     <button @click="forwardPage">下一页</button>
     <button @click="lastPage">尾页</button>
</div>

vue代码:

<script>
    new Vue({
        el: "#pageNum",
        data: {
            //分页参数

            currentPage: 1, //当前页
            pageSize: 6,  //页容量
            totalPage: '',//总页数
            totalCount: '',//总记录数
            courseList: '',//接收后台发送的数据容器
        },
        methods: {
            //分页_搜索
            findAll() {
                var url = "http://localhost:8080/edu/courses";
                axios.get(url, {
                    params: {
                        method: "findAll",
                        //分页                   
                        currentPage: this.currentPage,
                        pageSize: this.pageSize,
                    }
                })
                    .then(res => {
                        console.log(res);
                        this.currentPage = res.data.obj.currentPage
                        this.pageSize = res.data.obj.pageSize
                        this.totalCount = res.data.obj.totalCount
                        this.totalPage = res.data.obj.totalPage
                        this.courseList = res.data.obj.data
                    })

            },
            /* 首页 */
            firstPage() {
                this.currentPage = 1;
                this.findAll();
            },
            /*  尾页 */
            lastPage() {
                this.currentPage = this.totalPage;
                this.findAll();
            },
            /* 上一页 */
            backPage() { 
                if (this.currentPage == 1) {
                    return;
                }
                this.currentPage = this.currentPage - 1;
                this.findAll();
            },
            /* 下一页 */
            forwardPage() {
                if (this.currentPage == this.totalPage) {
                   return;
                }
                this.currentPage = this.currentPage + 1;
                this.findAll();
            },
            /* 跳转到xx页 */
            toPage(page){
                this.currentPage=page;
                this.findAll();
            },


        },
        created() {
            this.findAll();
        },

    })
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值