<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.c..._jsp+ajax实现批量删除">
当前位置:   article > 正文

jsp jQuery Ajax 实现批量删除_jsp+ajax实现批量删除

jsp+ajax实现批量删除
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <title>图书信息管理系统- 图书列表</title>
  9. <!-- 引入外部CSS样式 -->
  10. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
  11. <!-- 引入JS -->
  12. <script src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
  13. <script src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
  14. </head>
  15. <script type="text/javascript">
  16. $(function(){
  17. $("#checkAll").change(function(){//判断全选框的改变
  18. var flage =$(this).is(":checked");//全选选中为true,否则为false
  19. $("input[type='checkbox']").each(function(){
  20. $(this).prop("checked",flage);
  21. })
  22. });
  23. });
  24. </script>
  25. <script type="text/javascript">
  26. function deleteBooksByIds(pindex){
  27. var myArray=new Array()
  28. $.each($("input[name='book_ids']"),function(){
  29. if(this.checked){
  30. myArray.push($(this).val());
  31. }
  32. });
  33. var jsonStr=myArray.join(',')
  34. if(myArray.length>0){
  35. var msg = confirm("您确定要删除么?");
  36. if (msg) {
  37. $.post("deleteBooksByIds",
  38. {ids : jsonStr,pageIndex:pindex},function(data){
  39. if("ok"==data){
  40. goPage(pindex);
  41. }
  42. });
  43. }
  44. }else{
  45. alert("请选择删除项!");
  46. }
  47. }
  48. </script>
  49. <body>
  50. <div>
  51. <h1>图书信息展示</h1>
  52. <br />
  53. <button type="button" class="btn btn-danger" onclick="deleteBooksByIds(${pageIndex});">
  54. <span class="glyphicon glyphicon-trash"></span>
  55. 批量删除
  56. </button>
  57. <br />
  58. <br />
  59. <table class="table table-hover">
  60. <tr class="info">
  61. <th>
  62. <input type="checkbox" id="checkAll">
  63. 全选
  64. </th>
  65. <th>数据编号</th>
  66. <th>图书名称</th>
  67. <th>作者</th>
  68. <th>添加日期</th>
  69. <th>图书价格</th>
  70. <th>操作</th>
  71. </tr>
  72. <c:forEach items="${bookList}" var="book" varStatus="stat">
  73. <tr class="default" align="center">
  74. <td>
  75. <input class="checkids" type="checkbox" name="book_ids" value="${book.id}">
  76. </td>
  77. <td>${stat.index+1}</td>
  78. <td>${book.name}</td>
  79. <td>${book.author}</td>
  80. <td>
  81. <fmt:formatDate value="${book.createdate}" pattern="yyyy年MM月dd日" />
  82. </td>
  83. <td>
  84. <fmt:formatNumber type="currency" value="${book.price}" />
  85. </td>
  86. <td>
  87. <button type="button" class="btn btn-warning" onclick="goUpdate(${book.id},${pageIndex})">
  88. <span class="glyphicon glyphicon-edit"></span>
  89. 更新
  90. </button>
  91. &nbsp;
  92. <button type="button" class="btn btn-danger" onclick="goDelete(${book.id},${pageIndex})">
  93. <span class="glyphicon glyphicon-trash"></span>
  94. 删除
  95. </button>
  96. </td>
  97. </tr>
  98. </c:forEach>
  99. <!-- 分页插件 -->
  100. <tr class="default">
  101. <th colspan="7">
  102. <button type="button" class="btn btn-default" onclick="goPage(1);">
  103. <span class="glyphicon glyphicon-fast-backward"></span>
  104. 首页
  105. </button>
  106. <!-- 第一页是不可以再点击上一页 -->
  107. <c:choose>
  108. <c:when test="${pageIndex > 1}">
  109. <button type="button" class="btn btn-default" onclick="goPage(${pageIndex-1});">
  110. <span class="glyphicon glyphicon-backward"></span>
  111. 上一页
  112. </button>
  113. </c:when>
  114. <c:otherwise>
  115. <button type="button" class="btn btn-default" disabled="disabled">
  116. <span class="glyphicon glyphicon-backward"></span>
  117. 上一页
  118. </button>
  119. </c:otherwise>
  120. </c:choose>
  121. <!-- 末页的时候是不能再点下一页 -->
  122. <c:choose>
  123. <c:when test="${totalPage > pageIndex}">
  124. <button type="button" class="btn btn-default" onclick="goPage(${pageIndex+1});">
  125. <span class="glyphicon glyphicon-forward"></span>
  126. 下一页
  127. </button>
  128. </c:when>
  129. <c:otherwise>
  130. <button type="button" class="btn btn-default" disabled="disabled">
  131. <span class="glyphicon glyphicon-forward"></span>
  132. 下一页
  133. </button>
  134. </c:otherwise>
  135. </c:choose>
  136. <button type="button" class="btn btn-default" onclick="goPage(${totalPage});">
  137. <span class="glyphicon glyphicon-fast-forward"></span>
  138. 末页
  139. </button>
  140. </th>
  141. </tr>
  142. </table>
  143. </div>
  144. </body>
  145. </html>

controller处理类局部代码

  1. @ResponseBody
  2. @RequestMapping("/deleteBooksByIds")
  3. public String deleteBooksByIds(HttpServletRequest request, String ids,
  4. @RequestParam(name = "pageIndex", defaultValue = "1", required = false) Integer pageIndex) {
  5. ModelAndView mv = new ModelAndView();
  6. int count = 0;
  7. if (ids != null) {
  8. String[] idarr = ids.split(",");
  9. for (String id : idarr) {
  10. try {
  11. count = service.deleteBookById(Integer.parseInt(id));
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. return "error";
  15. }
  16. }
  17. return "ok";
  18. } else {
  19. return "error";
  20. }
  21. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/100760
推荐阅读
相关标签
  

闽ICP备14008679号