index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { Searchs } from '@/utils/searchs'
  2. import { NDataTable, 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. pageSizes: {
  43. type: Array as PropType<any>,
  44. default: () => [10, 20, 30, 40]
  45. },
  46. pageSlot: {
  47. type: Number,
  48. default: 9
  49. }
  50. },
  51. emits: ['update:page', 'update:pageSize', 'list'],
  52. setup(props, { slots, attrs, 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:page', 1)
  68. emit('update:pageSize', pageSize)
  69. emit('list')
  70. syncStore()
  71. }
  72. onMounted(() => {
  73. if (props.sync) {
  74. const searchs = new Searchs(props.saveKey || route.path)
  75. const active = searchs.get(props.saveKey || route.path)
  76. state.pageInformation = active
  77. if (active && active.page) {
  78. for (const key in active.page) {
  79. if (active.page.hasOwnProperty(key) && ['page', 'pageSize'].includes(key)) {
  80. const item = active.page[key]
  81. const tempKey = `update:${key}` as any
  82. emit(tempKey, item)
  83. }
  84. }
  85. }
  86. if (props.saveKey) {
  87. searchs.update(route.path, undefined, 'bind')
  88. }
  89. }
  90. window.addEventListener('watchStorage', watchStorage)
  91. })
  92. onUnmounted(() => {
  93. window.removeEventListener('watchStorage', watchStorage)
  94. })
  95. watch(
  96. () => props.pageSize,
  97. () => {
  98. pageCount.value = Math.ceil(props.pageTotal / props.pageSize)
  99. syncStore()
  100. }
  101. )
  102. watch(
  103. () => props.page,
  104. () => {
  105. syncStore()
  106. }
  107. )
  108. watch(
  109. () => props.pageTotal,
  110. () => {
  111. pageCount.value = Math.ceil(props.pageTotal / props.pageSize)
  112. syncStore()
  113. }
  114. )
  115. let currentPage = computed({
  116. get() {
  117. return props.page
  118. },
  119. set(val) {
  120. emit('update:page', val)
  121. }
  122. })
  123. const syncStore = () => {
  124. if (props.sync) {
  125. const searchs = new Searchs(props.saveKey || route.path)
  126. searchs.update(
  127. {
  128. page: props.page,
  129. pageCount: pageCount.value,
  130. pageSize: props.pageSize,
  131. saveKey: props.saveKey
  132. },
  133. undefined,
  134. 'page'
  135. )
  136. }
  137. }
  138. const watchStorage = () => {
  139. let page =
  140. state.pageInformation && state.pageInformation.page ? state.pageInformation.page : null
  141. currentPage = page && page.page ? page.page : 1
  142. }
  143. return () => (
  144. <NPagination
  145. style={{
  146. marginTop: '12px',
  147. justifyContent: 'flex-end'
  148. }}
  149. v-model:page={props.page}
  150. displayOrder={['quick-jumper', 'pages', 'size-picker']}
  151. pageCount={pageCount.value}
  152. showQuickJumper
  153. showSizePicker
  154. pageSize={props.pageSize}
  155. prefix={() => `共 ${props.pageTotal} 条`}
  156. pageSizes={props.pageSizes}
  157. onUpdatePage={onUpdatePage}
  158. onUpdatePageSize={onUpdatePageSize}
  159. pageSlot={props.pageSlot}
  160. ></NPagination>
  161. )
  162. }
  163. })