cl-pagination.uvue 4.3 KB

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