jqGrid 高级查询方法:
$('#trans_asearch').click(function(){ jQuery("#trans_lst").jqGrid('searchGrid',{multipleSearch:true}); });
向后台提交的请求为如下形式:
filters:{"groupOp":"OR","rules":[{"field":"startTime","op":"le","data":"2011-08-09"},{"field":"startTime","op":"ge","data":"2011-08-01"},{"field":"orderCode","op":"cn","data":"201108"}]}
后台组装处理类:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.swind.tools; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.util.HashMap; import java.util.Map; /** * * @author WangXiaoping */ public class SqlTools { public interface CallBack {//回调函数接口 public String executeData(String f,String o,String d); public String executeField(String f); } private static Map<String, String> Q2Oper; public SqlTools() { Q2Oper = new HashMap(); //['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc'] Q2Oper.put("eq", " = "); Q2Oper.put("ne", " <> "); Q2Oper.put("lt", " < "); Q2Oper.put("le", " <= "); Q2Oper.put("gt", " > "); Q2Oper.put("ge", " >= "); Q2Oper.put("bw", " LIKE "); Q2Oper.put("bn", " NOT LIKE "); Q2Oper.put("in", " IN "); Q2Oper.put("ni", " NOT IN "); Q2Oper.put("ew", " LIKE "); Q2Oper.put("en", " NOT LIKE "); Q2Oper.put("cn", " LIKE "); Q2Oper.put("nc", " NOT LIKE "); } public String constructWhere(String filter,CallBack cb) { String query=""; if (!filter.isEmpty()) { JsonObject jsono = new JsonParser().parse(filter).getAsJsonObject(); if (jsono.isJsonObject()) { String group = jsono.get("groupOp").getAsString(); JsonElement rules = jsono.get("rules"); int i = 0; for (JsonElement o : rules.getAsJsonArray()) { String field = o.getAsJsonObject().get("field").getAsString(); String op = o.getAsJsonObject().get("op").getAsString(); String data = o.getAsJsonObject().get("data").getAsString(); if (!op.isEmpty() && !data.isEmpty()) { i++; field = cb.executeField(field); data = cb.executeData(field, op, data); if(i==1) query=" AND "; else query +=" "+group+" "; if(op.equals("in")||op.equals("ni")){ query+= field+Q2Oper.get(op)+" ("+data+")"; } else { query+= field+Q2Oper.get(op)+data; } } } } } return query; } }
测试运行:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.swind.test; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.swind.tools.SqlTools; import com.swind.tools.SqlTools.CallBack; /** * * @author WangXiaoping */ public class GsonTest { public static void main(String[] arg){ String js = "{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"deptName\",\"op\":\"cn\",\"data\":\"工\"},{\"field\":\"fromType\",\"op\":\"eq\",\"data\":\"TFI10\"}]}"; String query = new SqlTools().constructWhere(js, new CallBack(){ public String executeData(String f, String o, String d) { if(o.equals("bw") || o.equals("bn")) return "'" + d + "%'"; else if (o.equals("ew") || o.equals("en")) return "'%"+d+"'"; else if (o.equals("cn") || o.equals("nc")) return "'%" +d+ "%'"; else return "'" +d+ "'"; } public String executeField(String f) { if(f.equals("deptName"))return "o.short_name"; else if(f.equals("fromType"))return "t.from_type"; else if(f.equals("orderCode"))return "t.order_code"; else if(f.equals("applicantName"))return "ta.fullname"; else if(f.equals("transType"))return "t.trans_type"; else if(f.equals("createTime"))return "t.create_time"; else if(f.equals("startTime"))return "t.start_time"; else if(f.equals("realFinishTime"))return "t.real_finish_time"; else if(f.equals("status"))return "t.status"; else return f; } }); System.out.println(query); } }