wd-pagination.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view :class="`wd-pager ${customClass}`" :style="customStyle" v-if="!(hideIfOnePage && totalPageNum === 1)">
  3. <view class="wd-pager__content">
  4. <wd-button :plain="modelValue > 1" type="info" size="small" :disabled="modelValue <= 1" custom-class="wd-pager__nav" @click="sub">
  5. <text v-if="!showIcon">{{ prevText || translate('prev') }}</text>
  6. <wd-icon
  7. v-else
  8. :custom-class="`wd-pager__left wd-pager__icon ${modelValue <= 1 ? 'wd-pager__nav--disabled' : 'wd-pager__nav--active'}`"
  9. name="arrow-right"
  10. ></wd-icon>
  11. </wd-button>
  12. <view class="wd-pager__size">
  13. <text class="wd-pager__current">{{ modelValue }}</text>
  14. <text class="wd-pager__separator">/</text>
  15. <text>{{ totalPageNum }}</text>
  16. </view>
  17. <wd-button
  18. :plain="modelValue < totalPageNum"
  19. type="info"
  20. size="small"
  21. :disabled="modelValue >= totalPageNum"
  22. custom-class="wd-pager__nav"
  23. @click="add"
  24. >
  25. <text v-if="!showIcon">{{ nextText || translate('next') }}</text>
  26. <wd-icon
  27. v-else
  28. :custom-class="`wd-pager__icon ${modelValue >= totalPageNum ? 'wd-pager__nav--disabled' : 'wd-pager__nav--active'}`"
  29. name="arrow-right"
  30. ></wd-icon>
  31. </wd-button>
  32. </view>
  33. <view class="wd-pager__message" v-if="showMessage">
  34. <text>{{ translate('page', modelValue) }},</text>
  35. <text v-if="total">{{ translate('total', total) }},</text>
  36. <text>{{ translate('size', pageSize) }}</text>
  37. </view>
  38. </view>
  39. </template>
  40. <script lang="ts">
  41. export default {
  42. name: 'wd-pagination',
  43. options: {
  44. virtualHost: true,
  45. addGlobalClass: true,
  46. styleIsolation: 'shared'
  47. }
  48. }
  49. </script>
  50. <script lang="ts" setup>
  51. import wdIcon from '../wd-icon/wd-icon.vue'
  52. import wdButton from '../wd-button/wd-button.vue'
  53. import { ref, watch } from 'vue'
  54. import { useTranslate } from '../composables/useTranslate'
  55. import { paginationProps } from './types'
  56. const { translate } = useTranslate('pagination')
  57. const props = defineProps(paginationProps)
  58. const emit = defineEmits(['change', 'update:modelValue'])
  59. const totalPageNum = ref<number>(0) // 总页数
  60. watch(
  61. () => props.totalPage,
  62. (newValue) => {
  63. if (!totalPageNum.value && newValue) {
  64. totalPageNum.value = newValue
  65. }
  66. },
  67. { immediate: true, deep: true }
  68. )
  69. watch(
  70. () => props.total,
  71. () => {
  72. updateTotalPage()
  73. },
  74. { immediate: true, deep: true }
  75. )
  76. function add() {
  77. const { modelValue } = props
  78. if (modelValue > totalPageNum.value - 1) {
  79. return
  80. }
  81. emit('change', { value: modelValue + 1 })
  82. emit('update:modelValue', modelValue + 1)
  83. }
  84. function sub() {
  85. const { modelValue } = props
  86. if (modelValue < 2) {
  87. return
  88. }
  89. emit('change', { value: modelValue - 1 })
  90. emit('update:modelValue', modelValue - 1)
  91. }
  92. function updateTotalPage() {
  93. const { total, pageSize } = props
  94. totalPageNum.value = Math.ceil(total / pageSize)
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. @import './index.scss';
  99. </style>