UNPKG

lc-vue-auto-pagination

Version:
130 lines (129 loc) 4.37 kB
(function(){"use strict";try{if(typeof document<"u"){var a=document.createElement("style");a.appendChild(document.createTextNode(":root{--auto-pagination-footer-margin-top: 16px}.auto-pagination[data-v-e9dd1a97]{height:100%;display:flex;flex-direction:column;overflow:hidden}.auto-pagination .auto-pagination_data-container[data-v-e9dd1a97]{flex:1;overflow:auto}.auto-pagination .auto-pagination_main[data-v-e9dd1a97]{margin-top:var(--auto-pagination-footer-margin-top);flex:none}.auto-pagination .el-pagination{justify-content:flex-end}")),document.head.appendChild(a)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})(); import { defineComponent as m, ref as s, watch as x, onMounted as S, nextTick as D, resolveComponent as M, resolveDirective as k, openBlock as r, createElementBlock as d, withDirectives as w, renderSlot as C, createVNode as B, createCommentVNode as F } from "vue"; const N = { class: "auto-pagination" }, I = { class: "auto-pagination_data-container" }, A = { key: 0, class: "auto-pagination_main" }, E = /* @__PURE__ */ m({ __name: "auto-pagination", props: { fetchData: { type: Function, default: () => ({ list: [], total: 0 }) }, pageSizes: { type: Array, default: () => [ 10, 20, 50, 100 ] }, defaultPageSize: { type: Number, default: 20 }, prePageChanged: { type: Function, default: () => !0 }, noPagination: { type: Boolean, default: !1 }, autoInit: { type: Boolean, default: !1 } }, setup(u, { expose: g }) { const l = u, o = s(0), t = s(l.defaultPageSize), e = s(1), c = s([]), v = s(!1), p = s(1); x(() => e.value, async (a, i) => { if (p.value === a) return; await l.prePageChanged(a, i) === !0 ? (p.value = a, n()) : e.value = i; }), S(() => { l.autoInit === !0 && f(); }); const P = (a) => (e.value - 1) * t.value + a + 1, h = (a) => { t.value = a, n(); }, n = async () => { if (v.value = !0, l.noPagination === !1) { let { list: a, total: i } = await l.fetchData({ pageNo: e.value, pageSize: t.value }); if (a.length > 0 || e.value === 1) c.value = [], await D(), c.value = a, o.value = i, v.value = !1; else { if (e.value * t.value <= i) throw v.value = !1, new Error("接口返回数据异常"); e.value = Math.ceil(i / t.value), n(); } } else { let { list: a } = await l.fetchData(); c.value = a, v.value = !1; } }, f = () => { n(); }; return g({ refresh: f, goFirstPage: () => { e.value = 1, n(); }, goLastPage: () => { e.value = Math.ceil(o.value / t.value), n(); }, goNextPage: () => { Math.ceil(o.value / t.value) === e.value || (e.value = e.value + 1, n()); }, goPrevPage: () => { e.value === 1 || (e.value = e.value - 1, n()); }, go: (a) => { a !== e.value && (a > Math.ceil(o.value / t.value) ? e.value = Math.ceil(o.value / t.value) : e.value = a, n()); } }), (a, i) => { const _ = M("el-pagination"), y = k("loading"); return r(), d("div", N, [ w((r(), d("div", I, [ C(a.$slots, "default", { data: c.value, total: o.value, pageSize: t.value, currentPage: e.value, indexMethod: P }, void 0, !0) ])), [ [y, v.value] ]), u.noPagination === !1 ? (r(), d("div", A, [ B(_, { "current-page": e.value, "onUpdate:currentPage": i[0] || (i[0] = (z) => e.value = z), background: "", "page-sizes": u.pageSizes, "page-size": t.value, layout: "total, sizes, prev, pager, next", total: o.value, small: "", "onUpdate:pageSize": h }, null, 8, ["current-page", "page-sizes", "page-size", "total"]) ])) : F("", !0) ]); }; } }); const L = (u, g) => { const l = u.__vccOpts || u; for (const [o, t] of g) l[o] = t; return l; }, q = /* @__PURE__ */ L(E, [["__scopeId", "data-v-e9dd1a97"]]); export { q as AutoPagination };