Vue.js读取本地json文件并分页显示

Vue.js读取本地json文件并分页显示

1.功能实现
通过axios异步加载技术读取本地的json文件内容,并通过vue.js处理数据在h5页面分页显示(这里以3行数据分页)

2.student.json数据如下

[
  {"stuId":1,"stuName":"李华","stuSex":"男","stuAge":20},
  {"stuId":2,"stuName":"张国伟","stuSex":"男","stuAge":22},
  {"stuId":3,"stuName":"刘艳","stuSex":"女","stuAge":19},
  {"stuId":4,"stuName":"李小燕","stuSex":"女","stuAge":22},
  {"stuId":5,"stuName":"张鹏","stuSex":"男","stuAge":26},
  {"stuId":6,"stuName":"李晔","stuSex":"女","stuAge":20},
  {"stuId":7,"stuName":"钱国强","stuSex":"男","stuAge":21},
  {"stuId":8,"stuName":"张三","stuSex":"男","stuAge":22},
  {"stuId":9,"stuName":"唐毓民","stuSex":"男","stuAge":25},
  {"stuId":10,"stuName":"玛丽亚","stuSex":"女","stuAge":21},
  {"stuId":11,"stuName":"李家明","stuSex":"男","stuAge":21}
]

3.h5代码如下

<body>
  <div id ="app" v-cloak>
    <table border="1px" style="width: 400px;" class="table table-striped table-bordered table-hover table-condensed">
      <thead>
         <tr>
           <th>序号</th>
           <th>姓名</th>
           <th>性别</th>
           <th>年龄</th>
         </tr>
      </thead>
     <tr v-for="student in stuData">
       <td>{{ student.stuId }}</td>
       <td>{{ student.stuName }}</td>
       <td>{{ student.stuSex }}</td>
       <td>{{ student.stuAge }}</td>
     </tr>
    </table>
    <!-- 用无序列表做一个页码导航条-->
    <ul>
      <li><a href="#" @click="prePage"> < </a></li>
      <li v-for="(value,index) in pageNumber">
       <a href="#" @click="thisPage(index)">{{ index+1 }}</a>
      </li>
      <li><a href="#" @click="nextPage"> > </a></li>
    </ul>
  </div>
</body>

4.css样式

<style>
  [v-cloak]{
    display: none;
  }
  ul{
   margin-left: 20px;
  }
  ul li{
    float: left;
    list-style: none;
    background-color: aqua;
  }
  ul li a{
    text-decoration: none;
    padding: 5px 15px;
    color:black;
    border: 1px solid white;
  }
  a:hover{
    background: tomato;
  }
</style>

5.js代码

<script>
   //创建Vue实例,得到 ViewModel
   var app = new Vue({
    el: '#app',
    data: {
     list:[],
     pageSize:3,//每页大小
     currentPage:0 //当前页码

    },/*数据*/
    mounted(){
     //异步加载json数据
     axios.get('/json/student.json',{}).then(function(response){
      app.list=response.data;
     });
    },/*自动加载函数*/
    methods: {
      //上一页
      nextPage: function(){
            if (this.currentPage == this.pageNumber - 1) return;
            this.currentPage++;
        },
        //下一页
        prePage: function(){
            if (this.currentPage == 0) return;
            this.currentPage--;
        },
        //页码
        thisPage: function(index){
           this.currentPage = index;
        }
    },/*执行触发函数*/
    computed: {
      //分页数据
      stuData: function(){
            let left = this.currentPage*this.pageSize;
            let right = Math.min((this.currentPage+1)*this.pageSize, this.list.length)
            return this.list.slice(left, right);//取出一页数据
        },
        //共有多少页
        pageNumber: function(){
            if(this.list.length<=this.pageSize){
            return 1;
          }
            return Math.ceil(this.list.length / this.pageSize);
        }
    },/*动态计算属性*/
   });
  </script>

6.运行效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值