• Element Plus& Ant Design(react) 表格的分页封装


    一、Element Plus

    封装代码:

    1. <template>
    2. <el-table ref="multipleTableRef" :data="searchParams.values" style="width: 100%"
    3. @selection-change="handleSelectionChange" border>
    4. <el-table-column type="selection" width="55" />
    5. <el-table-column v-for="item in column" :key="item.prop" :prop="item.prop" :label="item.label" :width="item.width">
    6. </el-table-column>
    7. </el-table>
    8. <!-- 分页 -->
    9. <div class="pagination">
    10. <el-pagination v-model:currentPage="searchParams.pagenum" v-model:page-size="searchParams.pagesize"
    11. :page-sizes="[1, 3, 5, 10]" layout="total, sizes, prev, pager, next" :total="total" @size-change="searchList"
    12. @current-change="searchList" />
    13. </div>
    14. </template>
    15. <script lang="ts" setup>
    16. import { onMounted, reactive, ref, toRaw } from 'vue'
    17. import { ElTable } from 'element-plus'
    18. import moment from 'moment'
    19. interface User {
    20. date: string
    21. name: string
    22. address: string
    23. }
    24. const multipleTableRef = ref<InstanceType<typeof ElTable>>()
    25. const multipleSelection = ref<User[]>([])
    26. const props = defineProps(['tableData', 'column'])
    27. const emits = defineEmits(['selectChange',])
    28. const searchParams = reactive({/*分页的参数 */
    29. query: "",
    30. pagesize: 5,
    31. pagenum: 1,
    32. values: []
    33. })
    34. const total = ref(0)/* 总数 */
    35. const tableRef = ref()
    36. onMounted(() => {
    37. searchList()
    38. })
    39. /* 分页的逻辑 */
    40. const searchList = () => {
    41. let results = toRaw(props.tableData)
    42. console.log(results);
    43. // 使用正则表达式进行模糊查询
    44. const searchTerm = searchParams.query;
    45. const regex = new RegExp(searchTerm, "gi");
    46. searchParams.values = results.filter((item) => regex.test(item.username));
    47. let len = searchParams.values.length;
    48. let num = len - searchParams.pagesize;
    49. // // 获取到的数组长度大于页面展示的长度
    50. if (num > 0 && searchParams.pagenum === 1) {
    51. console.log("展示首页内容!");
    52. searchParams.values.splice(searchParams.pagenum, num);
    53. } else if (num > 0 && searchParams.pagenum !== 1) {
    54. console.log("展示后面的内容!");
    55. console.log(
    56. "前面要删除的数目:" +
    57. (searchParams.pagenum - 1) * searchParams.pagesize
    58. );
    59. searchParams.values.splice(
    60. 0,
    61. (searchParams.pagenum - 1) * searchParams.pagesize
    62. );
    63. // 二次截断
    64. let len2 = searchParams.values.length;
    65. searchParams.values.splice(
    66. searchParams.pagesize,
    67. len2 - searchParams.pagesize
    68. );
    69. }
    70. total.value = len;
    71. };
    72. /* 多选框选中 */
    73. const handleSelectionChange = (val: User[]) => {
    74. multipleSelection.value = val
    75. emits('selectChange', val)
    76. }
    77. </script>
    78. <style scoped>
    79. .pagination {
    80. margin-top: 20px;
    81. display: flex;
    82. justify-content: flex-end;
    83. }
    84. .handle {
    85. color: #0279fe;
    86. margin-right: 6px;
    87. }
    88. .handle:hover {
    89. cursor: pointer;
    90. }
    91. </style>

    使用:

    1. <template>
    2. <el-card>
    3. <template #header>
    4. <div class="card-header">
    5. <span>数据列表</span>
    6. </div>
    7. </template>
    8. <baseTable v-if="tableData.length !== 0" :tableData="tableData" :column="column" @selectChange="handleSelectionChange">
    9. </baseTable>
    10. </el-card>
    11. </template>
    12. <script setup>
    13. import { onMounted, reactive, ref } from 'vue';
    14. import baseTable from '../../../../components/basTable1.vue'
    15. import axios from 'axios'
    16. const tableData = ref([])//表格数据
    17. onMounted(() => {
    18. getTableData()
    19. })
    20. /* 表头 */
    21. const column = reactive([
    22. {
    23. label: '公司名称',
    24. prop: 'corporation',
    25. },
    26. {
    27. label: '管理地区',
    28. prop: 'region',
    29. width: 200
    30. },
    31. {
    32. label: '所在地区',
    33. prop: 'nowregion',
    34. },
    35. {
    36. label: '地址',
    37. prop: 'address',
    38. },
    39. {
    40. label: '级别',
    41. prop: 'roleId',
    42. },
    43. {
    44. label: '账户姓名',
    45. prop: 'username',
    46. },
    47. {
    48. label: '注册手机号',
    49. prop: 'phone',
    50. },
    51. {
    52. label: '账户姓名',
    53. prop: 'username',
    54. },
    55. {
    56. label: '创建时间',
    57. prop: "createTime",
    58. width: 200
    59. },
    60. {
    61. label: '状态',
    62. prop: 'roleState',
    63. },
    64. {
    65. label: '操作',
    66. prop: 'handle',
    67. btn: ['赋权', '编辑', '开通', '禁用', '删除'],
    68. width: 200
    69. },
    70. ])
    71. /* 获取表格数据 */
    72. const getTableData = async () => {
    73. try {
    74. let data = await axios.get("Yourpath")
    75. tableData.value = data.data.results
    76. }
    77. catch (err) {
    78. console.log(err);
    79. }
    80. }
    81. /* 多选框选中的内容 */
    82. const handleSelectionChange = (val) => {
    83. console.log(JSON.parse(JSON.stringify(val)));
    84. }
    85. </script>
    86. <style lang="scss" scoped></style>

    二、Ant Design

    封装代码:

    1. import React, { useState } from 'react';
    2. import { Space, Table, Tag } from 'antd';
    3. const App = ({columns,data}) => {
    4. const [pagination, setPagination] = useState({
    5. current: 1,
    6. pageSize: 3,
    7. });
    8. const handleTableChange = (pagination) => {
    9. setPagination(pagination);
    10. };
    11. return (
    12. <Table
    13. columns={columns}
    14. dataSource={data}
    15. pagination={{
    16. current: pagination.current,
    17. pageSize: pagination.pageSize,
    18. showSizeChanger: true,
    19. pageSizeOptions: ['1','3', '5', '10'],
    20. showTotal: (total) => `一共 ${total} 条`,
    21. onChange: (page, pageSize) => handleTableChange({ current: page, pageSize }),
    22. onShowSizeChange: (current, size) => handleTableChange({ current, pageSize: size }),
    23. }}
    24. />
    25. );
    26. };
    27. export default App;

    使用:

    1. import React from 'react'
    2. import BaseTable from '../../../component/baseTable'
    3. import { Space, Tag } from 'antd';
    4. export default function UserData() {
    5. const columns = [
    6. {
    7. title: 'Name',
    8. dataIndex: 'name',
    9. key: 'name',
    10. render: (text) => <a>{text}</a>,
    11. },
    12. {
    13. title: 'Age',
    14. dataIndex: 'age',
    15. key: 'age',
    16. },
    17. {
    18. title: 'Address',
    19. dataIndex: 'address',
    20. key: 'address',
    21. },
    22. {
    23. title: 'Tags',
    24. key: 'tags',
    25. dataIndex: 'tags',
    26. render: (_, { tags }) => (
    27. <>
    28. {tags.map((tag) => {
    29. let color = tag.length > 5 ? 'geekblue' : 'green';
    30. if (tag === 'loser') {
    31. color = 'volcano';
    32. }
    33. return (
    34. <Tag color={color} key={tag}>
    35. {tag.toUpperCase()}
    36. </Tag>
    37. );
    38. })}
    39. </>
    40. ),
    41. },
    42. {
    43. title: 'Action',
    44. key: 'action',
    45. render: (_, record) => (
    46. <Space size="middle">
    47. <a>Invite {record.name}</a>
    48. <a>Delete</a>
    49. </Space>
    50. ),
    51. },
    52. ]
    53. const data = [
    54. {
    55. key: '1',
    56. name: 'John Brown',
    57. age: 32,
    58. address: 'New York No. 1 Lake Park',
    59. tags: ['nice', 'developer'],
    60. },
    61. {
    62. key: '2',
    63. name: 'Jim Green',
    64. age: 42,
    65. address: 'London No. 1 Lake Park',
    66. tags: ['loser'],
    67. },
    68. {
    69. key: '3',
    70. name: 'Joe Black',
    71. age: 32,
    72. address: 'Sydney No. 1 Lake Park',
    73. tags: ['cool', 'teacher'],
    74. },
    75. ];
    76. return (
    77. <div>
    78. <BaseTable columns={columns} data={data}/>
    79. </div>
    80. )
    81. }

  • 相关阅读:
    四、Netty模块组件
    基于深度学习的加密恶意流量检测
    【DC-DC升压电推剪方案】FP6277,FP6296电源升压芯片在电推剪中扮演着一个怎样的角色?带你深入了解电推剪的功能和应用及工作原理
    https跳过SSL认证时是不是就是不加密的,相当于http?
    ubuntu22.04 在wifi网络正常使用的情况下创建热点连接
    R语言包的升级与降级
    【基本算法题-2022.7.26】4. 起床困难综合症
    生成.keystore 安卓签名
    m3u8 文件格式详解
    合宙Air724UG LuatOS-Air LVGL API控件-图片(Gif)
  • 原文地址:https://blog.csdn.net/m0_69210482/article/details/140953820