| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- class VirtualPagination {
- constructor(data, itemsPerPage = 10) {
- console.log(data);
- this.originalData = data;
- this.currentData = [...data];
- this.itemsPerPage = itemsPerPage;
- this.currentPage = 1;
- this.totalPages = Math.ceil(data.length / itemsPerPage);
- }
- // 获取当前页数据
- getCurrentPageData() {
- const start = (this.currentPage - 1) * this.itemsPerPage;
- const end = start + this.itemsPerPage;
- return this.currentData.slice(start, end);
- }
- // 跳转到指定页
- goToPage(page) {
- console.log(page,'this.totalPages',this.totalPages);
- if (page >= 1 && page <= this.totalPages) {
- this.currentPage = page;
- return this.getCurrentPageData();
- }else{
- return;
- }
-
- }
- // 上一页
- prevPage() {
- return this.goToPage(this.currentPage - 1);
- }
- // 下一页
- nextPage() {
- return this.goToPage(this.currentPage + 1);
- }
- // 设置每页显示数量
- setItemsPerPage(count) {
- this.itemsPerPage = count;
- this.totalPages = Math.ceil(this.currentData.length / count);
- this.currentPage = 1; // 重置到第一页
- }
- // 过滤数据
- filterData(filterFn) {
- this.currentData = this.originalData.filter(filterFn);
- this.totalPages = Math.ceil(this.currentData.length / this.itemsPerPage);
- this.currentPage = 1; // 重置到第一页
- return this.getCurrentPageData();
- }
- // 排序数据
- sortData(compareFn) {
- this.currentData = [...this.originalData].sort(compareFn);
- return this.getCurrentPageData();
- }
- }
- export { VirtualPagination };
|