首页 > 其他分享 >删除选中功能

删除选中功能

时间:2022-12-27 20:56:23浏览次数:38  
标签:功能 删除 request ids 选中 user id

删除选中分析:

 

 

 

 

获取选中条目id的代码实现:

list.jsp:

  使用form标签将整个table包起来

  给删除选中按钮绑定单击事件

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比,为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>

    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>
    <%-- 删除指定id   --%>
    <script>
        function deleteUser(id) {
            //用户安全提示
            if (confirm("确定要删除吗?")){
                //访问路径
                location.href = "${pageContext.request.contextPath}/delUserServlet?id="+id;
            }
        }

        window.onload = function (){
            //给删除选中按钮添加点击事件
            document.getElementById("delSelected").onclick = function (){
                //表单提交
                document.getElementById("from").submit();

            }
        }

    </script>

</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>

    <div style="float: left">

        <form class="form-inline">
            <div class="form-group">
                <label for="exampleInputName2">姓名</label>
                <input type="text" class="form-control" id="exampleInputName2" placeholder="李四">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail2">籍贯</label>
                <input type="email" class="form-control" id="exampleInputEmail2" placeholder="北京">
            </div>

            <div class="form-group">
                <label for="exampleInputEmail3">邮箱</label>
                <input type="email" class="form-control" id="exampleInputEmail3" placeholder="">
            </div>

            <button type="submit" class="btn btn-default">查询</button>
        </form>

    </div>

    <div style="float: right; margin: 5px">

            <td colspan="8" align="center">

                <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a>

            </td>

            <td colspan="8" align="center">

                <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a>

            </td>

    </div>

    <form id="from" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">

    <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th><input type="checkbox"></th>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>籍贯</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>

        <c:forEach items="${users}" var="user" varStatus="s">
            <tr>
                <td><input type="checkbox" name="uid" value="${user.id}"></td>
                <td>${s.count}</td>
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.age}</td>
                <td>${user.address}</td>
                <td>${user.qq}</td>
                <td>${user.email}</td>
                <td>
                    <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;



                    <%--<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/delUserServlet?id=${user.id}">删除</a>--%>
                    <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id})">删除</a>

                </td>
            </tr>
        </c:forEach>
    </table>

    </form>

    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li>
                    <a href="#" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
                <li>
                    <a href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 25px;margin-left: 5px">
                    共15条记录,共4页

                </span>
            </ul>
        </nav>
    </div>


</div>
</body>
</html>

 

 

删除选中代码实现:

UserService接口:

    /**
     * 批量删除用户
     * @param ids
     */
    void delSelectedUser(String[] ids);

UserServiceImpl实现类:

    @Override
    public void delSelectedUser(String[] ids) {
        //1.遍历数组
        for (String id : ids) {
            //2.调用dao删除
            dao.delete(Integer.parseInt(id));
        }
    }
}

DelSelectedServlet:

package com.example.web.servlet;

import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "delSelectedServlet", value = "/delSelectedServlet")
public class DelSelectedServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取所有的id
        String[] ids = request.getParameterValues("uid");
        //2.调用Service删除
        UserService service = new UserServiceImpl();
        service.delSelectedUser(ids);

        //3.跳转到查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/userListServlet");

    }
}

 

 

删除选中细节处理

设置全选,全不选,以及删除条目提示是否要删除

list.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比,为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>

    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>
    <%-- 删除指定id   --%>
    <script>
        function deleteUser(id) {
            //用户安全提示
            if (confirm("确定要删除吗?")){
                //访问路径
                location.href = "${pageContext.request.contextPath}/delUserServlet?id="+id;
            }
        }

        window.onload = function (){
            //给删除选中按钮添加点击事件
            document.getElementById("delSelected").onclick = function (){
                if (confirm("确定要删除选中条目吗?")){

                    var flag = false;
                    //判断是否有选中条目
                    let cbs = document.getElementsByName("uid");
                    for (let i = 0; i <cbs.length; i++) {
                        if (cbs[i].checked){
                            //有一个条目选中了
                            flag = true;
                            break;
                        }
                    }

                    if (flag){//有条目被选中
                        //表单提交
                        document.getElementById("from").submit();
                    }
                }
            }

         //1.获取第一个cb
            document.getElementById("firstCb").onclick = function (){
                //2.获取下边列表中所有的cb
                let cbs = document.getElementsByName("uid");
                //3.遍历
                for (let i = 0; i <cbs.length; i++) {
                    //4.设置这些cbs[i]的checked状态 = firstCd.checked
                    cbs[i].checked = this.checked;
                }
            }
        }
    </script>

</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>

    <div style="float: left">

        <form class="form-inline">
            <div class="form-group">
                <label for="exampleInputName2">姓名</label>
                <input type="text" class="form-control" id="exampleInputName2" placeholder="李四">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail2">籍贯</label>
                <input type="email" class="form-control" id="exampleInputEmail2" placeholder="北京">
            </div>

            <div class="form-group">
                <label for="exampleInputEmail3">邮箱</label>
                <input type="email" class="form-control" id="exampleInputEmail3" placeholder="">
            </div>

            <button type="submit" class="btn btn-default">查询</button>
        </form>

    </div>

    <div style="float: right; margin: 5px">

            <td colspan="8" align="center">

                <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a>

            </td>

            <td colspan="8" align="center">

                <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a>

            </td>

    </div>

    <form id="from" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">

    <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th><input type="checkbox" id="firstCb"></th>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>籍贯</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>

        <c:forEach items="${users}" var="user" varStatus="s">
            <tr>
                <td><input type="checkbox" name="uid" value="${user.id}"></td>
                <td>${s.count}</td>
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.age}</td>
                <td>${user.address}</td>
                <td>${user.qq}</td>
                <td>${user.email}</td>
                <td>
                    <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;



                    <%--<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/delUserServlet?id=${user.id}">删除</a>--%>
                    <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id})">删除</a>

                </td>
            </tr>
        </c:forEach>
    </table>

    </form>

    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li>
                    <a href="#" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
                <li>
                    <a href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 25px;margin-left: 5px">
                    共15条记录,共4页

                </span>
            </ul>
        </nav>
    </div>


</div>
</body>
</html>

UserServiceImpl实现类:

    @Override
    public void delSelectedUser(String[] ids) {
        if (ids != null && ids.length > 0){
            //1.遍历数组
            for (String id : ids) {
                //2.调用dao删除
                dao.delete(Integer.parseInt(id));
            }
        }
    }
}

 

标签:功能,删除,request,ids,选中,user,id
From: https://www.cnblogs.com/qihaokuan/p/17008845.html

相关文章