基於Spring MVC + Mybatis實現多選框批量刪除
此專案基於的框架是:springmvc+mybatis,步驟如下:
A.前臺程式碼:
<th id="ckBox" style="text-align:center;width: 3%;" data-bind="visible: deleteqx == '1'">
<input type="checkbox" id="ckAll" onclick="CKAlls()" />
</th>
<td style="text-align:center;" data-bind="visible: deleteqx == '1'">
<div>
<input type="checkbox" name="subcheck" id="subcheck" data-bind="value: id" onclick="CKAllsOne()" />
</div>
</td>
備註
B.Js程式碼:
片段1:
/**
* 批量刪除
*/
self.ckdelete = function(item){
url="radar/ckDeletes";//後臺url
if(ckdeletes(url)){
$("#ckAll").attr('checked',false);
self.selectRadar();//刪除之後重新整理
}
}
片段2:
//批量刪除
function ckdeletes(url){
var exist = false;
var ckNum = $("input[name='subcheck']:checked").length;
if(ckNum == 0){
alert("請至少選擇一項!");
return exist;
}
if(confirm("確定刪除所選專案?")){
var checkedList = new Array();
$("input[name='subcheck']:checked").each(function(){
checkedList.push($(this).val());
});
$.ajax({
type:"POST",
url:"../../mvc/"+url,
data:{"delitems":checkedList.toString()},
datatype:"html",
async:false,
success:function(data){
exist = true;
alert("刪除成功!");
},
error:function(data){
alert("刪除失敗!");
return exist;
}
});
}
return exist;
}
/**
* 複選框全選或全不選
*/
function CKAlls(){
if ($("#ckAll").is(":checked")) {
$(":checkbox").prop("checked", true);//所有選擇框都選中
} else {
$(":checkbox").prop("checked", false);
}
}
function CKAllsOne(){
var count = 0;
$("input[type=checkbox][name=subcheck]").each(function(){
if($(this).attr('checked') != 'checked'){// 判斷一組複選框是否有未選中的
count+=1;
}
});
if(count == 0) { // 如果沒有未選中的那麼全選框被選中
$("#ckAll").attr('checked', true);
} else {
$("#ckAll").attr('checked',false);
}
}
注
接下來就是後臺操作了!
C.Controller層:
/**
* 批量刪除
* @param request
* @param response
* @throws IOException
*/
@ResponseBody
@RequestMapping("/ckDeletes")
public void ckDeletes(HttpServletRequest request,HttpServletResponse response) throws IOException{
String items = request.getParameter("delitems");
List<String> delList = new ArrayList<String>();
String[] strs = items.split(",");
User user=(User) request.getSession().getAttribute("user");
for (String str : strs) {
delList.add(str);
}
Map<String,Object> map = new HashMap<String,Object>();
map.put("delList",delList);
try{ radarService.batchDeletes(map);
msg = "success";
} catch (Exception e) {
msg = "fail";
}
String result = new GsonUtils().toJson(msg);
//String result = JSONArray.fromObject(list).toString();
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain;charset=utf-8");
response.getWriter().write(result);
response.getWriter().flush();
response.getWriter().close();
}
D.Service層:
/**
* 批量刪除
* @param delList
* @param request
*/
public void batchDeletes(Map map){
radarMapper.batchDeletes(map);
}
E.Mapper層:
/**
* 刪除
* @param map
*/
public void batchDeletes(Map map);
最後就是Mybatis了!
F.mapper.xml
<delete id="batchDeletes" parameterType="java.util.Map">
DELETE FROM t_radar where id in
<foreach collection="delList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
以上就是批量刪除的全部步驟了!