广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue分页查询怎么实现
  • 776
分享到

Vue分页查询怎么实现

Vue分页查询实现Vue分页功能 2023-05-15 08:05:51 776人浏览 泡泡鱼
摘要

我编写了一个简单的前端页面用来查询数据,页面一共有几个逻辑 具体的效果可以看下面的演示 下面就来看一下具体的实现步骤。 首先看一下Vue的代码 <script type="

我编写了一个简单的前端页面用来查询数据,页面一共有几个逻辑

具体的效果可以看下面的演示

下面就来看一下具体的实现步骤。

首先看一下Vue的代码

<script type="text/javascript">
    Vue.createApp({
        data()  {
            return {
                items : [],
                // 关键词
                keyWord : "",
                // 是否没有数据
                isnull : false,
                // 一开始不显示上一页和下一页
                isshow : false,
                // 一共有多少条数据
                countInfo : 0,
                // 每一页显示几条数据
                pageSize : 10,
                // 当前是第几页
                currentPage : 1,
                // 一共有几页
                countAll : 1,
                code : 200
            }
        },
        methods: {
            search() {
                // 拿到待搜索的关键词
                var keyword = document.getElementById("keyword").value;
                console.log(keyword);
                this.keyword = keyword;
                this.currentPage = 1;
                var url = "Http://localhost:8080/csdn/search/" + keyword + "/" + this.currentPage;
                console.log(url);
                axiOS.get(url).then((response) => {
                    if(response.data.msg.count==0) {
                        this.isnull = true;
                        // 将原始数据置空
                        this.items = [];
                        // 不显示上一页下一页按钮
                        this.isshow = false;
                    } else {
                        this.isnull = false;
                        console.log(response)
                        this.items = response.data.msg.list;
                        this.countInfo = response.data.msg.count;
                        // 计算一共有几页
                        this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                        this.isshow = true;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getNextPage() {
                if(this.currentPage == this.countAll) {
                    this.currentPage = this.currentPage;
                } else {
                    this.currentPage = this.currentPage + 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 计算一共有几页
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getPrePage() {
                if(this.currentPage == 1) {
                    this.currentPage = 1;
                } else {
                    this.currentPage = this.currentPage - 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 计算一共有几页
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            }
        },
    }).mount("#app");
</script>

data()中返回了几个变量,

  • items:用来存放待展示的数据项
  • keyword:记录本次查询使用的关键词
  • isnull:表示一次查询的结果数量是否为0,用来控制没有结果的显示逻辑
  • isshow:表示是否显示上一页下一页按钮,以及显示当前页数和数据总数
  • countInfo:记录一共有多少条结果
  • pageSize:记录每页显示的数据项,目前后端固定每页展示10条数据
  • currentPage:记录当前是第几页
  • countAll:记录一共有多少页数据
  • code:后端返回的一个状态码,没什么用

一共提供了三个方法进行查询

  • search():进行一个新的关键词的查询
  • getNextPage():查询下一页的数据,如果已经是最后一页了,则查询当前页的结果
  • getPrePage():查询上一页的数据,如果已经是第一页了,则查询当前页的结果

接着我们再来看一下后端返回的数据格式

上图中方框内的数据就是后端返回的数据,msg中记录的就是我们需要用到的数据,里面有交给数据项

  • count:表示数据总数,只是查询数据总数,并不会将所有的数据都返回给前端
  • list:返回当前页的数据
  • page:表示当前是第几页

我们具体来看一下list中数据项的内容

可以发现list中的每一项就是构成我们前端页面中一行的数据,这在vue中体现为数据的绑定,下面就来看看详细的html代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>纳米搜索</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/CSS/bootstrap.min.css" rel="external nofollow" >
    <script src="https://cdn.staticfile.org/Jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/vue@3"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div class="container">
    <!-- 先编写一个搜索栏 -->
    <div class="row" id="app">
        <div class="col-md-1"></div>
        <div class="col-md-10">
            <!-- 这里面有两个个部分 -->
            <div class="row">
                <!--<div class="col-md-2"></div>-->
                <div class="col-md-12">
                    <div style="float: left; margin-top: 20px;margin-left: 20%">
                        <h2 style="color:cornflowerblue">纳米搜索</h2>
                    </div>
                    <div style="float: left; margin-top: 20px; margin-left: 20px">
                        <div class="fORM-group" style="margin-right: 20px; float: left;" >
                            <div class="input-group" >
                                <input type="text" class="form-control" name="keyword"  id="keyword" placeholder="请输入要搜索的关键词">
                            </div>
                        </div>
                        <div style="float:left">
                            <button id="search" type="button" class="btn btn-primary" v-on:click="search">搜索</button>
                        </div>
                    </div>
                </div>
                <!--<div class="col-md-2"></div>-->
            </div>
            <hr>
            <div>
                <div v-for="item of items">
                    <!-- 第一行是url -->
                    <a :href="item.url" rel="external nofollow"  target="_blank">
                        <div style="color: #0000cc">{{item.title}}</div>
                    </a>
                    <div style="color: #28a745">{{item.url}}</div>
                    <!-- 这一行显示文章作者 -->
                    <div style="color: #000000">文章作者:<span style="color: #000000; margin-left: 10px">{{item.nick_name}}</span></div>
                    <!-- 这一行显示标签 -->
                    <div style="color: #000000">文章标签:<span style="color: #000000; margin-left: 10px">{{item.tag}}</span></div>
                    <!-- 下面一行显示发表时间,阅读数和收藏数 -->
                    <div>
                        <div style="color: #000000">发表时间:<span style="color: #000000;margin-left: 10px">{{item.up_time}}</span></div>
                        <div style="color: #000000;float: left">阅读量:<span style="color: #000000;margin-left: 10px">{{item.read_volum}}</span></div>
                        <div style="color: #000000;float: left; margin-left: 10px">收藏量:<span style="color: #000000;margin-left: 10px">{{item.collection_volum}}</span></div>
                    </div>
                    <br>
                    <hr>
                </div>
            </div>
            <!-- 当没有查询结果的时候显示 -->
            <div v-if="isnull">
                <span>非常抱歉,没有您想要的结果(。・_・。)ノI'm sorry~</span>
            </div>
            <!-- 当有数据的时候显示 -->
            <div v-if="isshow">
                <div style="float:left; margin-right: 20px;" >
                    <button type="button" class="btn btn-primary" v-on:click="getPrePage">上一页</button>
                </div>
                <div style="float:left; margin-right: 20px;" >
                    <button type="button" class="btn btn-primary" v-on:click="getNextPage" >下一页</button>
                </div>
                <div style="float:left; margin-right: 20px; margin-top: 5px;">
                    <span>第{{currentPage}}/{{countAll}}页</spa>
                </div>
                <div style="float:left; margin-right: 20px; margin-top: 5px;">
                    <span>共有{{countInfo}}条数据</spa>
                </div>
            </div>
        </div>
        <div class="col-md-1"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    Vue.createApp({
        data()  {
            return {
                items : [],
                // 关键词
                keyword : "",
                // 是否没有数据
                isnull : false,
                // 一开始不显示上一页和下一页
                isshow : false,
                // 一共有多少条数据
                countInfo : 0,
                // 每一页显示几条数据
                pageSize : 10,
                // 当前是第几页
                currentPage : 1,
                // 一共有几页
                countAll : 1,
                code : 200
            }
        },
        methods: {
            search() {
                // 拿到待搜索的关键词
                var keyword = document.getElementById("keyword").value;
                console.log(keyword);
                this.keyword = keyword;
                this.currentPage = 1;
                var url = "http://localhost:8080/csdn/search/" + keyword + "/" + this.currentPage;
                console.log(url);
                axios.get(url).then((response) => {
                    if(response.data.msg.count==0) {
                        this.isnull = true;
                        // 将原始数据置空
                        this.items = [];
                        // 不显示上一页下一页按钮
                        this.isshow = false;
                    } else {
                        this.isnull = false;
                        console.log(response)
                        this.items = response.data.msg.list;
                        this.countInfo = response.data.msg.count;
                        // 计算一共有几页
                        this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                        this.isshow = true;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getNextPage() {
                if(this.currentPage == this.countAll) {
                    this.currentPage = this.currentPage;
                } else {
                    this.currentPage = this.currentPage + 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 计算一共有几页
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getPrePage() {
                if(this.currentPage == 1) {
                    this.currentPage = 1;
                } else {
                    this.currentPage = this.currentPage - 1;
                }
                var url = "http://localhost:8080/csdn/search/" + this.keyword + "/" + this.currentPage;
                axios.get(url).then((response) => {
                    console.log(response)
                    this.items = response.data.msg.list;
                    // 计算一共有几页
                    this.countAll = Math.ceil(response.data.msg.count / this.pageSize); 
                })
                .catch(function (error) {
                    console.log(error);
                });
            }
        },
    }).mount("#app");
</script>
</html>

使用vue编写前端动态页面真的比原生js或者jquery要方便很多,对比theamleaf也有很多好处。

我们在使用theamleaf的时候,每次提交表单都需要刷新页面,使用vue+axios进行ajax请求则不需要刷新页面,这不仅会减轻服务端的压力,而且可以带来更好的用户体验。

到此这篇关于Vue分页查询怎么实现的文章就介绍到这了,更多相关Vue分页查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Vue分页查询怎么实现

本文链接: https://www.lsjlt.com/news/208612.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue分页查询怎么实现
    我编写了一个简单的前端页面用来查询数据,页面一共有几个逻辑 具体的效果可以看下面的演示 下面就来看一下具体的实现步骤。 首先看一下vue的代码 <script type="...
    99+
    2023-05-15
    Vue分页查询实现 Vue分页功能
  • Vue分页查询如何实现
    这篇文章主要介绍“Vue分页查询如何实现”,在日常操作中,相信很多人在Vue分页查询如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue分页查询如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-07-06
  • Ajax怎么实现分页查询
    这篇文章给大家分享的是有关Ajax怎么实现分页查询的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。要求:获取数据库中大量的信息显示在页面上,必然要使用到分页查询;若不使用Ajax,...
    99+
    2022-10-19
  • Mybatis分页查询怎么实现
    小编给大家分享一下Mybatis分页查询怎么实现,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!我们实现查询除了@org.junit.Test  ...
    99+
    2023-06-28
  • oracle怎么实现分页查询
    在Oracle中,可以通过使用ROWNUM和子查询来实现分页查询。以下是一个示例:```sqlSELECT * FROM (SELECT column1, column2, ..., ROWNUM AS rnFROM y...
    99+
    2023-08-11
    oracle
  • MSSQLServer中怎么实现查询分页
    本篇文章给大家分享的是有关MSSQLServer中怎么实现查询分页,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。select top...
    99+
    2022-10-18
  • sql中怎么实现分页查询
    本篇文章为大家展示了sql中怎么实现分页查询,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1.创建测试环境,(插入100万条数据大概耗时5分钟)。create&nb...
    99+
    2022-10-18
  • SQLSERVER中怎么实现分页查询
    SQLSERVER中怎么实现分页查询,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。第一种方案、最简单、普通的方法:复制代码 代...
    99+
    2022-10-18
  • java怎么实现es分页查询
    在Java中,可以使用Elasticsearch的Java客户端库来实现ES分页查询。下面是一个简单的示例代码: import or...
    99+
    2023-10-28
    java es
  • mysql 中怎么实现limit分页查询
    本篇文章为大家展示了mysql 中怎么实现limit分页查询,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。  优化方法1(让分页操作在索引中进行):  一般表中经常...
    99+
    2022-10-18
  • mybatis批量查询分页怎么实现
    MyBatis提供了两种方法来实现批量查询分页:1. 使用`RowBounds`实现分页查询:`RowBounds`是MyBatis...
    99+
    2023-09-05
    mybatis
  • JavaWeb分页查询功能怎么实现
    本篇内容主要讲解“JavaWeb分页查询功能怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JavaWeb分页查询功能怎么实现”吧!效果:实现:分页查询有几个比较重要的参数,pageNum...
    99+
    2023-06-26
  • Mybatis怎么快速实现分页查询
    这篇文章主要讲解了“Mybatis怎么快速实现分页查询”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Mybatis怎么快速实现分页查询”吧!目录前言首先创建一个Maven项目数据库中创建一张...
    99+
    2023-06-20
  • Mybatis实现分页查询
    一. 简单分页查询——limit 使用select查询时,如果结果集数据量较大,一个页面难以处理,就会采用分页查询。 分页查询,就是从结果集中拿出指定的第n页到第m页的数据来显示。 // limit分页公式 // currentP...
    99+
    2023-09-12
    mybatis java mysql
  • hbase分页查询实现
    Hbase本身是没有分页查询的,我在网上找了很多资料来实现一个分页功能,在这里做了一下记录,分享给大家,有什么不足之处,请尽管指出。废话不多说,看代码。import java.io.IOException;...
    99+
    2022-10-18
  • jquery实现查询分页
    随着IT行业的发展,Web应用程序越来越受到人们的欢迎。特别是最近几年,随着移动互联网和大数据等技术的快速发展,Web应用程序的需求也越来越多。在Web应用程序中,数据的查询和展示是很重要的功能之一。在大量数据的情况下,如何快速准确地查询和...
    99+
    2023-05-14
  • springboot结合vue实现增删改查及分页查询
    1:首先。创建一个springboot项目,这里我使用以及构建好基本框架的脚手架,打开是这个样子: Result类:已经封装好了三种返回类型的包装类:code,msg,data ...
    99+
    2022-11-12
  • SQLServer和Oracle中怎么实现分页查询
    本篇文章为大家展示了SQLServer和Oracle中怎么实现分页查询,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1.分页算法 最开始我在网上查找资料的...
    99+
    2022-10-18
  • 数据库中怎么实现分页查询
    数据库中怎么实现分页查询,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、 MySQL 数据库分页查询 MySQL数据库实现分...
    99+
    2022-10-18
  • oracle+mybatis-plus+springboot怎么实现分页查询
    本篇内容主要讲解“oracle+mybatis-plus+springboot怎么实现分页查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“oracle+mybatis-plus+springb...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作