封装个js分页插件

ops/2024/10/9 15:14:37/

javascript">// 分页插件类
class PaginationPlugin {constructor(fetchDataURL, options = {}) {this.fetchDataURL = fetchDataURL;this.options = {containerId: options.containerId || 'paginationContainer',dataSizeAttr: options.dataSizeAttr || 'toatalsize', // 修改为实际API返回的数据属性名pageSizeAttr: options.pageSizeAttr || 'size',onPageChange: options.onPageChange || (() => {}),};this.paginationData = {};this.totalPages=0;}async init() {const initialData = await this.fetchData(1);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);}createPagination(containerId) {// ... 保留原有的创建分页组件逻辑 ..const container = document.getElementById(containerId);container.innerHTML=''// 创建上一页按钮const prevPageButton = document.createElement('button');prevPageButton.id = 'prevPage';prevPageButton.textContent = '上一页';prevPageButton.classList.add('bg-blue-200','w-14', 'hover:bg-blue-700', 'text-white','text-sm',  'py-1', 'px-1', 'rounded', 'ml-2');// 创建下一页按钮const nextPageButton = document.createElement('button');nextPageButton.id = 'nextPage';nextPageButton.textContent = '下一页';nextPageButton.classList.add('bg-blue-500', 'w-14','hover:bg-blue-700', 'text-white', 'text-sm','py-1', 'px-1', 'rounded', 'ml-2');// 创建跳转按钮const gotoPageButton = document.createElement('button');gotoPageButton.id = 'gotoPageButton';gotoPageButton.textContent = '跳转';gotoPageButton.classList.add('bg-blue-500','w-14', 'hover:bg-blue-700', 'text-white', 'text-sm', 'py-1', 'px-1', 'rounded', 'ml-2');// 创建输入框const gotoPageInput = document.createElement('input');gotoPageInput.id = 'gotoPageInput';gotoPageInput.type = 'number';gotoPageInput.placeholder = '页码';gotoPageInput.classList.add('w-16', 'p-1', 'mt-1', 'border','text-sm', 'border-gray-300', 'rounded-md', 'ml-2');// 创建分页按钮容器const paginationContainer = document.createElement('div');paginationContainer.id = 'pagination';paginationContainer.classList.add('flex', 'justify-center');// 创建分页按钮列表let pages = [];const visibleRange = 5; // 显示5个完整页码const halfVisibleRange = Math.floor(visibleRange / 2); // 对称显示,一半数量的完整页码if (this.totalPages > visibleRange) {// 开始页码let startPage = this.paginationData.data.page - halfVisibleRange;if (startPage <= 0) {startPage = 1;}// 结束页码let endPage = this.paginationData.data.page + halfVisibleRange;if (endPage > this.totalPages) {endPage = this.totalPages;startPage = Math.max(startPage - (endPage - this.totalPages), 1);}// 添加开始页码之前的页码if (startPage > 1) {pages.push(1);if (startPage > 2) {pages.push('...');}}// 添加中间页码for (let i = startPage; i <= endPage; i++) {pages.push(i);}// 添加结束页码之后的页码if (endPage < this.totalPages) {pages.push('...');pages.push(this.totalPages);}} else {for (let i = 1; i <= this.totalPages; i++) {pages.push(i);}}pages.forEach((page) => {const button = document.createElement('button');button.id = `page-${page}`;button.textContent = page;button.classList.add('px-4', 'py-1', 'border', 'border-gray-300','text-sm', 'rounded-md', 'text-gray-700','ml-1',);if (page === this.paginationData.data.page) {button.classList.remove('text-gray-700');button.classList.add('bg-blue-500', 'text-white');}paginationContainer.appendChild(button);});// 创建分页信息容器const pageInfoContainer = document.createElement('div');pageInfoContainer.id = 'pageInfoContainer';pageInfoContainer.classList.add('flex',  'text-sm','justify-end');// 创建分页信息const pageInfo = document.createElement('span');pageInfo.id = 'pageInfo';pageInfo.textContent = `共 ${paginationData.data.toatalsize} 条记录,每页 ${paginationData.data.size} 条记录,当前第 ${paginationData.data.page} 页,共${paginationData.data.totalpage}页`;pageInfoContainer.appendChild(pageInfo);pageInfo.classList.add('ml-2','mt-2');// 创建跳转到第一页按钮const firstPageButton = document.createElement('button');firstPageButton.id = 'firstPageButton';firstPageButton.textContent = '首页';firstPageButton.classList.add('bg-blue-500','w-14', 'hover:bg-blue-700', 'text-white','text-sm',  'py-1', 'px-1', 'rounded', 'ml-2');pageInfoContainer.appendChild(firstPageButton);// 创建跳转到最后一页按钮const lastPageButton = document.createElement('button');lastPageButton.id = 'lastPageButton';lastPageButton.textContent = '尾页';lastPageButton.classList.add('bg-blue-500','w-14', 'hover:bg-blue-700', 'text-white','text-sm',  'py-1', 'px-1', 'rounded', 'ml-2');pageInfoContainer.appendChild(lastPageButton);// 将所有元素添加到容器中container.appendChild(prevPageButton);container.appendChild(nextPageButton);container.appendChild(gotoPageInput);container.appendChild(gotoPageButton);container.appendChild(paginationContainer);container.appendChild(pageInfoContainer);}// 更新选中的分页按钮updateSelectedPage(page, containerId) {const container = document.getElementById(containerId);const buttons = container.querySelectorAll('#pagination button');buttons.forEach((button) => {if (button.id === `page-${page}`) {button.classList.remove('text-gray-700');button.classList.add('bg-blue-500', 'text-white');} else {button.classList.add('text-gray-700');button.classList.remove('bg-blue-500', 'text-white');}});}bindEventListeners(containerId) {const prevPageButton = document.getElementById('prevPage');const nextPageButton = document.getElementById('nextPage');const gotoPageButton = document.getElementById('gotoPageButton');const gotoPageInput = document.getElementById('gotoPageInput');const pageInfoContainer = document.getElementById('pageInfoContainer');const firstPageButton = document.getElementById('firstPageButton');const lastPageButton = document.getElementById('lastPageButton');prevPageButton.addEventListener('click',async () => {const currentPage = parseInt(this.paginationData.data.page);if(currentPage==2){prevPageButton.classList.add('bg-blue-200');prevPageButton.classList.remove('bg-blue-500');}if(currentPage==1){alert("已经是第一页了");return;}if (currentPage > 1) {this.paginationData.data.page = currentPage - 1;const initialData =await  this.fetchData(  this.paginationData.data.page);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);this.updateSelectedPage(this.paginationData.data.page,containerId)}});nextPageButton.addEventListener('click', async() => {const currentPage = parseInt(this.paginationData.data.page);if( currentPage==this.totalPages){alert("已经是最后一页了");return;}prevPageButton.classList.remove('bg-blue-200');prevPageButton.classList.add('bg-blue-500');if (currentPage < this.totalPages) {const initialData =await  this.fetchData( this.paginationData.data.page);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.paginationData.data.page = currentPage + 1;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);this.updateSelectedPage(this.paginationData.data.page,containerId)}});gotoPageButton.addEventListener('click', async() => {const input = document.getElementById('gotoPageInput');const page = parseInt(input.value);if (!isNaN(page) && page > 0 && page <= this.totalPages) {const initialData =await  this.fetchData(page);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.paginationData.data.page = page;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);}});firstPageButton.addEventListener('click', async() => {this.paginationData.data.page = 1;const initialData =await  this.fetchData(this.paginationData.data.page);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);});lastPageButton.addEventListener('click',async () => {const initialData =await  this.fetchData(this.totalPages);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.paginationData.data.page = this.totalPages;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);});// ... 保留原有的事件绑定逻辑 ...const paginationButtons = document.querySelectorAll(`#${containerId} button[id^="page-"]`);paginationButtons.forEach((button) => {button.addEventListener('click', async (event) => {const targetPage = parseInt(event.target.id.replace('page-', ''));const initialData = await this.fetchData(targetPage);this.paginationData = initialData;this.totalPages=initialData.data.totalpage;this.paginationData.data.page =targetPage;this.options.onPageChange(initialData);this.createPagination(this.options.containerId);this.bindEventListeners(this.options.containerId);});});}async fetchData(page) {const url = `${this.fetchDataURL}&page=${page}&size=10`;try {const response = await fetch(url);const jsonData = await response.json();return jsonData;} catch (error) {console.error('There has been a problem with your fetch operation:', error);return null;}
}
}


http://www.ppmy.cn/ops/5337.html

相关文章

el-table使用show-summary合计,但只需要合并某一列

el-table使用show-summary合计&#xff0c;但只需要合并某一列 这里有两种方法&#xff0c;一种是网上的&#xff0c;我会引用他的链接给你们看。 一种是我自己看源码发现的 方法一 这个就是方法一的链接 点击我跳转方法一 方法二 不需要计算的列 去掉prop 然后用插槽显示即可…

Linux中文件特殊权限suid、sgid、sticky详解

Linux中文件特殊权限suid、sgid、sticky 作用对象 suid权限作用于文件属主sgid权限作用于属组sticky权限作用于other suid 作用&#xff1a;让普通用户临时拥有该文件的属主的指向权限&#xff0c;suid权限只能应用在二进制可执行文件&#xff08;命令&#xff09;表示方法…

mac jd-gui安装

在macOS上安装JD-GUI&#xff08;Java Decompiler GUI&#xff09;是一个简单的过程。JD-GUI是一个独立的图形化应用程序&#xff0c;你可以使用它来查看Java字节码对应的源代码。下面是安装步骤&#xff1a; 下载JD-GUI&#xff1a; 访问JD-GUI的官方网站&#xff08;http://j…

C语言入门算法——最大公约数和最小公倍数问题

题目描述&#xff1a; 输入两个正整数 x0​,y0​&#xff0c;求出满足下列条件的 P,Q 的个数&#xff1a; P,Q 是正整数。 要求P,Q 以 x0​ 为最大公约数&#xff0c;以 y0​ 为最小公倍数。 试求&#xff1a;满足条件的所有可能的 P,Q 的个数。 输入格式 一行两个正整数…

使用Python进行自动化测试

&#x1f47d;发现宝藏 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。【点击进入巨牛的人工智能学习网站】。 如何使用Python进行自动化测试&#xff1a;测试框架的选择与应用 自动化测试是软件开发过程…

XiaodiSec day015 Learn Note 小迪渗透学习笔记

XiaodiSec day015 Learn Note 小迪渗透学习笔记 记录得比较凌乱&#xff0c;不尽详细 day15 还是基础的Php开发 看来是比较基础的 主题&#xff1a;登录验证 COOKIE & SESSION 后台系统有多个验证 为了方便验证使用cookie或session 类似于用户状态管理? cookie存储…

cocos creator 3.6 发布web手机端 加载进度条添加

cocos creator 升级到3.x之后加载进度条取消了&#xff0c;测试了多个3.x版本最终以creator 3.6.3版本&#xff0c;构建了简单的进度加载 参考链接&#xff1a; https://forum.cocos.org/t/topic/137113 打包web-mobile后&#xff0c;没有进度条。加载的时候只显示一个黑屏。…

MySQL 列数据跨表拷贝,一句SQL快速将表A每条记录的某些字段拷贝到表B每条记录的某些字段(A、B表通过ID等字段对应)

文章目录 MySQL 列数据跨表拷贝&#xff0c;一句SQL快速将表A每条记录的某些字段拷贝到表B每条记录的某些字段&#xff08;A、B表通过ID等字段对应&#xff09;背景定义表填充测试数据跨表一 一对应拷贝列数据SQL参考资料 MySQL 列数据跨表拷贝&#xff0c;一句SQL快速将表A每条…