jsp-curd+分页倒导航案例

效果图

bash 复制代码
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理</title>
    <!-- 引入Bootstrap的CDN -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入jQuery的CDN -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <h1 class="mt-5">学生管理</h1>
    <!-- 搜索表单 -->
    <div class="mb-3">
        <form id="searchForm" class="form-inline">
            <input type="text" id="keyword" class="form-control mr-2" placeholder="请输入关键词">
            <button type="button" class="btn btn-primary" onclick="searchStudents()">搜索</button>
        </form>
    </div>
    <!-- 新增学生按钮 -->
    <div class="mb-3">
        <button type="button" class="btn btn-success" onclick="showAddStudentModal()">新增学生</button>
    </div>
    <!-- 学生列表 -->
    <div id="studentTable" class="table-responsive"></div>
    <!-- 分页控件 -->
    <nav>
        <ul class="pagination" id="pagination"></ul>
    </nav>
    <!-- 模态框用于新增和编辑学生 -->
    <div class="modal fade" id="studentModal" tabindex="-1" aria-labelledby="studentModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="studentModalLabel">学生信息</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="studentForm">
                        <input type="hidden" id="studentId">
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" required>
                        </div>
                        <div class="form-group">
                            <label for="gender">性别</label>
                            <select class="form-control" id="gender" required>
                                <option value="M">男</option>
                                <option value="F">女</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="className">班级</label>
                            <input type="text" class="form-control" id="className" required>
                        </div>
                        <div class="form-group">
                            <label for="studentNumber">学号</label>
                            <input type="text" class="form-control" id="studentNumber" required>
                        </div>
                        <div class="form-group">
                            <label for="phoneNumber">手机号</label>
                            <input type="text" class="form-control" id="phoneNumber" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveStudent()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    let currentPage = 1;
    let pageSize = 5;

    // 初始化加载学生列表
    $(document).ready(function() {
        loadStudents();
    });

    function loadStudents() {
        $.ajax({
            url: '/student',
            type: 'GET',
            data: {
                operation: 'findByKeyword',
                keyword: $('#keyword').val(),
                page: currentPage,
                pageSize: pageSize
            },
            success: function(response) {
                if (response.code === 200) {
                    renderStudentTable(response.data);
                    renderPagination(response.other);
                } else {
                    alert(response.msg);
                }
            }
        });
    }

    function renderStudentTable(students) {
        let table = `<table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <th>性别</th>
                                    <th>班级</th>
                                    <th>学号</th>
                                    <th>手机号</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>`;
        students.forEach(student => {
            table += `<tr data-id="${student.id}" data-name="${student.name}" data-gender="${student.gender}"
                           data-classname="${student.className}" data-studentnumber="${student.studentNumber}" data-phonenumber="${student.phoneNumber}">
                            <td>${student.name}</td>
                            <td>${student.gender}</td>
                            <td>${student.className}</td>
                            <td>${student.studentNumber}</td>
                            <td>${student.phoneNumber}</td>
                            <td>
                                <button class="btn btn-sm btn-primary" onclick="editStudent(this)">编辑</button>
                                <button class="btn btn-sm btn-danger" onclick="deleteStudent(${student.id})">删除</button>
                            </td>
                          </tr>`;
        });
        table += `</tbody></table>`;
        $('#studentTable').html(table);
    }

    function renderPagination(totalCount) {
        let totalPages = Math.ceil(totalCount / pageSize);
        let pagination = '';
        for (let i = 1; i <= totalPages; i++) {
            pagination += `<li class="page-item ${i === currentPage ? 'active' : ''}">
                                <button class="page-link" onclick="changePage(${i})">${i}</button>
                               </li>`;
        }
        $('#pagination').html(pagination);
    }

    function changePage(page) {
        currentPage = page;
        loadStudents();
    }

    function searchStudents() {
        currentPage = 1;
        loadStudents();
    }

    function showAddStudentModal() {
        $('#studentId').val('');
        $('#name').val('');
        $('#gender').val('M');
        $('#className').val('');
        $('#studentNumber').val('');
        $('#phoneNumber').val('');
        $('#studentModalLabel').text('新增学生');
        $('#studentModal').modal('show');
    }

    function editStudent(button) {
        let row = $(button).closest('tr');
        $('#studentId').val(row.data('id'));
        $('#name').val(row.data('name'));
        $('#gender').val(row.data('gender'));
        $('#className').val(row.data('classname'));
        $('#studentNumber').val(row.data('studentnumber'));
        $('#phoneNumber').val(row.data('phonenumber'));
        $('#studentModalLabel').text('编辑学生');
        $('#studentModal').modal('show');
    }

    function saveStudent() {
        let student = {
            id: $('#studentId').val(),
            name: $('#name').val(),
            gender: $('#gender').val(),
            className: $('#className').val(),
            studentNumber: $('#studentNumber').val(),
            phoneNumber: $('#phoneNumber').val()
        };

        let operation = student.id ? 'updateStudent' : 'register';

        $.ajax({
            url: '/student',
            type: 'POST',
            data: {
                operation: operation,
                ...student
            },
            success: function(response) {
                if (response.code === 200) {
                    $('#studentModal').modal('hide');
                    loadStudents();
                } else {
                    alert(response.msg);
                }
            }
        });
    }

    function deleteStudent(id) {
        if (confirm('确定要删除这个学生吗?')) {
            $.ajax({
                url: '/student',
                type: 'POST',
                data: {
                    operation: 'deleteStudent',
                    studentId: id
                },
                success: function(response) {
                    if (response.code === 200) {
                        loadStudents();
                    } else {
                        alert(response.msg);
                    }
                }
            });
        }
    }
</script>
</body>
</html>
相关推荐
虫小宝22 分钟前
如何在Java中实现PDF生成
java·开发语言·pdf
菜鸡且互啄691 小时前
在线教育平台,easyexcel使用案例
java·开发语言
八月林城1 小时前
JAVA导出数据库字典到Excel
java·数据库·excel
电饭叔2 小时前
《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数
开发语言·python
weixin_452600692 小时前
如何为老化的汽车铅酸电池充电
开发语言·单片机·安全·汽车·电机·电源模块·充电桩
浅念同学3 小时前
算法-常见数据结构设计
java·数据结构·算法
Java资深爱好者3 小时前
如何在std::map中查找元素
开发语言·c++
YCCX_XFF213 小时前
ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1
开发语言·python
哥廷根数学学派4 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
杰哥在此5 小时前
Java面试题:讨论持续集成/持续部署的重要性,并描述如何在项目中实施CI/CD流程
java·开发语言·python·面试·编程