datagrid查询时如何加入自定义排序
datagrid对象查询时 排序只能按照某一字段升降排序,是否有方法实现自定义排序呢看UI标签库文档,有排序参数 UI标签库能多字段排序,但排序规则只能按照升降序。
如果后台添加dataGrid对象有此方法么,
没有只能自己写sql语句拼接了 自定义排序已解决。
1.在每个实体类对应的Service层的 setDataGrid()方法中,
在commonService.getDataGridReturn(cq, true);执行前加入如下判断:
//查询条件组装器
org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, employeeDeclare, parameterMap);
if(dataGrid.getSort()==null||"".equals(dataGrid.getSort())) {
//获取优先排序的状态码
Integer lv = jeecgRepo.getDeclareInitStatusNumber(user);
//自定义排序Sql语句
String customFormula = " case this_.declare_status when "+lv+" then 1 when "+(lv-1)+" then 2 when "+(lv-2)+" then 3 ELSE 4 END,this_.declare_status desc";
//新增公共方法 getDataGridReturn
commonRepo.getDataGridReturn(cq,true,"declareStatus",customFormula);
}else {
commonService.getDataGridReturn(cq, true);
}
2.commonRepo.getDataGridReturn()方法中将原先的代码中的排序去掉,替换成
criteria.addOrder(CustomOrder.getOrder(propertyName, customFormula));
而CustomOrder继承Order类
public class CustomOrder extends Order{
// private String propertyName;
private String customFormula;
protected CustomOrder(String propertyName,String customFormula) {
super(propertyName, true);
// this.propertyName=propertyName;
this.customFormula=customFormula;
// TODO Auto-generated constructor stub
}
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)throws HibernateException {
// String[] columns = criteriaQuery.getColumnsUsingProjection(criteria,propertyName);
//customFormula自定义你的字段
//for example
// return " case "+columns+" when 12 then 1 "
// + "when 11 then 2 "
// + "when 10 then 3 "
// + "else 4 end ,"+columns+" desc";
return customFormula;
}
public static CustomOrder getOrder(String propertyName,String customFormula) {
return new CustomOrder(propertyName, customFormula);
}
}
3.在相应申报List.jsp 页面 的dataGrid标签中删掉sortName="declareStatus" sortOrder="desc" 属性。
页:
[1]