customPagination.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div class="pagination" v-if="!hideOnSinglePage ? true : total !== 1">
  3. <span>{{ showTotal(totalNum) }}</span>
  4. <a-button
  5. type="link"
  6. style="padding: 0; font-size: 14px"
  7. :disabled="isDisabled('leftOutLined')"
  8. @click="previousPage"
  9. >
  10. <template #icon><leftOutlined class="icon" /></template>
  11. </a-button>
  12. <span
  13. v-for="pageIndex in pageRangeArr"
  14. :title="pageIndex"
  15. :key="pageIndex"
  16. class="pageIndex"
  17. @click="jumpCurrentPage(pageIndex)"
  18. :class="{ active: totalNum && pageIndex === currentPage }"
  19. >
  20. {{ pageIndex }}
  21. </span>
  22. <a-button type="link" :disabled="isDisabled('rightOutlined')" style="padding: 0; font-size: 14px" @click="nextPage">
  23. <template #icon><rightOutlined class="icon" /></template>
  24. </a-button>
  25. <a-select
  26. class="sizeChanger"
  27. v-model:value="pageSize"
  28. :size="size"
  29. @change="handleSizeChange"
  30. v-if="isShowSizeChanger"
  31. >
  32. <a-select-option v-for="item in pageSizeOptions" :key="item"> {{ item }}条/页 </a-select-option>
  33. </a-select>
  34. <span v-if="totalPage > 1 && showQuickJumper" class="jumpPage"
  35. >跳至<a-input
  36. v-model:value="inputIndex"
  37. :size="size"
  38. class="toInputIndex"
  39. @blur="() => quickJumpPage(inputIndex)"
  40. @keyup.enter="quickJumpPage(inputIndex)"
  41. />页</span
  42. >
  43. </div>
  44. </template>
  45. <script setup>
  46. import { ref, computed, watch } from 'vue'
  47. import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue'
  48. const props = defineProps({
  49. total: {
  50. type: Number,
  51. required: true,
  52. default: 0
  53. },
  54. pageSize: {
  55. type: Number,
  56. default: 10
  57. },
  58. current: {
  59. type: Number,
  60. default: 1
  61. },
  62. pageSizeOptions: {
  63. type: Array,
  64. default: () => ['10', '20', '30', '40', '50', '200', '500']
  65. },
  66. showQuickJumper: {
  67. type: Boolean,
  68. default: false
  69. },
  70. showSizeChanger: {
  71. type: Boolean
  72. },
  73. hideOnSinglePage: {
  74. type: Boolean,
  75. default: false
  76. },
  77. showTotal: {
  78. type: Function,
  79. default: () => ''
  80. },
  81. size: {
  82. type: String,
  83. default: ''
  84. }
  85. })
  86. const emit = defineEmits(['change', 'showSizeChange'])
  87. const currentPage = ref(props.current)
  88. const pageSize = ref(String(props.pageSize))
  89. const totalNum = ref(props.total)
  90. const inputIndex = ref('')
  91. const isShowSizeChanger = ref(false)
  92. const pageRangeStart = ref(1)
  93. const isDisabled = (type) => {
  94. switch (type) {
  95. case 'leftOutLined':
  96. return currentPage.value === 1
  97. case 'rightOutlined':
  98. return currentPage.value === totalPage.value
  99. default:
  100. break
  101. }
  102. }
  103. const handleSizeChange = (size) => {
  104. pageRangeStart.value = 1
  105. currentPage.value = 1
  106. emit('showSizeChange', currentPage.value, Number(size))
  107. }
  108. watch(
  109. () => props.total,
  110. (val) => {
  111. totalNum.value = val
  112. isShowSizeChanger.value = props.showSizeChanger ? true : val > 50
  113. },
  114. { immediate: true }
  115. )
  116. watch(
  117. () => props.pageSize,
  118. (val) => {
  119. pageSize.value = String(val)
  120. }
  121. )
  122. watch(
  123. () => props.current,
  124. (val) => {
  125. if (val === 1) {
  126. quickJumpPage(1)
  127. }
  128. }
  129. )
  130. const pageRangeEnd = computed(() => Math.min(pageRangeStart.value + 9, totalPage.value))
  131. const pageRangeArr = computed(() => {
  132. const start = pageRangeStart.value
  133. const end = pageRangeEnd.value
  134. const pages = []
  135. for (let i = start; i <= end; i++) {
  136. pages.push(i)
  137. }
  138. return pages
  139. })
  140. const totalPage = computed(() => {
  141. return !totalNum.value ? 1 : Math.ceil(totalNum.value / pageSize.value)
  142. })
  143. // 点击页码
  144. const jumpCurrentPage = (page) => {
  145. // 不重复请求
  146. if (page === currentPage.value) return
  147. if (page === totalPage.value) {
  148. currentPage.value = page
  149. emit('change', currentPage.value)
  150. } else {
  151. if (page >= 1 && page < totalPage.value) {
  152. currentPage.value = page
  153. if ((page <= pageRangeStart.value && page !== 1) || page >= pageRangeEnd.value) {
  154. pageRangeStart.value = Math.max(1, page - 5)
  155. }
  156. emit('change', currentPage.value)
  157. }
  158. }
  159. }
  160. // 上一页
  161. const previousPage = () => {
  162. if (currentPage.value > 1) {
  163. currentPage.value--
  164. if (currentPage.value < pageRangeStart.value) {
  165. pageRangeStart.value = Math.max(1, currentPage.value - 5)
  166. }
  167. emit('change', currentPage.value)
  168. }
  169. }
  170. // 下一页
  171. const nextPage = () => {
  172. if (currentPage.value < totalPage.value) {
  173. currentPage.value++
  174. if (currentPage.value > pageRangeEnd.value && currentPage.value !== totalNum.value) {
  175. pageRangeStart.value = Math.max(1, currentPage.value - 5)
  176. }
  177. emit('change', currentPage.value)
  178. }
  179. }
  180. // 快速跳转
  181. const quickJumpPage = (pageIndex) => {
  182. let index = Number(pageIndex)
  183. let isInteger = Number.isInteger(index)
  184. if (!index || !isInteger) {
  185. inputIndex.value = ''
  186. return
  187. } else {
  188. pageRangeStart.value = 1
  189. if (index >= 1 && index < totalPage.value) {
  190. pageRangeStart.value = Math.max(1, index - 5)
  191. currentPage.value = index
  192. } else {
  193. if (index >= totalPage.value) {
  194. if (totalPage.value < 10) {
  195. pageRangeStart.value = 1
  196. } else {
  197. pageRangeStart.value = totalPage.value - (totalPage.value % 10) - 4
  198. }
  199. currentPage.value = totalPage.value
  200. } else if (index < 1) {
  201. pageRangeStart.value = 1
  202. currentPage.value = 1
  203. }
  204. }
  205. inputIndex.value = ''
  206. }
  207. emit('change', currentPage.value)
  208. }
  209. </script>
  210. <style scoped lang="less">
  211. .pagination {
  212. user-select: none;
  213. display: flex;
  214. align-items: center;
  215. flex-wrap: wrap;
  216. }
  217. .icon {
  218. font-size: 12px;
  219. }
  220. .ant-btn-link {
  221. color: #212325;
  222. font-size: 12px;
  223. &:hover {
  224. color: #4458fe;
  225. }
  226. }
  227. .ant-btn-link[disabled],
  228. .ant-btn-link[disabled]:hover,
  229. .ant-btn-link[disabled]:focus,
  230. .ant-btn-link[disabled]:active {
  231. color: rgba(0, 0, 0, 0.25);
  232. }
  233. .pagination span.active {
  234. color: #4458fe;
  235. }
  236. :deep(.ant-select select) {
  237. border-radius: 6px;
  238. }
  239. .pageIndex {
  240. display: inline-block;
  241. min-width: 32px;
  242. height: 30px;
  243. line-height: 30px;
  244. text-align: center;
  245. cursor: pointer;
  246. border-radius: 6px;
  247. font-size: 14px;
  248. padding: 0 6px;
  249. }
  250. .pageIndex:hover {
  251. color: #4458fe;
  252. }
  253. .sizeChanger {
  254. margin-left: 8px;
  255. }
  256. .jumpPage {
  257. margin-left: 8px;
  258. }
  259. .toInputIndex {
  260. width: 48px;
  261. margin: 0 8px;
  262. }
  263. .disabledBgc {
  264. color: rgba(0, 0, 0, 0.25);
  265. cursor: not-allowed;
  266. }
  267. </style>