index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Searchs } from '@/utils/searchs';
  2. import { NPagination } from 'naive-ui';
  3. import {
  4. PropType,
  5. computed,
  6. defineComponent,
  7. onMounted,
  8. onUnmounted,
  9. reactive,
  10. ref,
  11. watch
  12. } from 'vue';
  13. import { useRoute } from 'vue-router';
  14. import styles from './index.module.less';
  15. export default defineComponent({
  16. name: 'table-container',
  17. props: {
  18. page: {
  19. type: Number,
  20. default: 1,
  21. required: true
  22. },
  23. pageSize: {
  24. type: Number,
  25. default: 10
  26. },
  27. pageTotal: {
  28. type: Number,
  29. default: 0
  30. },
  31. saveKey: {
  32. type: String,
  33. default: ''
  34. },
  35. sync: {
  36. type: Boolean,
  37. default: false
  38. },
  39. checkedRowKeysRef: {
  40. type: Object
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false
  45. },
  46. pageSizes: {
  47. type: Array as PropType<any>,
  48. default: () => [10, 20, 30, 40]
  49. }
  50. },
  51. emits: ['update:page', 'update:pageSize', 'list'],
  52. setup(props, { emit }) {
  53. const route = useRoute();
  54. const state = reactive({
  55. pageInformation: null as any
  56. });
  57. const pageCount = ref(0);
  58. pageCount.value = Math.ceil(props.pageTotal / props.pageSize);
  59. // 当前页发生改变时的回调函数
  60. const onUpdatePage = (page: number) => {
  61. emit('update:page', page);
  62. emit('list');
  63. syncStore();
  64. };
  65. // 当前分页大小发生改变时的回调函数
  66. const onUpdatePageSize = (pageSize: number) => {
  67. emit('update:pageSize', pageSize);
  68. emit('list');
  69. syncStore();
  70. };
  71. onMounted(() => {
  72. if (props.sync) {
  73. const searchs = new Searchs(props.saveKey || route.path);
  74. const active = searchs.get(props.saveKey || route.path);
  75. state.pageInformation = active;
  76. if (active && active.page) {
  77. for (const key in active.page) {
  78. if (
  79. // eslint-disable-next-line no-prototype-builtins
  80. active.page.hasOwnProperty(key) &&
  81. ['page', 'pageSize'].includes(key)
  82. ) {
  83. const item = active.page[key];
  84. const tempKey = `update:${key}` as any;
  85. emit(tempKey, item);
  86. }
  87. }
  88. }
  89. if (props.saveKey) {
  90. searchs.update(route.path, undefined, 'bind');
  91. }
  92. }
  93. window.addEventListener('watchStorage', watchStorage);
  94. });
  95. onUnmounted(() => {
  96. window.removeEventListener('watchStorage', watchStorage);
  97. });
  98. watch(
  99. () => props.pageSize,
  100. () => {
  101. pageCount.value = Math.ceil(props.pageTotal / props.pageSize);
  102. syncStore();
  103. }
  104. );
  105. watch(
  106. () => props.page,
  107. () => {
  108. syncStore();
  109. }
  110. );
  111. watch(
  112. () => props.pageTotal,
  113. () => {
  114. pageCount.value = Math.ceil(props.pageTotal / props.pageSize);
  115. syncStore();
  116. }
  117. );
  118. const currentPage = computed({
  119. get() {
  120. return props.page;
  121. },
  122. set(val) {
  123. emit('update:page', val);
  124. }
  125. });
  126. const syncStore = () => {
  127. if (props.sync) {
  128. const searchs = new Searchs(props.saveKey || route.path);
  129. searchs.update(
  130. {
  131. page: props.page,
  132. pageCount: pageCount.value,
  133. pageSize: props.pageSize,
  134. saveKey: props.saveKey
  135. },
  136. undefined,
  137. 'page'
  138. );
  139. }
  140. };
  141. const watchStorage = () => {
  142. const page =
  143. state.pageInformation && state.pageInformation.page
  144. ? state.pageInformation.page
  145. : null;
  146. currentPage.value = page && page.page ? page.page : 1;
  147. };
  148. return () => (
  149. <NPagination
  150. disabled={props.disabled}
  151. class={styles.pagination}
  152. v-model:page={props.page}
  153. displayOrder={['quick-jumper', 'pages', 'size-picker']}
  154. pageCount={pageCount.value}
  155. showQuickJumper
  156. showSizePicker
  157. pageSize={props.pageSize}
  158. prefix={() => `共 ${props.pageTotal} 条`}
  159. pageSizes={props.pageSizes}
  160. onUpdatePage={onUpdatePage}
  161. onUpdatePageSize={onUpdatePageSize}></NPagination>
  162. );
  163. }
  164. });