[修复] 分页组件中的最大页码数量计算时机导致的显示问题

This commit is contained in:
castleiMac 2022-01-08 11:24:17 +08:00
parent 3b337aeba1
commit 797e24ee16
2 changed files with 64 additions and 55 deletions

View File

@ -7,7 +7,7 @@
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total" :show-page="showPage"></lay-page> <lay-page :limit="limit" @limit="limit = $event" :total="total" :show-page="showPage"></lay-page>
</template> </template>
<script> <script>
@ -37,7 +37,7 @@ export default {
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total"></lay-page> <lay-page :limit="limit" @limit="limit = $event" :total="total"></lay-page>
</template> </template>
<script> <script>
@ -65,7 +65,7 @@ export default {
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total"> <lay-page :limit="limit" @limit="limit = $event" :total="total">
<template v-slot:prev></template> <template v-slot:prev></template>
<template v-slot:next></template> <template v-slot:next></template>
</lay-page> </lay-page>
@ -96,11 +96,11 @@ export default {
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total" :show-page="showPage" theme="red"></lay-page> <lay-page :limit="limit" @limit="limit = $event" :total="total" :show-page="showPage" theme="red"></lay-page>
<br> <br>
<lay-page :limit="limit" :total="total" :show-page="showPage" theme="blue"></lay-page> <lay-page :limit="limit" @limit="limit = $event" :total="total" :show-page="showPage" theme="blue"></lay-page>
<br> <br>
<lay-page :limit="limit" :total="total" :show-page="showPage" theme="orange"></lay-page> <lay-page :limit="limit" @limit="limit = $event" :total="total" :show-page="showPage" theme="orange"></lay-page>
</template> </template>
<script> <script>
@ -124,7 +124,7 @@ export default {
::: :::
::: title 完整分页 ::: title 指定分页容量
::: :::
::: demo ::: demo
@ -222,13 +222,13 @@ export default {
::: :::
::: title 切换事件 ::: title 页码切换事件(jump)
::: :::
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total" @jump="jump" :show-page="showSkip"></lay-page> <lay-page :limit="limit" :total="total" @jump="jump" @limit="limit = $event" :show-page="showSkip"></lay-page>
</template> </template>
<script> <script>
@ -270,6 +270,8 @@ export default {
| showLimit | 显示每页数量 | `false` | | showLimit | 显示每页数量 | `false` |
| showRefresh | 显示刷新按钮 | `false` | | showRefresh | 显示刷新按钮 | `false` |
| showSkip | 显示跳转 | `false` | | showSkip | 显示跳转 | `false` |
| pages | 显示切页按钮数量 | `10` |
| limits | 切换每页数量的选择项 | `[10,20,30,40,50]` |
::: :::
@ -281,6 +283,7 @@ export default {
| 事件 | 描述 | 参数 | | 事件 | 描述 | 参数 |
| ---- | -------- | --------------------- | | ---- | -------- | --------------------- |
| jump | 切换回调 | { current: 当前页面 } | | jump | 切换回调 | { current: 当前页面 } |
| limit | 每页数量变化 | 变化后的值 |
::: :::

View File

@ -1,77 +1,77 @@
<template> <template>
<div class="layui-box layui-laypage layui-laypage-default"> <div class="layui-box layui-laypage layui-laypage-default">
<span v-if="showCount" class="layui-laypage-count" <span v-if="showCount" class="layui-laypage-count"
> {{ total }} {{}} </span > {{ total }} {{ maxPage }} </span
><a >
<a
href="javascript:;" href="javascript:;"
class="layui-laypage-prev" class="layui-laypage-prev"
:class="[currentPage === 1 ? 'layui-disabled' : '']" :class="[currentPage === 1 ? 'layui-disabled' : '']"
@click="prev()" @click="prev()"
><slot v-if="slots.prev" name="prev"></slot>
<template v-else>上一页</template></a
> >
<slot v-if="slots.prev" name="prev"></slot>
<template v-else>上一页</template>
</a>
<template v-if="showPage"> <template v-if="showPage">
<template v-for="index of totalPage" :key="index"> <template v-for="index of totalPage" :key="index">
<span v-if="index === currentPage" class="layui-laypage-curr" <span v-if="index === currentPage" class="layui-laypage-curr">
><em <em
class="layui-laypage-em" class="layui-laypage-em"
:class="[theme ? 'layui-bg-' + theme : '']" :class="[theme ? 'layui-bg-' + theme : '']"
></em ></em>
><em>{{ index }}</em></span <em>{{ index }}</em>
> </span>
<a v-else href="javascript:;" @click="jump(index)"> <a v-else href="javascript:;" @click="jump(index)">{{ index }}</a>
{{ index }}
</a>
</template> </template>
</template> </template>
<a <a
href="javascript:;" href="javascript:;"
class="layui-laypage-next" class="layui-laypage-next"
:class="[currentPage === totalPage ? 'layui-disabled' : '']" :class="[currentPage === maxPage ? 'layui-disabled' : '']"
@click="next()" @click="next()"
><slot v-if="slots.next" name="next"></slot> >
<template v-else>下一页</template></a <slot v-if="slots.next" name="next"></slot>
><span v-if="showLimit" class="layui-laypage-limits" <template v-else>下一页</template>
><select v-model="inlimit"> </a>
<option value="10">10 /</option> <span v-if="showLimit" class="layui-laypage-limits">
<option value="20">20 /</option> <select v-model="inlimit">
<option value="30">30 /</option> <option v-for="val of limits" :key="val" :value="val">
<option value="40">40 /</option> {{ val }} /
<option value="50">50 /</option> </option>
</select></span </select>
><a v-if="showRefresh" href="javascript:;" class="layui-laypage-refresh" </span>
><i class="layui-icon layui-icon-refresh"></i></a <a v-if="showRefresh" href="javascript:;" class="layui-laypage-refresh">
><span v-if="showSkip" class="layui-laypage-skip" <i class="layui-icon layui-icon-refresh"></i>
>到第<input </a>
<span v-if="showSkip" class="layui-laypage-skip">
到第
<input
v-model="currentPageShow" v-model="currentPageShow"
type="number" type="number"
class="layui-input layui-input-number" class="layui-input layui-input-number"
/><button type="button" class="layui-laypage-btn" @click="jumpPage()"> />
确定 <button
</button></span type="button"
class="layui-laypage-btn"
@click="jumpPage()"
:disabled="currentPageShow > maxPage"
> >
确定
</button>
</span>
</div> </div>
</template> </template>
<script setup name="LayPage" lang="ts"> <script setup name="LayPage" lang="ts">
import { import { defineProps, Ref, ref, watch, useSlots, computed } from "vue";
defineProps,
Ref,
ref,
watch,
useSlots,
computed,
ComputedRef,
} from "vue";
const slots = useSlots(); const slots = useSlots();
const props = withDefaults( const props = withDefaults(
defineProps<{ defineProps<{
total: number; total: number;
limit?: number; limit: number;
pages?: number;
theme?: string; theme?: string;
showPage?: boolean | string; showPage?: boolean | string;
showSkip?: boolean | string; showSkip?: boolean | string;
@ -79,10 +79,11 @@ const props = withDefaults(
showLimit?: boolean | string; showLimit?: boolean | string;
showInput?: boolean | string; showInput?: boolean | string;
showRefresh?: boolean | string; showRefresh?: boolean | string;
pages?: number;
limits?: number[];
}>(), }>(),
{ {
limit: 10, limit: 10,
pages: 10,
theme: "green", theme: "green",
showPage: false, showPage: false,
showSkip: false, showSkip: false,
@ -90,8 +91,11 @@ const props = withDefaults(
showLimit: true, showLimit: true,
showInput: false, showInput: false,
showRefresh: false, showRefresh: false,
pages: 10,
limits: () => [10, 20, 30, 40, 50],
} }
); );
const limits = ref(props.limits);
const pages = props.pages / 2; const pages = props.pages / 2;
const inlimit = computed({ const inlimit = computed({
get() { get() {
@ -101,8 +105,9 @@ const inlimit = computed({
emit("limit", v); emit("limit", v);
}, },
}); });
const maxPage = ref(Math.ceil(props.total / props.limit)); const maxPage = ref(0);
const totalPage = computed(() => { const totalPage = computed(() => {
maxPage.value = Math.ceil(props.total / props.limit);
let r: number[] = [], let r: number[] = [],
start = start =
maxPage.value <= props.pages maxPage.value <= props.pages
@ -120,6 +125,7 @@ const totalPage = computed(() => {
}); });
const currentPage: Ref<number> = ref(1); const currentPage: Ref<number> = ref(1);
const currentPageShow: Ref<number> = ref(currentPage.value); const currentPageShow: Ref<number> = ref(currentPage.value);
const emit = defineEmits(["jump", "limit"]); const emit = defineEmits(["jump", "limit"]);
const prev = function () { const prev = function () {
@ -130,7 +136,7 @@ const prev = function () {
}; };
const next = function () { const next = function () {
if (currentPage.value === totalPage.value[totalPage.value.length - 1]) { if (currentPage.value === maxPage.value) {
return; return;
} }
currentPage.value++; currentPage.value++;
@ -146,7 +152,7 @@ const jumpPage = function () {
watch(inlimit, function () { watch(inlimit, function () {
currentPage.value = 1; currentPage.value = 1;
// totalPage.value = Math.ceil(props.total / inlimit.value); // maxPage.value = Math.ceil(props.total / inlimit.value);
}); });
watch(currentPage, function () { watch(currentPage, function () {