js方法 讲form表单的数据 序列化返回obj对象 调用dataGrid的 load 方法
//查询按钮
$('#search').click(function(){
$('#chance').datagrid('load',serializeForm($('#searchForm')));
});
//js序列化表单返回 obj 对象
function serializeForm(form){
var obj = { };
$.each(form.serializeArray(),function(index){
if(obj[this['name']]){
obj[this['name']] = obj[this['name']] + ','+this['value'];
}else {
obj[this['name']] =this['value'];
}
});
return obj;
}
=========================Action层 方法跟数据展示一个方法体内=====================
//页面显示+条件查询
public void chanceList() throws Exception{
int pageNo = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));//当前页
int pageSize = Integer.parseInt(ServletActionContext.getRequest().getParameter("rows"));//每页有几条记录
//条件查询传递的参数
String khname = ServletActionContext.getRequest().getParameter("khname") == null ?"":ServletActionContext.getRequest().getParameter("khname");
String createtime = ServletActionContext.getRequest().getParameter("createtime")== null ?"":ServletActionContext.getRequest().getParameter("createtime");
String contact = ServletActionContext.getRequest().getParameter("contact")== null ?"":ServletActionContext.getRequest().getParameter("contact");
String telephone = ServletActionContext.getRequest().getParameter("telephone")== null ?"":ServletActionContext.getRequest().getParameter("telephone");
//将查询条件放到Map中
Map<String ,Object> m = new HashMap<String ,Object>();
m.put("khname", khname);
m.put("createtime", createtime);
m.put("contact", contact);
m.put("telephone", telephone);
//页面数据显示
//int total = chanceService.chanceCount();
//System.out.println(total);
List<Chance> chanceList = chanceService.findByPage(pageNo, pageSize,m);
List<Chance> list = chanceService.findAll();
String json = "{"total":"+list.size()+","rows":"+JSONArray.fromObject(chanceList).toString()+"}";
ServletActionContext.getResponse().getWriter().write(json);
}
==================Dao层 根据PageSize PageNo Map 参数查数据拼接SQL ==================================
//分页查询数据+ 条件查询
public List<Chance> findByPage(int pageNo,int pageSize,Map<String,Object> m){
String hql = " from Chance where 1 =1 ";
Set<Entry<String, Object>> set = m.entrySet();
Iterator io = set.iterator();
while(io.hasNext()){
Map.Entry<String, Object> me = (Map.Entry<String, Object>) io.next();
if("khname".equals(me.getKey())&& !"".equals(me.getValue())){
hql +=" and "+me.getKey()+" like '%"+me.getValue()+"%' ";
}
if("createtime".equals(me.getKey())&& !"".equals(me.getValue())){
hql +=" and "+me.getKey()+" = '"+me.getValue()+"'";
}
if("contact".equals(me.getKey())&& !"".equals(me.getValue())){
hql +=" and "+me.getKey()+" like '%"+me.getValue()+"%'";
}
if("telephone".equals(me.getKey())&& !"".equals(me.getValue())){
hql +=" and "+me.getKey()+" ='"+me.getValue()+"' ";
}
}
Query query = this.getSession().createQuery(hql);
//Hibernate数据库分页算法
query.setFirstResult((pageNo - 1) * pageSize);
query.setMaxResults(pageSize);
return query.list();
}