利用JS实现 TABLE的分页
时间: 2024-05-07 17:20:57 浏览: 93
可以通过以下步骤来实现 TABLE 的分页:
1. 在 HTML 中创建一个 TABLE 元素,并设置一个 ID,以便在 JavaScript 中引用。
```html
<table id="myTable">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
<!-- Add more rows as needed -->
</tbody>
</table>
```
2. 在 JavaScript 中定义每页显示的行数和当前页数。
```javascript
var rowsPerPage = 10;
var currentPage = 1;
```
3. 编写一个函数来显示当前页的行。
```javascript
function showPage(page) {
var table = document.getElementById("myTable");
var rows = table.getElementsByTagName("tr");
var startIndex = (page - 1) * rowsPerPage + 1;
var endIndex = startIndex + rowsPerPage - 1;
for (var i = 1; i < rows.length; i++) {
if (i < startIndex || i > endIndex) {
rows[i].style.display = "none";
} else {
rows[i].style.display = "";
}
}
}
```
4. 在页面加载时调用 `showPage` 函数来显示第一页的行。
```javascript
window.onload = function() {
showPage(currentPage);
};
```
5. 编写一个函数来更新页码链接。
```javascript
function updatePageLinks() {
var table = document.getElementById("myTable");
var rows = table.getElementsByTagName("tr");
var totalPages = Math.ceil((rows.length - 1) / rowsPerPage);
var links = document.getElementById("pageLinks");
links.innerHTML = "";
for (var i = 1; i <= totalPages; i++) {
var link = document.createElement("a");
link.href = "#";
link.onclick = function() {
currentPage = parseInt(this.innerHTML);
showPage(currentPage);
updatePageLinks();
return false;
};
link.innerHTML = i;
if (i == currentPage) {
link.className = "active";
}
links.appendChild(link);
}
}
```
6. 在页面加载时调用 `updatePageLinks` 函数来显示页码链接。
```javascript
window.onload = function() {
showPage(currentPage);
updatePageLinks();
};
```
7. 在 HTML 中创建一个元素来显示页码链接,并设置一个 ID,以便在 JavaScript 中引用。
```html
<div id="pageLinks"></div>
```
完整代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Table Pagination</title>
<style>
.active {
font-weight: bold;
text-decoration: none;
}
</style>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
<!-- Add more rows as needed -->
</tbody>
</table>
<div id="pageLinks"></div>
<script>
var rowsPerPage = 10;
var currentPage = 1;
function showPage(page) {
var table = document.getElementById("myTable");
var rows = table.getElementsByTagName("tr");
var startIndex = (page - 1) * rowsPerPage + 1;
var endIndex = startIndex + rowsPerPage - 1;
for (var i = 1; i < rows.length; i++) {
if (i < startIndex || i > endIndex) {
rows[i].style.display = "none";
} else {
rows[i].style.display = "";
}
}
}
function updatePageLinks() {
var table = document.getElementById("myTable");
var rows = table.getElementsByTagName("tr");
var totalPages = Math.ceil((rows.length - 1) / rowsPerPage);
var links = document.getElementById("pageLinks");
links.innerHTML = "";
for (var i = 1; i <= totalPages; i++) {
var link = document.createElement("a");
link.href = "#";
link.onclick = function() {
currentPage = parseInt(this.innerHTML);
showPage(currentPage);
updatePageLinks();
return false;
};
link.innerHTML = i;
if (i == currentPage) {
link.className = "active";
}
links.appendChild(link);
}
}
window.onload = function() {
showPage(currentPage);
updatePageLinks();
};
</script>
</body>
</html>
```
阅读全文