前端实现分页

<template>
  <div>
    <div id="PaginationSimple">
      <table class="table-main">
        <tr class="table-header">
          <th v-for="(item,index) in tableHead" :key="index">{{item}}</th>
        </tr>
        <tr class="table-detail" v-for="(item, index) in currentPageData" :key="index"
          :class="{ whited: index % 2 === 1 }">
          <th>{{ item.id }}</th>
          <th>{{ item.productName }}</th>
          <th>{{ item.manufactureDate }}</th>
          <th>{{ item.publisher }}</th>
          <th>
            <button @click="previewSelected(item)">预览</button>
            <button @click="downloadSelected(item)">下载</button>
            <button @click="delSelectedTableDetail(item)">删除</button>
          </th>
        </tr>
        <div class="table-pagination">
          <div>
            共{{tableDetailLength}},每页{{pageSize}}条

            <!-- 上一页  -->
            <button @click="prePage"> 上一页</button>

            <!-- 点击 按钮 实现分页  -->
            <button v-for="(item,index) in totalPageArray" :key="index"
              :class="{selected:item ==currentPage}" @click="selectPage(item)">
              {{item}}
            </button>

            <!-- 下一页  -->
            <button @click="nextPage"> 下一页</button>

            <!-- 到第xxx 页  -->
            到第<input type="text" v-model="inputPage" @keyup.enter="selectPage(inputPage)">
            页
            <button class="confirm-page" @click="selectPage(inputPage)">确定</button>
          </div>
        </div>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表头 
      tableHead: ["序号", "产品名称", "制作日期", "发布人", "操作"],
      // 表身体 
      tableDetail: [],
      // 分页器相关
      currentPage: 2, // 当前页
      pageSize: 3, // 每页显示多少条
      tableDetailLength: 1, // 总数据条数
      totalPageArray: [], // 存储按钮
      totalPage: 1, // 按钮数量
      inputPage: null
    }
  },
  computed: {
    currentPageData: function() {
      let begin = (this.currentPage - 1) * this.pageSize;
      let end = this.currentPage * this.pageSize;
      return this.tableDetail.slice(begin, end);
    },
  },
  mounted() { // 拿到数据
    this.tableDetail = [{
        id: "1",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "2",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "3",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "4",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "5",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "6",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "7",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "8",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "9",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "10",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "11",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "12",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
      {
        id: "13",
        productName: "天气预报",
        manufactureDate: "1月1日",
        publisher: "思思",
      },
    ];
    // 有多少条数据
    this.tableDetailLength = this.tableDetail.length
    // 计算有几个按钮
    let num = this.tableDetailLength / this.pageSize

    for (let i = 1; i <= num; i++) {
      this.totalPageArray.push(i)
    }

    // 避免出现数据条数是奇数没有按钮点击可以分页 
    if (this.tableDetail.length % this.pageSize !== 0) {
      this.totalPageArray.push(this.totalPageArray.length + 1)
    }

    // 得到按钮总数
    this.totalPage = this.totalPageArray.length
  },
  methods: {
    // 实现 点击 1 2 3 4 切换 
    selectPage(item) {
      this.currentPage = item
    },
    // 上一页
    prePage() {
      if (this.currentPage === 1) return // 等于1 不继续执行
      this.currentPage--
    },
    // 下一页 
    nextPage() {
      if (this.currentPage === this.totalPage) return; // 等于按钮数量 不往下执行
      this.currentPage++;
    }
  }
}
</script>

<style lang="less" scoped>
#PaginationSimple {
  width: 79%;
  height: calc(100% - 100px);
  margin-top: 23px;
  margin-left: 1%;

  .table-main {
    .table-head {
      background: #f0f7ff;
      height: 40px;
      font-size: 14px;
      color: #4d4d4d;

      th {
        font-weight: normal;
        width: 230px;
      }

      th:nth-child(4),
      th:nth-child(5),
      th:nth-child(6) {
        width: 169px;
      }

      th:nth-child(7) {
        width: 320px;
      }
    }

    .table-detail {
      border-bottom: 1px solid #eeeeee;
      height: 44px;
      font-size: 14px;
      color: #4d4d4d;

      th {
        font-weight: normal;
        width: 230px;
      }

      th:nth-child(4),
      th:nth-child(5),
      th:nth-child(6) {
        width: 169px;
      }

      th:nth-child(7) {
        width: 320px;

        button {
          font-size: 12px;
          color: #ffffff;
          width: 48px;
          height: 24px;
          border-radius: 12px;
          margin: 0 5px;
        }

        button:nth-child(1) {
          background: linear-gradient(-90deg, #2f87f5, #126cdc);
          border: 1px solid #126cdc;
        }

        button:nth-child(2) {
          background: linear-gradient(-90deg, #5f75f6, #6768ef);
          border: 1px solid #6768ef;
        }

        button:nth-child(3) {
          background: linear-gradient(-90deg, #f28255, #ef6747);
          border: 1px solid #ef6747;
        }
      }
    }

    .whited {
      background-color: #f7f7f7;
    }
  }

  .table-pagination {
    color: #353535;
    font-size: 14px;
    position: relative;
    width: 500px;
    height: 75px;
    line-height: 75px;
    border-left: 1px solid #eeeeee;
    border-right: 1px solid #eeeeee;
    border-bottom: 1px solid #eeeeee;

    div {
      position: absolute;
      right: 14px;

      button {
        background-color: transparent;
        width: 24px;
        height: 24px;
        border: none;
      }

      .selected {
        background-color: #006dfa;
        color: #ffffff;
      }

      input {
        width: 65px;
        height: 26px;
        border: 1px solid #d9d9d9;
        border-radius: 2px;
        margin: 0 15px 0 10px;
      }

      .confirm-page {
        border: 1px solid #d9d9d9;
        border-radius: 2px;
        width: 48px;
        height: 27px;
        margin: 0 10px;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值