vxetable实现前端分页

9 篇文章 0 订阅

直接上代码吧

<template>
  <div>
    <page-wraper>
      <a-modal
        title="详情"
        v-model="visible"
        @ok="visible = !visible"
        @cancel="visible = !visible"
        width="900px"
        :destroyOnClose="true"
        :maskClosable="false"
      >
        <vxe-table
          border
          show-overflow
          ref="dataTable"
          height="460"
          :data="tableData"
          :columns="columns"
          :head-toolbar="headToolbar"
          :pager-config="pagerConfig"
          :seq-config="{
            startIndex: (pagerConfig.currentPage - 1) * pagerConfig.pageSize
          }"
          @page-change="handlePageChange"
        >
        </vxe-table>
      </a-modal>
    </page-wraper>
  </div>
</template>

<script>
import ticketNoteApi from "@/api/ticketNote";

export default {
  name: "BusinessUnit",
  props: {
    data: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      visible: false,
      modal: {},
      form: {},
      tableData: [],
      id: "",
      detailData: [],
      pagerConfig: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      headToolbar: {
        search: {
          layout: "inline",
          titleWidth: "auto",
          // foldingLayout:"flex",
          on: {
            submit: values => {
              this.frontFilter(values);
            }
          },
          items: [
            {
              field: "userName",
              title: "使用人姓名",
              itemRender: {
                name: "a-input",
                props: { placeholder: "请输入名称" }
              }
            },
            {
              field: "status",
              title: "状态",
              itemRender: {
                name: "a-select",
                props: {
                  placeholder: "请选择性别",
                  showSearch: true,
                  defaultField: "isSelected",
                  valueField: "id",
                  labelField: "name",
                  param: { code: "TicketNoteDetailStatusType" }
                }
              }
            }
          ]
        }
      },
      columns: [
        { type: "seq", title: "序号", width: 50 },
        {
          field: "ticketYear",
          title: "票据年份",
          sortable: true,
          width: 100
        },
        {
          field: "serialValue",
          title: "原始流水值",
          align: "left",
          sortable: true,
          width: 100
        },
        {
          field: "ticketSerialNo",
          title: "票据流水号",
          sortable: true,
          width: 100
        },
        {
          field: "applyUserName",
          title: "使用人姓名",
          sortable: true,
          width: 100
        },
        {
          field: "applyTime",
          title: "使用时间",
          sortable: true,
          width: 100
        },
        {
          field: "status",
          title: "状态",
          sortable: true,
          width: 100
        },
        {
          field: "cancelUserName",
          title: "取消作废人姓名",
          sortable: true,
          width: 100
        },
        {
          field: "cancelTime",
          title: "取消作废时间",
          sortable: true,
          width: 100
        }
      ]
    };
  },
  created() {},
  methods: {
    show(id) {
      this.visible = true;
      this.id = id;
      ticketNoteApi
        .getDetail({
          id: id
        })
        .then(res => {
          this.loading = false;
          this.detailData = this.splitArr(
            res.data.detailList,
            this.pagerConfig.pageSize
          );
          this.pagerConfig.total = res.data.detailList.length;
          this.handlePageChange({ ...this.pagerConfig });
        });
    },
    handlePageChange({ currentPage, pageSize }) {
      this.pagerConfig.currentPage = currentPage;
      this.pagerConfig.pageSize = pageSize;
      this.tableData = this.detailData[currentPage - 1];
    },
    splitArr(ar, size = 1) {
      let index = 0;
      let res = [];
      while (index < ar.length) {
        res.push(ar.slice(index, index + size));
        index += size;
      }
      return res;
    }
  }
};
</script>

<style scoped></style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的使用Vue实现前端分页的示例: ```html <template> <div> <table> <thead> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in displayData" :key="index"> <td>{{ startIndex + index }}</td> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.gender }}</td> </tr> </tbody> </table> <div class="pagination"> <button @click="prevPage" :disabled="currentPage === 1">上一页</button> <button v-for="page in pages" :key="page" @click="changePage(page)" :class="{ active: currentPage === page }">{{ page }}</button> <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button> </div> </div> </template> <script> export default { data() { return { dataList: [ { name: '张三', age: 20, gender: '男' }, { name: '李四', age: 22, gender: '女' }, { name: '王五', age: 25, gender: '男' }, { name: '赵六', age: 21, gender: '女' }, { name: '孙七', age: 24, gender: '男' }, { name: '周八', age: 26, gender: '女' }, { name: '吴九', age: 23, gender: '男' }, { name: '郑十', age: 28, gender: '女' } ], pageSize: 3, // 每页显示数量 currentPage: 1, // 当前页码 startIndex: 0, // 当前页数据的起始下标 pageCount: 0 // 总页数 } }, computed: { // 计算当前显示的数据 displayData() { return this.dataList.slice(this.startIndex, this.startIndex + this.pageSize) }, // 计算页码数组 pages() { const pageArr = [] for (let i = 1; i <= this.pageCount; i++) { pageArr.push(i) } return pageArr } }, mounted() { // 计算总页数 this.pageCount = Math.ceil(this.dataList.length / this.pageSize) }, methods: { // 上一页 prevPage() { this.currentPage-- this.startIndex = (this.currentPage - 1) * this.pageSize }, // 下一页 nextPage() { this.currentPage++ this.startIndex = (this.currentPage - 1) * this.pageSize }, // 点击页码 changePage(page) { this.currentPage = page this.startIndex = (this.currentPage - 1) * this.pageSize } } } </script> ``` 上面的代码中,我们首先定义了一个数据数组 dataList,里面存放了我们要展示的数据。然后定义了 pageSize、currentPage、startIndex、pageCount 等数据,用来记录每页显示数量、当前页码、当前页数据的起始下标和总页数等信息。 接着,在计算属性 displayData 中,我们根据 startIndex 和 pageSize 计算出当前页显示的数据。 在计算属性 pages 中,我们使用循环来生成页码数组。 在 mounted 钩子函数中,我们计算了总页数 pageCount。 最后,在 methods 中,我们定义了 prevPage、nextPage 和 changePage 三个方法,用来处理上一页、下一页和页码点击事件。在这些方法中,我们更新了 currentPage 和 startIndex,从而实现前端分页的效果。 总的来说,使用Vue实现前端分页并不困难,只需要合理利用Vue的计算属性和方法即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值