• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用vue实现分页

武飞扬头像
haohulala
帮助1

使用vue实现分页的逻辑并不复杂,接收后端传输过来的数据,然后根据数据的总数和每一页的数据量就可以计算出一共可以分成几页

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

 学新通

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

 学新通

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

首先看一下vue的代码

  1.  
    <script type="text/javascript">
  2.  
    Vue.createApp({
  3.  
    data() {
  4.  
    return {
  5.  
    items : [],
  6.  
    // 关键词
  7.  
    keyword : "",
  8.  
    // 是否没有数据
  9.  
    isnull : false,
  10.  
    // 一开始不显示上一页和下一页
  11.  
    isshow : false,
  12.  
    // 一共有多少条数据
  13.  
    countInfo : 0,
  14.  
    // 每一页显示几条数据
  15.  
    pageSize : 10,
  16.  
    // 当前是第几页
  17.  
    currentPage : 1,
  18.  
    // 一共有几页
  19.  
    countAll : 1,
  20.  
    code : 200
  21.  
    }
  22.  
    },
  23.  
    methods: {
  24.  
    search() {
  25.  
    // 拿到待搜索的关键词
  26.  
    var keyword = document.getElementById("keyword").value;
  27.  
    console.log(keyword);
  28.  
    this.keyword = keyword;
  29.  
    this.currentPage = 1;
  30.  
    var url = "http://localhost:8080/csdn/search/" keyword "/" this.currentPage;
  31.  
    console.log(url);
  32.  
    axios.get(url).then((response) => {
  33.  
    if(response.data.msg.count==0) {
  34.  
    this.isnull = true;
  35.  
    // 将原始数据置空
  36.  
    this.items = [];
  37.  
    // 不显示上一页下一页按钮
  38.  
    this.isshow = false;
  39.  
    } else {
  40.  
    this.isnull = false;
  41.  
    console.log(response)
  42.  
    this.items = response.data.msg.list;
  43.  
    this.countInfo = response.data.msg.count;
  44.  
    // 计算一共有几页
  45.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  46.  
    this.isshow = true;
  47.  
    }
  48.  
    })
  49.  
    .catch(function (error) {
  50.  
    console.log(error);
  51.  
    });
  52.  
    },
  53.  
    getNextPage() {
  54.  
    if(this.currentPage == this.countAll) {
  55.  
    this.currentPage = this.currentPage;
  56.  
    } else {
  57.  
    this.currentPage = this.currentPage 1;
  58.  
    }
  59.  
    var url = "http://localhost:8080/csdn/search/" this.keyword "/" this.currentPage;
  60.  
    axios.get(url).then((response) => {
  61.  
    console.log(response)
  62.  
    this.items = response.data.msg.list;
  63.  
    // 计算一共有几页
  64.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  65.  
    })
  66.  
    .catch(function (error) {
  67.  
    console.log(error);
  68.  
    });
  69.  
    },
  70.  
    getPrePage() {
  71.  
    if(this.currentPage == 1) {
  72.  
    this.currentPage = 1;
  73.  
    } else {
  74.  
    this.currentPage = this.currentPage - 1;
  75.  
    }
  76.  
    var url = "http://localhost:8080/csdn/search/" this.keyword "/" this.currentPage;
  77.  
    axios.get(url).then((response) => {
  78.  
    console.log(response)
  79.  
    this.items = response.data.msg.list;
  80.  
    // 计算一共有几页
  81.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  82.  
    })
  83.  
    .catch(function (error) {
  84.  
    console.log(error);
  85.  
    });
  86.  
    }
  87.  
    },
  88.  
    }).mount("#app");
  89.  
    </script>
学新通

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

  • items:用来存放待展示的数据项
  • keyword:记录本次查询使用的关键词

  • isnull:表示一次查询的结果数量是否为0,用来控制没有结果的显示逻辑

  • isshow:表示是否显示上一页下一页按钮,以及显示当前页数和数据总数

  • countInfo:记录一共有多少条结果

  • pageSize:记录每页显示的数据项,目前后端固定每页展示10条数据

  • currentPage:记录当前是第几页

  • countAll:记录一共有多少页数据

  • code:后端返回的一个状态码,没什么用

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

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

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

