• Vue3+ElementPlus纯前端分页(手撕分页),无需修改后端


    前提:先把pagination安装上先

    1、在script中加上

            

    1. // 实现分页
    2. const currentPage = ref(1);
    3. const pageSize = ref(10);
    4. const totalItems = computed(() => tableData.value.length);
    5. const paginatedData = computed(() => {
    6. const start = (currentPage.value - 1) * pageSize.value;
    7. const end = start + pageSize.value;
    8. return tableData.value.slice(start, end);
    9. });
    10. const handleCurrentChange = (newPage) => {
    11. currentPage.value = newPage;
    12. };
    13. const handleSizeChange = (newSize) => {
    14. pageSize.value = newSize;
    15. };

    2.如果有查询的,调用查询后重置页码

    currentPage.value = 1; // 重置页码

    3.中的数据由原本的tableData更换为分页处理后的:paginatedData

    4.紧接着标签下面加上el-pagination

    1. <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
    2. :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="totalItems">
    3. el-pagination>

    修改前和修改后对比

    修改前:

    1. <template>
    2. <el-card class="box-card">
    3. <el-row>
    4. <el-col :span="5">
    5. <el-input v-model="searchVal" placeholder="请输入需要查询内容" @change="Search" />
    6. el-col>
    7. <el-col :span="12">
    8. <el-button :disabled="!Sagency" type="primary" @click="Search">查询el-button>
    9. <el-button :disabled="!Iagency" @click="open" type="primary">新增el-button>
    10. <el-button type="primary" @click="handleExport">导出Excelel-button>
    11. el-col>
    12. el-row>
    13. <br>
    14. <el-row>
    15. <el-col>
    16. <el-table :data="tableData" style="width: 100%;height: 65vh;" border ref="tb">
    17. <el-table-column type="selection" width="55" />
    18. <el-table-column prop="Ano" label="编号" width="150" />
    19. <el-table-column prop="Aname" label="姓名" width="150" />
    20. <el-table-column prop="Asex" label="性别" width="150" />
    21. <el-table-column prop="Aphone" label="电话" width="150" />
    22. <el-table-column prop="Aremark" label="备注" width="190" />
    23. <el-table-column label="操作" align="center">
    24. <template #default="scope">
    25. <el-button :disabled="!Fagency" size="small" @click="handleEdit(scope.$index, scope.row)">修改el-button>
    26. <el-button :disabled="!Dagency" size="small" type="danger"
    27. @click="handleDelete(scope.$index, scope.row)">删除el-button>
    28. template>
    29. el-table-column>
    30. el-table>
    31. <el-pagination style="margin-top: 10px;" background layout="prev, pager, next" :total="total"
    32. :current-page="parms.PageIndex" :page-size="pageSize" @current-change="handlePageChange" />
    33. el-col>
    34. el-row>
    35. <add :isShow="isShow" :info="info" @closeAdd="closeAdd" @success="success">add>
    36. el-card>
    37. template>
    38. <script lang="ts" setup>
    39. import { ElMessage, ElTable } from 'element-plus';
    40. import { onMounted, Ref, ref, toRefs } from 'vue';
    41. import Agency from '../../../class/Agency';
    42. import { delAgency, getAgency } from '../../../http';
    43. import add from './add.vue';
    44. import useStore from '../../../store';
    45. import { exportToExcel } from '../../../tool/report'
    46. const store = useStore()
    47. const total = ref(10)
    48. const parms = ref({
    49. Id: 0,
    50. Ano: "",
    51. Aname: "",
    52. Asex: "",
    53. Aphone: "",
    54. Aremark: "",
    55. PageIndex: 2,
    56. PageSize: 10
    57. })
    58. // -------------------- 载入数据 --------------------
    59. const tableData: Ref<Array<Agency>> = ref<Array<Agency>>([])
    60. const load = async () => {
    61. console.log("load中:" + parms.value.Aname)
    62. let res = await getAgency(parms.value) as any
    63. // console.log("查询结果:" + res as Array)
    64. tableData.value = res as Array<Agency>
    65. }
    66. // 查询
    67. const searchVal = ref("")
    68. const Search = async () => {
    69. parms.value.Aname = searchVal.value
    70. // console.log("parms.value.Aname:" + parms.value.Aname)
    71. await load()
    72. }
    73. onMounted(async () => {
    74. await load()
    75. })
    76. // -------------------- 新增、修改、删除逻辑 Start --------------------
    77. const isShow = ref(false)
    78. const open = () => {
    79. isShow.value = true
    80. }
    81. const closeAdd = () => {
    82. isShow.value = false
    83. info.value = new Agency()
    84. }
    85. const info: Ref<Agency> = ref<Agency>(new Agency()); //响应式对象
    86. const handleEdit = (index: number, row: Agency) => {
    87. info.value = row
    88. index ++
    89. isShow.value = true
    90. }
    91. const success = async (message: string) => {
    92. isShow.value = false
    93. info.value = new Agency()
    94. ElMessage.success(message)
    95. await load()
    96. }
    97. const handleDelete = async (index: number, row: Agency) => {
    98. await delAgency(row.Id)
    99. index ++
    100. await load()
    101. }
    102. const tb = ref<InstanceType<typeof ElTable>>()
    103. // -------------------- 设置分页 ----------------------
    104. const pageSize = ref(10); // Number of records to display per page
    105. // Handle page change
    106. const handlePageChange = async (page: number) => {
    107. // parms.PageIndex = page;
    108. page ++;
    109. // page = 10;
    110. await load();
    111. };
    112. const { Permission } = toRefs(store);
    113. const Dagency = ref(Permission.value.Dagency);
    114. const Fagency = ref(Permission.value.Fagency);
    115. const Iagency = ref(Permission.value.Iagency);
    116. const Sagency = ref(Permission.value.Sagency);
    117. // Excel
    118. const handleExport = () => {
    119. const columnHeaders = ['经办人编号', '姓名', '性别', '电话', '备注'];
    120. exportToExcel(tableData.value, '经办人信息', columnHeaders);
    121. };
    122. script>

    修改后:

    1. <template>
    2. <el-card class="box-card">
    3. <el-row>
    4. <el-col :span="5">
    5. <el-input v-model="searchVal" placeholder="请输入需要查询内容" @change="Search" />
    6. el-col>
    7. <el-col :span="12">
    8. <el-button :disabled="!Sagency" type="primary" @click="Search">查询el-button>
    9. <el-button :disabled="!Iagency" @click="open" type="primary">新增el-button>
    10. <el-button type="primary" @click="handleExport">导出Excelel-button>
    11. el-col>
    12. el-row>
    13. <br>
    14. <el-row>
    15. <el-col>
    16. <el-table :data="paginatedData" style="width: 100%;height: 65vh;" border ref="tb">
    17. <el-table-column type="selection" width="55" />
    18. <el-table-column prop="Ano" label="编号" width="150" />
    19. <el-table-column prop="Aname" label="姓名" width="150" />
    20. <el-table-column prop="Asex" label="性别" width="150" />
    21. <el-table-column prop="Aphone" label="电话" width="150" />
    22. <el-table-column prop="Aremark" label="备注" width="190" />
    23. <el-table-column label="操作" align="center">
    24. <template #default="scope">
    25. <el-button :disabled="!Fagency" size="small" @click="handleEdit(scope.$index, scope.row)">修改el-button>
    26. <el-button :disabled="!Dagency" size="small" type="danger"
    27. @click="handleDelete(scope.$index, scope.row)">删除el-button>
    28. template>
    29. el-table-column>
    30. el-table>
    31. <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
    32. :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="totalItems">
    33. el-pagination>
    34. el-col>
    35. el-row>
    36. <add :isShow="isShow" :info="info" @closeAdd="closeAdd" @success="success">add>
    37. el-card>
    38. template>
    39. <script lang="ts" setup>
    40. import { ElMessage, ElTable } from 'element-plus';
    41. import { onMounted, Ref, ref, toRefs } from 'vue';
    42. import Agency from '../../../class/Agency';
    43. import { delAgency, getAgency } from '../../../http';
    44. import add from './add.vue';
    45. import useStore from '../../../store';
    46. import { exportToExcel } from '../../../tool/report'
    47. import { computed } from 'vue';
    48. const store = useStore()
    49. const total = ref(10)
    50. const parms = ref({
    51. Id: 0,
    52. Ano: "",
    53. Aname: "",
    54. Asex: "",
    55. Aphone: "",
    56. Aremark: "",
    57. PageIndex: 2,
    58. PageSize: 10
    59. })
    60. // -------------------- 载入数据 --------------------
    61. const tableData: Ref<Array<Agency>> = ref<Array<Agency>>([])
    62. const load = async () => {
    63. console.log("load中:" + parms.value.Aname)
    64. let res = await getAgency(parms.value) as any
    65. // console.log("查询结果:" + res as Array)
    66. tableData.value = res as Array<Agency>
    67. }
    68. // 查询
    69. const searchVal = ref("")
    70. const Search = async () => {
    71. parms.value.Aname = searchVal.value
    72. // console.log("parms.value.Aname:" + parms.value.Aname)
    73. await load()
    74. currentPage.value = 1; // 重置页码
    75. }
    76. onMounted(async () => {
    77. await load()
    78. })
    79. // -------------------- 新增、修改、删除逻辑 Start --------------------
    80. const isShow = ref(false)
    81. const open = () => {
    82. isShow.value = true
    83. }
    84. const closeAdd = () => {
    85. isShow.value = false
    86. info.value = new Agency()
    87. }
    88. const info: Ref<Agency> = ref<Agency>(new Agency()); //响应式对象
    89. const handleEdit = (index: number, row: Agency) => {
    90. info.value = row
    91. index ++
    92. isShow.value = true
    93. }
    94. const success = async (message: string) => {
    95. isShow.value = false
    96. info.value = new Agency()
    97. ElMessage.success(message)
    98. await load()
    99. }
    100. const handleDelete = async (index: number, row: Agency) => {
    101. await delAgency(row.Id)
    102. index ++
    103. await load()
    104. }
    105. const tb = ref<InstanceType<typeof ElTable>>()
    106. const { Permission } = toRefs(store);
    107. const Dagency = ref(Permission.value.Dagency);
    108. const Fagency = ref(Permission.value.Fagency);
    109. const Iagency = ref(Permission.value.Iagency);
    110. const Sagency = ref(Permission.value.Sagency);
    111. // Excel
    112. const handleExport = () => {
    113. const columnHeaders = ['编号', '姓名', '性别', '电话', '备注'];
    114. exportToExcel(tableData.value, '经办人信息', columnHeaders);
    115. };
    116. // 实现分页
    117. const currentPage = ref(1);
    118. const pageSize = ref(10);
    119. const totalItems = computed(() => tableData.value.length);
    120. const paginatedData = computed(() => {
    121. const start = (currentPage.value - 1) * pageSize.value;
    122. const end = start + pageSize.value;
    123. return tableData.value.slice(start, end);
    124. });
    125. const handleCurrentChange = (newPage) => {
    126. currentPage.value = newPage;
    127. };
    128. const handleSizeChange = (newSize) => {
    129. pageSize.value = newSize;
    130. };
    131. script>

  • 相关阅读:
    学生静态HTML个人博客主页【Web大学生网页作业成品】HTML+CSS+JavaScript
    JAVA微信小程序浴室预约系统毕业设计 开题报告
    $ vue -Vbash: vue: command not found
    CloudFlare系列--使用第三方来自定义CDN的IP(笨牛详细版)
    1058 选择题 (利用ASCII码)
    【通信工程笔记】【终端与业务-第十二章】市场营销计划、实施和控制
    图像分割-Segment Anything实践
    【C++编程能力提升】
    【C语言】basename函数
    负载均衡 dubbo
  • 原文地址:https://blog.csdn.net/weixin_46407807/article/details/134493506