1. 程式人生 > >SSH+EasyUI批量刪除

SSH+EasyUI批量刪除

使用Spring+Struts2+Hibernate+EasyUI實現批量刪除功能,以下是效果圖片:


前端

批量刪除JS:

function onDeleteBatch() {
<span style="white-space:pre">	</span>var rows = $("#sysloglist").datagrid('getSelections');//獲取選中的行
	
	if(rows.length==0){
		$.messager.alert('提示','請選擇所需刪除的記錄!');
		return false;
	}
	var ids=[];  
	for (var i = 0; i < rows.length; i++) {  
        <span style="white-space:pre">	</span>var id=rows[i].id; //取出id值
    	<span style="white-space:pre">	</span>ids.push(id); //將id值放入陣列中
    <span style="white-space:pre">	</span>}  
    
<span style="white-space:pre">	</span>var params = $.param({'ids':ids},true);
<span style="white-space:pre">	</span>var url = 'system/syslog_deleteBatch';
	
	$.messager.confirm('提示', '確定所選記錄刪除?', function(r){
		
		if(r){
			$.ajax({
		        url : url,
		        data: params,
		        cache : false, 
		        async : false,
		        type : "POST",
		        dataType : 'json',
		        success : function (result){
		        	var msg = '';
		            if(result.success) {
		            	msg = result.msg;
		            } else {
		            	msg = '<span style="color: red;">' + result.msg + '</span>';
		            }
		            $.messager.show({
							title:'提示',
							msg:msg,
							timeout:1500,
							showType:'slide'
					});
					
					if(result.success) {
						$("#sysloglist").datagrid('reload');
					}
		        }
    		});
		}
	});
}
html頁面:
<body class="easyui-layout" data-options="fit:true,border:false">

	<div data-options="region:'center',title:'任務列表',fit:true" style="height:100%;background:#fafafa">   
		<table id="sysloglist" class="easyui-datagrid" style="height:100%;"
	        data-options="url:'system/syslog_grid.action',
	        			  border:false,
	        			  rownumbers:'true',
	        			  toolbar:'#tb',
	        			  pagination:true,
	        			  pageSize:'50',
	        			  pageList:[50,100,150],
	        			  fit:true,
	        			  view: detailview,
	        			  detailFormatter:detailFormatter">
		    <thead>
		        <tr>
		        	<th data-options="field:'ck',checkbox:true"></th>
		            <th data-options="field:'log',width:300">日誌</th>
		           	<th data-options="field:'status',width:80,align:'center'">狀態</th>
		           	<th data-options="field:'createdate',width:180,align:'center',formatter: formatDate">建立日期</th>
		            <th data-options="field:'operator',width:120,align:'center',formatter: formatAction">操作</th>
		        </tr>
		    </thead>
		</table> 
		
		<div id="tb">
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="onDeleteBatch()">批量刪除</a>  
		</div>
	</div> 
</body>
</html>
這裡使用了easyui的拓展外掛datagrid-detailview.js,當使用該外掛時,在頁面中就呈現如上圖的效果,點選每條記錄的“+”號會彈出自定義的資訊。如只使用easyui的datagrid控制元件,只需去掉table的data-options的view和detailFormatter屬性即可。

Action

public class SyslogAction extends BaseAction {
	private SyslogServiceI syslogService;
	private String[] ids;
	
	public void deleteBatch() {
		boolean success = syslogService.doDeleteBatch(ids);
		Feekback back = new Feekback();
		back.putSuccess(success);
		back.putMsg(success?"批量刪除成功!":"批量刪除失敗!");
		
		this.write(back.toString());
	}
	
	//--------//
	public SyslogServiceI getSyslogService() {
		return syslogService;
	}

	public void setSyslogService(SyslogServiceI syslogService) {
		this.syslogService = syslogService;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
}

Service

public class SyslogServiceImpl implements SyslogServiceI {
	private SyslogDAOI syslogDao;

	public boolean doDeleteBatch(String[] ids) {
		StringBuilder sb = new StringBuilder();
		for(String id : ids) {
			sb.append(id);
			sb.append(",");
		}
		String strIds = sb.substring(0, sb.length() - 1);
		String hql = "delete from Syslog l where l.id in (" + strIds + ")";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		int r = syslogDao.executeHql(hql);
		if(r != 0) {
			return true;
		}
		return false;
	}

	//----//
	public SyslogDAOI getSyslogDao() {
		return syslogDao;
	}

	public void setSyslogDao(SyslogDAOI syslogDao) {
		this.syslogDao = syslogDao;
	}
}

DAO

@Override
public int executeHql(String hql) {
	int result = -1;
	try {
	<span style="white-space:pre">	</span>Query q = this.getCurrentSession().createQuery(hql);
	result = q.executeUpdate();
	} catch(Exception e) {
		e.printStackTrace();
	}
	return result;
}