1. 程式人生 > 其它 >批量刪除操作----程式碼

批量刪除操作----程式碼

此處實現出現的問題,  form表單未閉合 ,出現多個form 未取消,導致一直無法提交,debug多次也無法定位錯誤,最終才發現是form 標籤的問題

 

 

第一步:新增 複選框 以及 全選取消全選的js

實現批量刪除 多選, 需要 在form表單裡面進行

<script>

<%--    設定或取消全選--%>
function checkAll(obj){
var boxes = document.getElementsByName("id");
for (var i =0;i<boxes.length;i++){
boxes[i].checked = obj.checked;

}
}

</script>

 <table class="table table-striped table-bordered table-hover dataTable">
<tr class="active">
        <td class="text-center"><input type="checkbox" id="all" value="全選" onclick="checkAll(this)"></td>
<th>專業id</th>
<th>專業名稱</th>

<th>學費</th>
<th width="200">操作</th>
</tr>
<form action="${pageContext.request.contextPath}/major/deleteMajorsById" method="post">

<c:forEach items="${sessionScope.majorList}" var="list">
<tr class="success">
<td class="text-center"><input type="checkbox" name="id" value="${list.majorId}" class="cbox"/></td>

<td>${list.majorId}</td>
<td>${list.majorName}</td>
<td>${list.tuition}</td>
<td>
<div class="btn-group">
<a href="${pageContext.request.contextPath}/major/queryOne?majorId=${list.majorId}" class="btn btn-default btn-gradient"><span class="glyphicons glyphicon-pencil"></span></a>
<a onclick="" href="${pageContext.request.contextPath}/major/delete?majorId=${list.majorId}" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicons glyphicon-trash"></span></a>
</div>
</td>
</tr>
</c:forEach>

</table>
<div class="pull-right">
<ul class="pagination" id="paginator-example">
<li><a href="#">&lt;</a></li>
<li><a href="#">&lt;&lt;</a></li>
<li><a href="#">1</a></li>
<li class="active"><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">&gt;</a></li>
<li><a href="#">&gt;&gt;</a></li>
</ul>
</div>
         <%--批量刪除--%>
<div class="pull-left">
<input type="submit" value="批量刪除" class="btn btn-default btn-gradient pull-right delall" >
</div>
</div>

</form>

第二步: dao實現

@Override
public int deleteMajorByIds(String[] ids) {
//delete from tb_major where majorId in ("01","04","05")
String sql = "delete from tb_major where major_id in (";
for (int i =0;i<ids.length-1;i++){
sql += ids[i]+",";
}
sql += ids[ids.length-1]+")";
System.out.println("sql語句"+sql);
return jdbcTemplate.update(sql);
}

第三步: controller 實現

@RequestMapping("/deleteMajorsById")
public String deleteMajorsById(String[] id){
int rows = majorDao.deleteMajorByIds(id);
System.out.println("刪除記錄條數:"+rows);
return "redirect:query";
}