学新通

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

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

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

学新通

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

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
  3.  
    <html lang="en">
  4.  
    <head>
  5.  
    <meta charset="UTF-8">
  6.  
    <title>纳米搜索</title>
  7.  
     
  8.  
     
  9.  
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  10.  
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  11.  
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  12.  
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  13.  
    <script src="https://unpkg.com/vue@3"></script>
  14.  
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  15.  
     
  16.  
     
  17.  
     
  18.  
    </head>
  19.  
    <body>
  20.  
     
  21.  
    <div class="container">
  22.  
    <!-- 先编写一个搜索栏 -->
  23.  
    <div class="row" id="app">
  24.  
    <div class="col-md-1"></div>
  25.  
    <div class="col-md-10">
  26.  
     
  27.  
    <!-- 这里面有两个个部分 -->
  28.  
    <div class="row">
  29.  
    <!--<div class="col-md-2"></div>-->
  30.  
    <div class="col-md-12">
  31.  
    <div style="float: left; margin-top: 20px;margin-left: 20%">
  32.  
    <h2 style="color:cornflowerblue">纳米搜索</h2>
  33.  
    </div>
  34.  
    <div style="float: left; margin-top: 20px; margin-left: 20px">
  35.  
    <div class="form-group" style="margin-right: 20px; float: left;" >
  36.  
    <div class="input-group" >
  37.  
    <input type="text" class="form-control" name="keyword" id="keyword" placeholder="请输入要搜索的关键词">
  38.  
    </div>
  39.  
    </div>
  40.  
    <div style="float:left">
  41.  
    <button id="search" type="button" class="btn btn-primary" v-on:click="search">搜索</button>
  42.  
    </div>
  43.  
    </div>
  44.  
    </div>
  45.  
    <!--<div class="col-md-2"></div>-->
  46.  
    </div>
  47.  
    <hr>
  48.  
     
  49.  
    <div>
  50.  
    <div v-for="item of items">
  51.  
    <!-- 第一行是url -->
  52.  
    <a :href="item.url" target="_blank">
  53.  
    <div style="color: #0000cc">{{item.title}}</div>
  54.  
    </a>
  55.  
    <div style="color: #28a745">{{item.url}}</div>
  56.  
    <!-- 这一行显示文章作者 -->
  57.  
    <div style="color: #000000">文章作者:<span style="color: #000000; margin-left: 10px">{{item.nick_name}}</span></div>
  58.  
    <!-- 这一行显示标签 -->
  59.  
    <div style="color: #000000">文章标签:<span style="color: #000000; margin-left: 10px">{{item.tag}}</span></div>
  60.  
    <!-- 下面一行显示发表时间,阅读数和收藏数 -->
  61.  
    <div>
  62.  
    <div style="color: #000000">发表时间:<span style="color: #000000;margin-left: 10px">{{item.up_time}}</span></div>
  63.  
    <div style="color: #000000;float: left">阅读量:<span style="color: #000000;margin-left: 10px">{{item.read_volum}}</span></div>
  64.  
    <div style="color: #000000;float: left; margin-left: 10px">收藏量:<span style="color: #000000;margin-left: 10px">{{item.collection_volum}}</span></div>
  65.  
    </div>
  66.  
    <br>
  67.  
    <hr>
  68.  
    </div>
  69.  
    </div>
  70.  
    <!-- 当没有查询结果的时候显示 -->
  71.  
    <div v-if="isnull">
  72.  
    <span>非常抱歉,没有您想要的结果(。・_・。)ノI’m sorry~</span>
  73.  
    </div>
  74.  
     
  75.  
    <!-- 当有数据的时候显示 -->
  76.  
    <div v-if="isshow">
  77.  
    <div style="float:left; margin-right: 20px;" >
  78.  
    <button type="button" class="btn btn-primary" v-on:click="getPrePage">上一页</button>
  79.  
    </div>
  80.  
    <div style="float:left; margin-right: 20px;" >
  81.  
    <button type="button" class="btn btn-primary" v-on:click="getNextPage" >下一页</button>
  82.  
    </div>
  83.  
    <div style="float:left; margin-right: 20px; margin-top: 5px;">
  84.  
    <span>第{{currentPage}}/{{countAll}}页</spa>
  85.  
    </div>
  86.  
    <div style="float:left; margin-right: 20px; margin-top: 5px;">
  87.  
    <span>共有{{countInfo}}条数据</spa>
  88.  
    </div>
  89.  
    </div>
  90.  
     
  91.  
    </div>
  92.  
    <div class="col-md-1"></div>
  93.  
    </div>
  94.  
     
  95.  
    </div>
  96.  
    </body>
  97.  
    <script type="text/javascript">
  98.  
    Vue.createApp({
  99.  
    data() {
  100.  
    return {
  101.  
    items : [],
  102.  
    // 关键词
  103.  
    keyword : "",
  104.  
    // 是否没有数据
  105.  
    isnull : false,
  106.  
    // 一开始不显示上一页和下一页
  107.  
    isshow : false,
  108.  
    // 一共有多少条数据
  109.  
    countInfo : 0,
  110.  
    // 每一页显示几条数据
  111.  
    pageSize : 10,
  112.  
    // 当前是第几页
  113.  
    currentPage : 1,
  114.  
    // 一共有几页
  115.  
    countAll : 1,
  116.  
    code : 200
  117.  
    }
  118.  
    },
  119.  
    methods: {
  120.  
    search() {
  121.  
    // 拿到待搜索的关键词
  122.  
    var keyword = document.getElementById("keyword").value;
  123.  
    console.log(keyword);
  124.  
    this.keyword = keyword;
  125.  
    this.currentPage = 1;
  126.  
    var url = "http://localhost:8080/csdn/search/" keyword "/" this.currentPage;
  127.  
    console.log(url);
  128.  
    axios.get(url).then((response) => {
  129.  
    if(response.data.msg.count==0) {
  130.  
    this.isnull = true;
  131.  
    // 将原始数据置空
  132.  
    this.items = [];
  133.  
    // 不显示上一页下一页按钮
  134.  
    this.isshow = false;
  135.  
    } else {
  136.  
    this.isnull = false;
  137.  
    console.log(response)
  138.  
    this.items = response.data.msg.list;
  139.  
    this.countInfo = response.data.msg.count;
  140.  
    // 计算一共有几页
  141.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  142.  
    this.isshow = true;
  143.  
    }
  144.  
    })
  145.  
    .catch(function (error) {
  146.  
    console.log(error);
  147.  
    });
  148.  
    },
  149.  
    getNextPage() {
  150.  
    if(this.currentPage == this.countAll) {
  151.  
    this.currentPage = this.currentPage;
  152.  
    } else {
  153.  
    this.currentPage = this.currentPage 1;
  154.  
    }
  155.  
    var url = "http://localhost:8080/csdn/search/" this.keyword "/" this.currentPage;
  156.  
    axios.get(url).then((response) => {
  157.  
    console.log(response)
  158.  
    this.items = response.data.msg.list;
  159.  
    // 计算一共有几页
  160.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  161.  
    })
  162.  
    .catch(function (error) {
  163.  
    console.log(error);
  164.  
    });
  165.  
    },
  166.  
    getPrePage() {
  167.  
    if(this.currentPage == 1) {
  168.  
    this.currentPage = 1;
  169.  
    } else {
  170.  
    this.currentPage = this.currentPage - 1;
  171.  
    }
  172.  
    var url = "http://localhost:8080/csdn/search/" this.keyword "/" this.currentPage;
  173.  
    axios.get(url).then((response) => {
  174.  
    console.log(response)
  175.  
    this.items = response.data.msg.list;
  176.  
    // 计算一共有几页
  177.  
    this.countAll = Math.ceil(response.data.msg.count / this.pageSize);
  178.  
    })
  179.  
    .catch(function (error) {
  180.  
    console.log(error);
  181.  
    });
  182.  
    }
  183.  
    },
  184.  
    }).mount("#app");
  185.  
    </script>
  186.  
     
  187.  
     
  188.  
    </html>
学新通

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiebgbg
系列文章
更多 icon
同类精品
更多 icon
继续加载