cl-pagination.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <view class="cl-pagination">
  3. <view
  4. class="cl-pagination__prev"
  5. :class="[
  6. {
  7. 'is-disabled': value == 1,
  8. 'is-dark': isDark
  9. },
  10. pt.item?.className,
  11. pt.prev?.className
  12. ]"
  13. @tap="prev"
  14. >
  15. <slot name="prev" :disabled="value == 1">
  16. <cl-icon name="arrow-left-s-line"></cl-icon>
  17. </slot>
  18. </view>
  19. <view
  20. v-for="(item, index) in list"
  21. :key="index"
  22. class="cl-pagination__item"
  23. :class="[
  24. {
  25. 'is-active': item == value,
  26. 'is-dark': isDark
  27. },
  28. pt.item?.className
  29. ]"
  30. @tap="toPage(item)"
  31. >
  32. <text
  33. class="cl-pagination__item-text"
  34. :class="{
  35. 'is-active': item == value,
  36. 'is-dark': isDark
  37. }"
  38. >{{ item }}</text
  39. >
  40. </view>
  41. <view
  42. class="cl-pagination__next"
  43. :class="[
  44. {
  45. 'is-disabled': value == totalPage,
  46. 'is-dark': isDark
  47. },
  48. pt.item?.className,
  49. pt.next?.className
  50. ]"
  51. @tap="next"
  52. >
  53. <slot name="next" :disabled="value == totalPage">
  54. <cl-icon name="arrow-right-s-line"></cl-icon>
  55. </slot>
  56. </view>
  57. </view>
  58. </template>
  59. <script setup lang="ts">
  60. import type { PassThroughProps } from "../../types";
  61. import { isDark, parsePt } from "@/cool";
  62. import { computed, ref, watch } from "vue";
  63. defineOptions({
  64. name: "cl-pagination"
  65. });
  66. const props = defineProps({
  67. pt: {
  68. type: Object,
  69. default: () => ({})
  70. },
  71. modelValue: {
  72. type: Number,
  73. default: 1
  74. },
  75. total: {
  76. type: Number,
  77. default: 0
  78. },
  79. size: {
  80. type: Number,
  81. default: 10
  82. }
  83. });
  84. const emit = defineEmits(["update:modelValue", "change"]);
  85. // 透传样式类型定义
  86. type PassThrough = {
  87. className?: string;
  88. item?: PassThroughProps;
  89. prev?: PassThroughProps;
  90. next?: PassThroughProps;
  91. };
  92. // 解析透传样式配置
  93. const pt = computed(() => parsePt<PassThrough>(props.pt));
  94. // 计算总页数,根据总数和每页大小向上取整
  95. const totalPage = computed(() => Math.ceil(props.total / props.size));
  96. // 绑定值
  97. const value = ref(props.modelValue);
  98. // 计算分页列表,根据当前页码和总页数生成显示的分页按钮
  99. const list = computed(() => {
  100. const total = totalPage.value;
  101. const list: (number | string)[] = [];
  102. if (total <= 7) {
  103. // 总页数小于等于7,显示所有页码
  104. for (let i = 1; i <= total; i++) {
  105. list.push(i);
  106. }
  107. } else {
  108. // 总是显示第一页
  109. list.push(1);
  110. if (value.value <= 4) {
  111. // 当前页在前面: 1 2 3 4 5 ... 100
  112. for (let i = 2; i <= 5; i++) {
  113. list.push(i);
  114. }
  115. list.push("...");
  116. list.push(total);
  117. } else if (value.value >= total - 3) {
  118. // 当前页在后面: 1 ... 96 97 98 99 100
  119. list.push("...");
  120. for (let i = total - 4; i <= total; i++) {
  121. list.push(i);
  122. }
  123. } else {
  124. // 当前页在中间: 1 ... 4 5 6 ... 100
  125. list.push("...");
  126. for (let i = value.value - 1; i <= value.value + 1; i++) {
  127. list.push(i);
  128. }
  129. list.push("...");
  130. list.push(total);
  131. }
  132. }
  133. return list;
  134. });
  135. // 跳转到指定页面,处理页码点击事件
  136. function toPage(item: number | string) {
  137. // 忽略省略号点击
  138. if (item == "..." || typeof item !== "number") return;
  139. // 边界检查,确保页码在有效范围内
  140. if (typeof item == "number") {
  141. if (item > totalPage.value) return;
  142. if ((item as number) < 1) return;
  143. }
  144. value.value = item;
  145. // 触发双向绑定更新和变化事件
  146. emit("update:modelValue", item);
  147. emit("change", item);
  148. }
  149. // 跳转到上一页
  150. function prev() {
  151. toPage(value.value - 1);
  152. }
  153. // 跳转到下一页
  154. function next() {
  155. toPage(value.value + 1);
  156. }
  157. watch(
  158. computed(() => props.modelValue),
  159. (val: number) => {
  160. value.value = val;
  161. },
  162. {
  163. immediate: true
  164. }
  165. );
  166. defineExpose({
  167. prev,
  168. next
  169. });
  170. </script>
  171. <style lang="scss" scoped>
  172. .cl-pagination {
  173. @apply flex flex-row justify-center w-full;
  174. &__prev,
  175. &__next,
  176. &__item {
  177. @apply flex flex-row items-center justify-center bg-surface-100 rounded-lg;
  178. height: 60rpx;
  179. width: 60rpx;
  180. &.is-disabled {
  181. @apply opacity-50;
  182. }
  183. &.is-dark {
  184. @apply bg-surface-700 text-white;
  185. }
  186. }
  187. &__item {
  188. margin: 0 5rpx;
  189. &.is-active {
  190. @apply bg-primary-500;
  191. }
  192. }
  193. &__prev {
  194. margin-right: 5rpx;
  195. }
  196. &__next {
  197. margin-left: 5rpx;
  198. }
  199. &__item-text {
  200. @apply text-md text-surface-700;
  201. &.is-dark,
  202. &.is-active {
  203. @apply text-white;
  204. }
  205. }
  206. }
  207. </style>