ExtJs 前端代碼:
var piliang_audit=function(){
//取得所有選中的行
var record=grid.getSelectionModel().getSelections();
var sum = 0;//未審核的數據條數
var indexs = new Array();//存放所選擇數據行數的下標
var str = '[';
if(record.length>0){
for(var i = 0; i <record.length; i++){
if(record[i].get('audit_status').toUpperCase()!='Y'){
sum = sum + 1;
indexs.push(store.indexOf(record[i]));
str += "{detail_id:'"+record[i].get('detail_id');
str += "'}";
if(i<record.length-1){
str += ",";
}
}
}
str += "]";
if(sum>0){
Ext.Ajax.request({
method:'POST',
url:'pa_priceAction.do?actionType=piliang_audit',
success: function(response){
var json=Ext.decode(response.responseText);
if(json.success){
Ext.Msg.alert('提示',"批量審核成功!",function(){
for(var i=0;i<indexs.length;i++){
store.getAt(indexs[i]).set("audit_person",json.msg);
store.getAt(indexs[i]).set("audit_status","Y");
}
});
}else{
Ext.Msg.alert('提示',json.msg);
}
},
failure:function(response){
Ext.Msg.alert('提示','批量審核失敗!');
},
params:{
json:str
}
});
}else{
Ext.Msg.alert('提示','沒有需要審核的數據!');
}
}else{
Ext.Msg.alert('提示','請選擇要審核的記錄!');
}
}
Java後臺代碼:
public ActionForward piliang_audit(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws
IOException, ServletException, SQLException {
HashMap maplist = new HashMap();
String returnvalue = "";
boolean success = true;
String sqlString ="";
String ip = httpServletRequest.getRemoteHost();
String user_name = httpServletRequest.getSession().getAttribute("user_name").
toString();
String json1 = java.net.URLDecoder.decode(httpServletRequest.getParameter("json"), "utf-8").replace('"', ' ').trim();
//System.out.println(json1);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
DataSource datasource = null;
ServletContext context = null;
context = servlet.getServletContext();
datasource = (DataSource) context.getAttribute(Globals.DATA_SOURCE_KEY);
conn = datasource.getConnection();
stmt = conn.createStatement();
conn.setAutoCommit(false);
String temp_str="";
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
temp_str=sdf.format(dt);
try {
ArrayList list = json.jsonstrToarraylist(json1);
for (int i = 0; i < list.size(); i++) {
maplist = (HashMap) list.get(i);
sqlString = "update a set a.audit_status='Y',a.audit_date='"+temp_str+"',a.audit_person='"+user_name+"' from pa_price a "+
"where a.detail_id="+maplist.get("detail_id")+"";
stmt.addBatch(sqlString);
}
stmt.executeBatch();
returnvalue =user_name;
conn.setAutoCommit(true);
}
catch (SQLException e) {
conn.rollback();
conn.setAutoCommit(true);
System.err.println(e.getMessage());
success = false;
returnvalue = "批量審核失敗!";
}
finally {
if (rs != null) {
try {
rs.close();
}
catch (SQLException e) {
rs = null;
}
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {
stmt = null;
}
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
conn = null;
}
}
}
returnvalue = "{success:" + success + ",msg:'" + returnvalue + "'}";
try {
httpServletResponse.setContentType("text/json; charset=utf-8");
httpServletResponse.getWriter().write(returnvalue);
}
catch (IOException ex) {
ex.printStackTrace();
}
return actionMapping.findForward("");
}