修改以下代码增加表格分页 let popup = document.createElement('div'); let table = document.createElement('table'); let thead = document.createElement('thead'); let tbody = document.createElement('tbody'); let tableHead = '<tr><th>映射对象id</th><th>映射对象类型</th><th>映射对象名称</th><th>操作</th></tr>'; let tableBody = '' for (let i = 0; i < dom.tableData.length; i++) { const obj = dom.tableData[i] tableBody = tableBody + '<tr><td style="text-align: center;">'+ obj.dataId +'</td><td style="text-align: center;">'+ obj.type +'</td><td style="text-align: center;">'+ obj.dataName +'</td><td style="text-align: center;"><button class="info-btn" data-dataid="'+ obj.dataId +'" data-type="'+ obj.type +'" data-dataname="'+ obj.dataName +'">查看</button></td></tr>' } thead.innerHTML = tableHead; tbody.innerHTML = tableBody; table.appendChild(thead); table.appendChild(tbody); popup.appendChild(table);
时间: 2024-03-03 14:48:45 浏览: 123
表格显示并具有分页功能
可以使用第三方库或者手写实现分页功能。以下是手写实现的示例代码:
1. 在HTML中添加分页元素
```
<div id="pagination"></div>
```
2. 修改代码增加分页逻辑
```
let popup = document.createElement('div');
let table = document.createElement('table');
let thead = document.createElement('thead');
let tbody = document.createElement('tbody');
let currentPage = 1; // 当前页
let pageSize = 10; // 每页显示的数据量
// 根据当前页和每页显示的数据量,截取数据
let startIndex = (currentPage - 1) * pageSize;
let endIndex = startIndex + pageSize;
let tableData = dom.tableData.slice(startIndex, endIndex);
let tableHead = '<tr><th>映射对象id</th><th>映射对象类型</th><th>映射对象名称</th><th>操作</th></tr>';
let tableBody = '';
for (let i = 0; i < tableData.length; i++) {
const obj = tableData[i];
tableBody = tableBody + '<tr><td style="text-align: center;">'+ obj.dataId +'</td><td style="text-align: center;">'+ obj.type +'</td><td style="text-align: center;">'+ obj.dataName +'</td><td style="text-align: center;"><button class="info-btn" data-dataid="'+ obj.dataId +'" data-type="'+ obj.type +'" data-dataname="'+ obj.dataName +'">查看</button></td></tr>';
}
thead.innerHTML = tableHead;
tbody.innerHTML = tableBody;
table.appendChild(thead);
table.appendChild(tbody);
popup.appendChild(table);
// 计算总页数
let totalPages = Math.ceil(dom.tableData.length / pageSize);
// 生成分页按钮
let pagination = document.getElementById('pagination');
for (let i = 1; i <= totalPages; i++) {
let btn = document.createElement('button');
btn.innerText = i;
btn.onclick = function() {
currentPage = i;
renderTable();
}
pagination.appendChild(btn);
}
function renderTable() {
// 清空表格和分页按钮
table.innerHTML = '';
pagination.innerHTML = '';
// 根据当前页和每页显示的数据量,截取数据
let startIndex = (currentPage - 1) * pageSize;
let endIndex = startIndex + pageSize;
let tableData = dom.tableData.slice(startIndex, endIndex);
let tableBody = '';
for (let i = 0; i < tableData.length; i++) {
const obj = tableData[i];
tableBody = tableBody + '<tr><td style="text-align: center;">'+ obj.dataId +'</td><td style="text-align: center;">'+ obj.type +'</td><td style="text-align: center;">'+ obj.dataName +'</td><td style="text-align: center;"><button class="info-btn" data-dataid="'+ obj.dataId +'" data-type="'+ obj.type +'" data-dataname="'+ obj.dataName +'">查看</button></td></tr>';
}
tbody.innerHTML = tableBody;
table.appendChild(tbody);
popup.appendChild(table);
// 重新生成分页按钮
for (let i = 1; i <= totalPages; i++) {
let btn = document.createElement('button');
btn.innerText = i;
btn.onclick = function() {
currentPage = i;
renderTable();
}
pagination.appendChild(btn);
}
}
renderTable();
```
阅读全文