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>
相关推荐
喵叔哟20 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生26 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow40 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
不是二师兄的八戒1 小时前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
小牛itbull1 小时前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
请叫我欧皇i1 小时前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落1 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
爱编程的小生1 小时前
Easyexcel(2-文件读取)
java·excel
GIS瞧葩菜1 小时前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming19871 小时前
STL关联式容器之set
开发语言·c++