[修复][新增]给page组件增加pages参数,用于限定页数过多情况下的按钮显示数量

This commit is contained in:
castle 2021-12-27 15:16:02 +08:00
parent e49fb0c9d3
commit e803274ba6
2 changed files with 52 additions and 39 deletions

View File

@ -127,7 +127,7 @@ export default {
::: demo ::: demo
<template> <template>
<lay-page :limit="limit" :total="total" :show-count="showCount" :show-page="showPage" :show-limit="showLimit" :show-refresh="showRefresh" showSkip="showSkip"></lay-page> <lay-page :limit="limit" :total="9999" :show-count="showCount" :show-page="showPage" :show-limit="showLimit" :show-refresh="showRefresh" showSkip="showSkip"></lay-page>
</template> </template>
<script> <script>
@ -136,8 +136,8 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const limit = ref(20) const limit = ref(5)
const total = ref(100) const total = ref(9999)
const showCount = ref(true) const showCount = ref(true)
const showPage = ref(true) const showPage = ref(true)
const showLimit = ref(true) const showLimit = ref(true)
@ -207,6 +207,7 @@ export default {
| showLimit | 显示每页数量 | `false` | | showLimit | 显示每页数量 | `false` |
| showRefresh | 显示刷新按钮 | `false` | | showRefresh | 显示刷新按钮 | `false` |
| showSkip | 显示跳转 | `false` | | showSkip | 显示跳转 | `false` |
| pages | 显示切页按钮数量 | `10` |
::: :::

View File

@ -1,60 +1,56 @@
<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"> {{ total }} </span <span v-if="showCount" class="layui-laypage-count"> {{ total }} </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="[theme ? 'layui-bg-' + theme : '']"></em>
class="layui-laypage-em" <em>{{ index }}</em>
:class="[theme ? 'layui-bg-' + theme : '']" </span>
></em <a v-else href="javascript:;" @click="jump(index)">{{ index }}</a>
><em>{{ index }}</em></span
>
<a v-else href="javascript:;" @click="jump(index)">
{{ 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>
<span v-if="showLimit" class="layui-laypage-limits">
<select v-model="inlimit">
<option value="10">10 /</option> <option value="10">10 /</option>
<option value="20">20 /</option> <option value="20">20 /</option>
<option value="30">30 /</option> <option value="30">30 /</option>
<option value="40">40 /</option> <option value="40">40 /</option>
<option value="50">50 /</option> <option value="50">50 /</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>
v-model="currentPageShow" <span v-if="showSkip" class="layui-laypage-skip">
type="number" 到第
class="layui-input layui-input-number" <input v-model="currentPageShow" type="number" class="layui-input layui-input-number" />
/><button type="button" class="layui-laypage-btn" @click="jumpPage()"> <button type="button" class="layui-laypage-btn" @click="jumpPage()">确定</button>
确定 </span>
</button></span
>
</div> </div>
</template> </template>
<script setup name="LayPage" lang="ts"> <script setup name="LayPage" lang="ts">
import { defineProps, Ref, ref, watch, useSlots } from "vue"; import { defineProps, Ref, ref, watch, useSlots, computed } from "vue";
const slots = useSlots(); const slots = useSlots();
@ -69,6 +65,7 @@ const props = withDefaults(
showLimit?: boolean | string; showLimit?: boolean | string;
showInput?: boolean | string; showInput?: boolean | string;
showRefresh?: boolean | string; showRefresh?: boolean | string;
pages?: number
}>(), }>(),
{ {
limit: 10, limit: 10,
@ -79,11 +76,26 @@ const props = withDefaults(
showLimit: true, showLimit: true,
showInput: false, showInput: false,
showRefresh: false, showRefresh: false,
pages: 10
} }
); );
const pages = props.pages / 2
const inlimit = ref(props.limit); const inlimit = ref(props.limit);
const totalPage = ref(Math.ceil(props.total / inlimit.value)); const maxPage = ref(Math.ceil(props.total / props.limit));
const totalPage = computed(() => {
let r: number[] = [], end = maxPage.value
if (currentPage.value <= pages) {
end = props.pages + 1
} else if (currentPage.value + pages > end) {
} else {
end = currentPage.value + pages
}
for (let i = currentPage.value > pages ? currentPage.value - pages : 1; i < end; i++) {
r.push(i)
}
return r;
})
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);
@ -97,7 +109,7 @@ const prev = function () {
}; };
const next = function () { const next = function () {
if (currentPage.value === totalPage.value) { if (currentPage.value === maxPage.value) {
return; return;
} }
currentPage.value++; currentPage.value++;
@ -113,7 +125,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 () {