原生Html5 使用Vue实现一个 完整的分页功能

用Vue 实现了 个很简单的分页功能。
效果图:
在这里插入图片描述
写的很简单,大家可以 自己diy. 注意 mid 可以 控制 按钮每次调整 后处于的位置。一般是 页码按钮数 / 2 。

<div style="margin:0 auto;text-align:center">
      <a @click="prevPage()">上一页</a>
      <div style="display: inline-block;margin-left: 10px" v-for="index of  pagelist" :key="index">
        <button :class="{active: currentPage === activatePage + index - 1}"  @click="selectPage($event,index)">{{activatePage + index -1}}</button>
      </div>
      <span >第{{currentPage}}页/共{{totalPage}}页</span>
      <a @click="nextPage($event)">下一页</a>
</div>

data(){
          return{
            productList:{total:200,data:{}}, //所有数据
            totalPage: '', // 总共页数,
            activatePage:1,   //激活页 默认为1
            currentPage: 1, //当前页数 ,默认为1
            pagelist:7, //分页按钮个数
            pageSize: 10, // 每页显示数量
            mid: 3, //点击按钮 分页按钮重新渲染时的位置 一般 是 pagelist /2 居中
          }
      }
      
methods:{
        // 设置当前页面数据,对数组操作的截取规则为[0~9],[10~20]...,
        // 当currentPage为1时,我们显示(0*pageSize+1)-1*pageSize,当currentPage为2时,我们显示(1*pageSize+1)-2*pageSize...
        getCurrentPageData() {
          let begin = (this.currentPage - 1) * this.pageSize;
          let end = this.currentPage * this.pageSize;
          this.mid = Math.floor(this.pagelist/2);
          //这里自己diy请求数据

          this.totalPage = this.productList.total / this.pageSize;

        },
        //上一页
        prevPage() {
          console.log(this.currentPage);
          if (this.currentPage === 1) {
            return false;
          } else {
            this.currentPage--;
            if (this.activatePage !== 1 ) {
              if (this.currentPage <= (this.totalPage - this.pagelist  + this.mid )){
                this.activatePage = this.currentPage - this.mid;
              }
            }

            this.getCurrentPageData();
          }
        },
        // 下一页
        nextPage() {

          if (this.currentPage === this.totalPage) {
            return false;
          } else {
            if (this.activatePage !== this.totalPage - this.pagelist  + 1 ) {
              if (this.currentPage >= ( this.pagelist  - this.mid  )){
                this.activatePage = this.currentPage - this.mid + 1 ;
              }
            }
            this.currentPage++;

            this.getCurrentPageData();
          }
        },selectPage(event,msg){
          //计算 是往前还是往后移动
          let gap =  (this.activatePage + msg -1)  - this.currentPage ;

          //把 当前页更新
          this.currentPage=  this.activatePage + msg -1 ;
          if (this.currentPage > this.totalPage){
            this.currentPage = this.totalPage;
          }
          if (this.currentPage < 1){
            this.currentPage = 1;
          }
          //如果是 往前移动 需要 判断两种情况 第一种 如果移动到的下一步 加上 显示的页码按钮数 超出了 总页码数
          //那么 我们就 把 页码按钮的起始更新为 页码数 - 页码按钮显示数 + 1
          //如果小于等于 那么把 页码按钮更新为点击的页码按钮
           if(gap >0 &&  (this.currentPage +  this.pagelist -1) > this.totalPage){
            this.activatePage = this.totalPage - this.pagelist + 1;
          }else if (gap >0 &&  (this.currentPage +  this.pagelist -1) <= this.totalPage){
             //对 最小需要调整按钮的边界进行判断
             if (this.currentPage >= ( this.pagelist  - this.mid)){
               this.activatePage = this.currentPage - this.mid;
             }

          }


          //和上面 一样 我们需要判断 点击分页按钮的 索引  如果点击按钮的数 - 分页按钮的个数  小于0了 那我们 把 分页按钮其实位置改成0
          //否则的 话 就直接 更新成 点击按钮的索引
          if(gap <0 &&  (this.currentPage -  this.pagelist  + 1) <= 1 ){
            this.activatePage = 1;

          }else if (gap < 0 &&  (this.currentPage -  this.pagelist +1) > 1 ){
            //对 最大需要调整按钮的边界进行判断
            if (this.currentPage <= (this.totalPage - this.pagelist  + this.mid)){
              this.activatePage = this.currentPage - this.mid;
            }



          }
          var el = event.currentTarget;
          this.getCurrentPageData();

        }

}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是在Vue使用`ul`和`li`标签展示数据并完成分页功能的代码示例,分页使用原生`JavaScript`实现: ```html <template> <div id="page-container"> <ul id="pagination"> <li v-for="page in pageCount" :key="page"> <a href="javascript:void(0)" @click="showPage(page)">{{ page }}</a> </li> </ul> <ul id="data-list"> <li v-for="item in pageData" :key="item">{{ item }}</li> </ul> </div> </template> <script> export default { data() { return { data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], // 分页数据 pageSize: 5, // 每页显示的条目数 currentPage: 1, // 当前页码 }; }, computed: { // 计算总页数 pageCount() { return Math.ceil(this.data.length / this.pageSize); }, // 计算当前页的数据 pageData() { const start = (this.currentPage - 1) * this.pageSize; const end = start + this.pageSize; return this.data.slice(start, end); }, }, methods: { // 显示指定页的数据 showPage(page) { this.currentPage = page; }, }, }; </script> ``` 在上面的代码中,我们使用Vue的计算属性来计算总页数和当前页的数据。在模板中,我们通过`v-for`指令循环生成分页列表和数据列表中的每个元素。在分页列表中,我们给每个页码链接添加了一个`click`事件,点击链接时调用`showPage`方法来显示对应页码的数据。在`showPage`方法中,我们将当前页码赋值给`currentPage`变量,Vue会自动根据`currentPage`的变化重新计算当前页的数据并更新页面。这样,我们就实现了在Vue使用`ul`和`li`标签展示数据并完成分页功能的效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值