CommonTable.vue 2.5 KB

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