CommonTable.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <!-- 表格 + 分页 -->
  3. <div class="tablePaganations">
  4. <a-table
  5. class="ant-table-striped"
  6. bordered
  7. size="small"
  8. height="1000px"
  9. :loading="isLoading"
  10. :data-source="dataSource"
  11. :columns="columns"
  12. :scroll="{ y: 1000}"
  13. :pagination="pagination"
  14. :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)"
  15. >
  16. <template v-for="(item,index) in renderArr" #[item]="scope" :key="index">
  17. <slot :name="item" :scope="scope" v-bind="scope || {}" />
  18. </template>
  19. </a-table>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { useSlots, ref, reactive, defineProps, defineEmits, defineExpose, watch } from 'vue';
  24. const props = defineProps({
  25. dataSource: {
  26. type: Object,
  27. required: true,
  28. },
  29. columns: {
  30. type: Object,
  31. required: true,
  32. },
  33. isSelect: {
  34. type: Boolean,
  35. },
  36. isLoading: {
  37. type: Boolean,
  38. },
  39. total: {
  40. type: Number,
  41. default: 0,
  42. },
  43. });
  44. // 分页配置
  45. const pagination = reactive({
  46. showQuickJumper: true,
  47. current: 1,
  48. pageSize: 20, // 默认每页显示数量
  49. showSizeChanger: true, // 显示可改变每页数量
  50. pageSizeOptions: ['10', '20', '50', '100','200','500'], // 每页数量选项值
  51. showTotal: (total, range) => range[0] + '-' + range[1] + '条' + ' 共' + total + '条', // 显示总数
  52. onShowSizeChange: (current, pageSize) => showSizeChange(current, pageSize),
  53. onChange: (current, pageSize) => changePage(current, pageSize), //点击页码事件
  54. total: props.total,
  55. });
  56. const emit = defineEmits(['getPage']);
  57. // 改变每页数量时更新显示
  58. const showSizeChange = (current, pageSize) => {
  59. setTimeout(() => {
  60. pagination.current = 1;
  61. emit('getPage', pagination.current, pageSize);
  62. });
  63. pagination.pageSize = pageSize;
  64. };
  65. //点击页码事件
  66. const changePage = (current, size) => {
  67. pagination.current = current;
  68. emit('getPage', pagination.current, size);
  69. };
  70. // 回到第一页
  71. const backFirstPage = () => {
  72. pagination.current = 1;
  73. emit('getPage', pagination.current,pagination.pageSize);
  74. };
  75. defineExpose({ backFirstPage });
  76. // 监听total变化
  77. watch(
  78. props,
  79. newData => {
  80. pagination.total = newData.total;
  81. },
  82. { immediate: true, deep: true },
  83. );
  84. // 插槽的实例
  85. const slots = useSlots();
  86. const renderArr = Object.keys(slots);
  87. </script>
  88. <style scoped>
  89. .tablePaganations {
  90. width: 100%;
  91. margin-top: 20px;
  92. }
  93. .ant-table-striped :deep(.table-striped) td {
  94. background-color: #fafafa;
  95. }
  96. </style>