%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.c..._jsp+ajax实现批量删除">
赞
踩
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>图书信息管理系统- 图书列表</title>
- <!-- 引入外部CSS样式 -->
- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
- <!-- 引入JS -->
- <script src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
- <script src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
- </head>
-
- <script type="text/javascript">
-
- $(function(){
- $("#checkAll").change(function(){//判断全选框的改变
- var flage =$(this).is(":checked");//全选选中为true,否则为false
- $("input[type='checkbox']").each(function(){
- $(this).prop("checked",flage);
- })
- });
- });
- </script>
-
- <script type="text/javascript">
-
- function deleteBooksByIds(pindex){
- var myArray=new Array()
- $.each($("input[name='book_ids']"),function(){
- if(this.checked){
- myArray.push($(this).val());
- }
- });
-
- var jsonStr=myArray.join(',')
- if(myArray.length>0){
- var msg = confirm("您确定要删除么?");
- if (msg) {
- $.post("deleteBooksByIds",
- {ids : jsonStr,pageIndex:pindex},function(data){
- if("ok"==data){
- goPage(pindex);
- }
- });
- }
- }else{
-
- alert("请选择删除项!");
- }
-
-
- }
-
- </script>
-
- <body>
-
- <div>
-
- <h1>图书信息展示</h1>
-
- <br />
-
- <button type="button" class="btn btn-danger" onclick="deleteBooksByIds(${pageIndex});">
- <span class="glyphicon glyphicon-trash"></span>
- 批量删除
- </button>
- <br />
- <br />
-
- <table class="table table-hover">
-
- <tr class="info">
- <th>
- <input type="checkbox" id="checkAll">
- 全选
- </th>
- <th>数据编号</th>
- <th>图书名称</th>
- <th>作者</th>
- <th>添加日期</th>
- <th>图书价格</th>
- <th>操作</th>
- </tr>
- <c:forEach items="${bookList}" var="book" varStatus="stat">
- <tr class="default" align="center">
- <td>
- <input class="checkids" type="checkbox" name="book_ids" value="${book.id}">
- </td>
- <td>${stat.index+1}</td>
-
- <td>${book.name}</td>
- <td>${book.author}</td>
- <td>
- <fmt:formatDate value="${book.createdate}" pattern="yyyy年MM月dd日" />
- </td>
- <td>
- <fmt:formatNumber type="currency" value="${book.price}" />
- </td>
- <td>
- <button type="button" class="btn btn-warning" onclick="goUpdate(${book.id},${pageIndex})">
- <span class="glyphicon glyphicon-edit"></span>
- 更新
- </button>
-
- <button type="button" class="btn btn-danger" onclick="goDelete(${book.id},${pageIndex})">
- <span class="glyphicon glyphicon-trash"></span>
- 删除
- </button>
- </td>
- </tr>
- </c:forEach>
- <!-- 分页插件 -->
- <tr class="default">
- <th colspan="7">
- <button type="button" class="btn btn-default" onclick="goPage(1);">
- <span class="glyphicon glyphicon-fast-backward"></span>
- 首页
- </button>
- <!-- 第一页是不可以再点击上一页 -->
- <c:choose>
- <c:when test="${pageIndex > 1}">
- <button type="button" class="btn btn-default" onclick="goPage(${pageIndex-1});">
- <span class="glyphicon glyphicon-backward"></span>
- 上一页
- </button>
- </c:when>
- <c:otherwise>
- <button type="button" class="btn btn-default" disabled="disabled">
- <span class="glyphicon glyphicon-backward"></span>
- 上一页
- </button>
- </c:otherwise>
- </c:choose>
- <!-- 末页的时候是不能再点下一页 -->
- <c:choose>
- <c:when test="${totalPage > pageIndex}">
- <button type="button" class="btn btn-default" onclick="goPage(${pageIndex+1});">
- <span class="glyphicon glyphicon-forward"></span>
- 下一页
- </button>
- </c:when>
- <c:otherwise>
- <button type="button" class="btn btn-default" disabled="disabled">
- <span class="glyphicon glyphicon-forward"></span>
- 下一页
- </button>
- </c:otherwise>
- </c:choose>
- <button type="button" class="btn btn-default" onclick="goPage(${totalPage});">
- <span class="glyphicon glyphicon-fast-forward"></span>
- 末页
- </button>
- </th>
- </tr>
-
- </table>
-
- </div>
-
- </body>
- </html>
-
-
-
controller处理类局部代码
- @ResponseBody
- @RequestMapping("/deleteBooksByIds")
- public String deleteBooksByIds(HttpServletRequest request, String ids,
- @RequestParam(name = "pageIndex", defaultValue = "1", required = false) Integer pageIndex) {
- ModelAndView mv = new ModelAndView();
- int count = 0;
-
- if (ids != null) {
- String[] idarr = ids.split(",");
- for (String id : idarr) {
- try {
- count = service.deleteBookById(Integer.parseInt(id));
- } catch (Exception e) {
- e.printStackTrace();
- return "error";
- }
- }
- return "ok";
- } else {
-
- return "error";
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。