ES6语法下HTML+CSS+JS学生信息管理系统 - 添加、删除、修改、查看功能,支持分页
<!DOCTYPE html>
<html>
<head>
<title>ES6语法下HTML+CSS+JS学生信息管理系统</title>
<style>
.container {
margin: 20px;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
.pagination {
margin-top: 20px;
}
.pagination button {
margin-left: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>学生信息管理系统</h1>
<button id="addButton">增加</button>
<button id="deleteButton">删除</button>
<button id="editButton">更改</button>
<button id="viewButton">查看</button>
<table id="studentTable">
<thead>
<tr>
<th></th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<!-- 学生信息行通过JS生成 -->
</tbody>
</table>
<div class="pagination">
<button id="prevButton">上一页</button>
<button id="nextButton">下一页</button>
</div>
</div>
<script>
// 学生信息数据
const students = [
{ id: 1, name: '张三', age: 18, gender: '男' },
{ id: 2, name: '李四', age: 20, gender: '女' },
// 其他学生信息...
{ id: 10, name: '王五', age: 19, gender: '男' }
];
// 当前页码和每页显示的学生数量
let currentPage = 1;
const pageSize = 10;
// 获取当前页的学生信息
function getCurrentPageStudents() {
const startIndex = (currentPage - 1) * pageSize;
const endIndex = startIndex + pageSize;
return students.slice(startIndex, endIndex);
}
// 渲染学生信息表格
function renderStudentTable() {
const tableBody = document.querySelector('#studentTable tbody');
tableBody.innerHTML = '';
const currentPageStudents = getCurrentPageStudents();
currentPageStudents.forEach((student) => {
const row = document.createElement('tr');
const checkboxCell = document.createElement('td');
const nameCell = document.createElement('td');
const ageCell = document.createElement('td');
const genderCell = document.createElement('td');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.value = student.id;
checkboxCell.appendChild(checkbox);
nameCell.textContent = student.name;
ageCell.textContent = student.age;
genderCell.textContent = student.gender;
row.appendChild(checkboxCell);
row.appendChild(nameCell);
row.appendChild(ageCell);
row.appendChild(genderCell);
tableBody.appendChild(row);
});
}
// 渲染分页按钮
function renderPagination() {
const prevButton = document.querySelector('#prevButton');
const nextButton = document.querySelector('#nextButton');
if (currentPage === 1) {
prevButton.disabled = true;
} else {
prevButton.disabled = false;
}
if (currentPage === Math.ceil(students.length / pageSize)) {
nextButton.disabled = true;
} else {
nextButton.disabled = false;
}
}
// 切换到上一页
function goToPrevPage() {
if (currentPage > 1) {
currentPage--;
renderStudentTable();
renderPagination();
}
}
// 切换到下一页
function goToNextPage() {
if (currentPage < Math.ceil(students.length / pageSize)) {
currentPage++;
renderStudentTable();
renderPagination();
}
}
// 初始化页面
function init() {
renderStudentTable();
renderPagination();
const prevButton = document.querySelector('#prevButton');
const nextButton = document.querySelector('#nextButton');
prevButton.addEventListener('click', goToPrevPage);
nextButton.addEventListener('click', goToNextPage);
}
// 页面加载完成后执行初始化
window.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>

原文地址: http://www.cveoy.top/t/topic/pA2w 著作权归作者所有。请勿转载和采集!