pages.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. class VirtualPagination {
  2. constructor(data, itemsPerPage = 10) {
  3. console.log(data);
  4. this.originalData = data;
  5. this.currentData = [...data];
  6. this.itemsPerPage = itemsPerPage;
  7. this.currentPage = 1;
  8. this.totalPages = Math.ceil(data.length / itemsPerPage);
  9. }
  10. // 获取当前页数据
  11. getCurrentPageData() {
  12. const start = (this.currentPage - 1) * this.itemsPerPage;
  13. const end = start + this.itemsPerPage;
  14. return this.currentData.slice(start, end);
  15. }
  16. // 跳转到指定页
  17. goToPage(page) {
  18. console.log(page,'this.totalPages',this.totalPages);
  19. if (page >= 1 && page <= this.totalPages) {
  20. this.currentPage = page;
  21. return this.getCurrentPageData();
  22. }else{
  23. return;
  24. }
  25. }
  26. // 上一页
  27. prevPage() {
  28. return this.goToPage(this.currentPage - 1);
  29. }
  30. // 下一页
  31. nextPage() {
  32. return this.goToPage(this.currentPage + 1);
  33. }
  34. // 设置每页显示数量
  35. setItemsPerPage(count) {
  36. this.itemsPerPage = count;
  37. this.totalPages = Math.ceil(this.currentData.length / count);
  38. this.currentPage = 1; // 重置到第一页
  39. }
  40. // 过滤数据
  41. filterData(filterFn) {
  42. this.currentData = this.originalData.filter(filterFn);
  43. this.totalPages = Math.ceil(this.currentData.length / this.itemsPerPage);
  44. this.currentPage = 1; // 重置到第一页
  45. return this.getCurrentPageData();
  46. }
  47. // 排序数据
  48. sortData(compareFn) {
  49. this.currentData = [...this.originalData].sort(compareFn);
  50. return this.getCurrentPageData();
  51. }
  52. }
  53. export { VirtualPagination };