今天为了写个批量删除纠结了一天,主要是前台获取到选中的记录的id后,无法传到后台,晚上终于解决了,主要就是将要删除的记录的id拼接成字符串,以逗号分隔开来,后台在用数组接收。
1、前台Extjs代码如下:
dodeleteall:function(){
var record= Ext.getCmp("grid02").getSelectionModel().getSelection();
if(record.length>1){
var list='';
for(var i= 0 , length= record.length;i<length;i++){
list+=record[i].get('unitId');
if (i != length - 1) {
list += ',';
}
};
Ext.Msg.show({
title:'删除',
msg:'确定要删除'+record.length+'条数据吗?',
icon: Ext.MessageBox.WARNING,
buttons: Ext.MessageBox.YESNO,
fn:function(btn){
if(btn=='yes'){
Ext.Ajax.request({
url: '/dodeleteall02?unitids=' + list,
success: function (response) {
var msg = Ext.JSON.decode(response.responseText);
var totalCount = Ext.getCmp('grid02').store.getTotalCount(); // 所有的记录数,不单单是当前页展示的数据
var pageSize = Ext.getCmp('grid02').store.pageSize; // 一页上面展示的记录条数
var curPage = Ext.getCmp('grid02').store.currentPage; // 当前页码
var fromRecord = ((curPage - 1) * pageSize) + 1; // 当前页展示的起始记录号
var toRecord = Math.min(curPage * pageSize, totalCount); // 当前页展示的结尾记录号
var totalOnCurPage = toRecord - fromRecord + 1; // 当前页展示的记录条数
var totalPageCount = Math.ceil(totalCount / pageSize); // 总的页数
var delCount = length1;// 删除的记录条数
//若当前页是最后一页,且不是仅有的一页,且删除的记录数是当前页上的所有记录数
if (curPage === totalPageCount && totalPageCount != 1 && delCount == totalOnCurPage)
{
Ext.getCmp('grid02').store.currentPage-1;
Ext.getCmp('grid02').store.loadPage(Ext.getCmp('grid02').store.currentPage-1);
}
Ext.MessageBox.show({
title: '成功',
msg: msg.message,
icon: Ext.MessageBox.WARNING,
buttons: Ext.MessageBox.YES
});
Ext.getCmp('grid02').store.reload();
},
failure: function (response) {
var msg = Ext.JSON.decode(response.responseText);
Ext.MessageBox.show({
title: '失败',
msg: msg.msg,
icon: Ext.MessageBox.QUESTION,
buttons: Ext.MessageBox.YES
});
}
});
}
}
})
}
}
2、后台Java代码如下:
public String deleteall(){
String arr[]=unitids.split(",");
for(int i =0;i<arr.length;i++){
int a =dao.extcuteHQL("delete from TMeUnitInfoEntity WHERE unitId=?", Integer.valueOf(arr[i]));
if(a>0){
success=true;
setMessage("删除商品信息成功!");
}else{
success=false;
setMessage("删除商品信息失败!");
}
}
return "ok";
}