jquery dataTables php服务类改成jFinul工具类

把dataTables例子中php版的服务类改成了一个jFinul工具类,并根据实际业务加入了关联表的处理。

工具类代码:



public class DataTablesUtil {
    public static final DataTablesUtil util = new DataTablesUtil();
    public static Map<String,Object> query(String[] aColumns, String sTables, Controller controller){
    //分页数据
        String sLimit = "";
        if(!StringKit.isBlank(controller.getPara("iDisplayStart")) && controller.getPara("iDisplayLength") != "-1"){
            sLimit = "limit " + controller.getParaToInt("iDisplayStart") + ", " + controller.getParaToInt("iDisplayLength");
        }
        //排序
        String sOrder = "";
        if(!StringKit.isBlank(controller.getPara("iSortCol_0"))){
            sOrder = "ORDER BY  ";
            int count = controller.getParaToInt("iSortingCols");
            if(count<aColumns.length){
                for(int i=0; i < count; i++){
                    if("true".equals(controller.getPara("bSortable_" + controller.getPara("iSortCol_" + i)))){
                        String column = aColumns[controller.getParaToInt("iSortCol_"+ i) ];
                        column = util.getColumnName(column);
                        sOrder += (column + ("asc".equals(controller.getPara("sSortDir_"+i))? " asc" : " desc") + ", ");
                    }
                }
            }
            sOrder = sOrder.substring(0, sOrder.length() - 2);
            if("ORDER BY".equals(sOrder)){
                sOrder = "";
            }
        }
        //过滤
        String sWhere = "";
        if(!StringKit.isBlank(controller.getPara("sSearch"))){
            sWhere = "WHERE (";
            for(int i=0; i<aColumns.length; i++){
                String column = util.getColumnName(aColumns[i]);
                sWhere += column + " LIKE '%" + controller.getPara("aSerch")+"%' OR ";
            }
            sWhere = sWhere.substring(0, sWhere.length()-3);
            sWhere += ")";
        }
        //列过滤
        for(int i=0; i<aColumns.length; i++){
            if(!StringKit.isBlank(controller.getPara("bSearchable_" + i)) 
                && "true".equals(controller.getPara("bSearchable_" + i))
                && !StringKit.isBlank(controller.getPara("sSearch_"+i))){
                if(StringKit.isBlank(sWhere)){
                    sWhere = "WHERE ";
                }else{
                    sWhere += " AND ";
                }
                sWhere += "`"+util.getColumnName(aColumns[i]) + "` LIKE '%" + controller.getPara("sSearch_"+i) + "%' ";
            }
        }
        String sColumns = " ";
        for(String col : aColumns){
            sColumns += col + ",";
        }
        sColumns = sColumns.substring(0, sColumns.length()-1);
        String sQuery = "SELECT SQL_CALC_FOUND_ROWS "+ sColumns +" FROM " + sTables+ " " + sWhere+" " + sOrder+ " " + sLimit;
        List<Record>  list = Db.find(sQuery);
        Long iFilteredTotal = Db.queryLong("SELECT FOUND_ROWS()");//结果集数量
        Map<String,Object> rs = new HashMap<String,Object>();
        String table = sTables.split(" ")[0];
        Long iTotal = Db.queryLong("SELECT COUNT(`id`) FROM " + table + "");//总记录
        rs.put("sEcho", controller.getParaToInt("sEcho"));
        rs.put("iTotalRecords", iTotal);
        rs.put("iTotalDisplayRecords", iFilteredTotal);//当前数量
        rs.put("aaData", list);
        return rs;
    }
/**
 * 获取真实的字段名,字段两端添加 `
 * 例 getColumnName("p.id as pk") 返回 p.`id`
 * @param column
 * @return
 */
    public String getColumnName(String column){
        if(column.indexOf(".") != -1){//判断有表名别名
            String[] _column_info = column.split("\\.");
            String _column_name = _column_info[1].trim();
            String regex = "";
            if(_column_name.toLowerCase().indexOf(" as ") != -1 || _column_name.indexOf(" ") != -1){//判断有字段别名
                if(_column_name.toLowerCase().indexOf(" as ") != -1){
                    regex = " +[a|A][s|S] +";
                }else if(_column_name.indexOf(" ") != -1){
                    regex = "\\s+";
                }
                _column_info[1] = _column_name.split(regex)[0]; //去除别名,只获取字段名称
            }
            column = _column_info[0] + ".`" + _column_info[1]+"`";
        }else{
            String regex = "";
            if(column.toLowerCase().indexOf(" as ") != -1 || column.trim().indexOf(" ") != -1){//判断有字段别名
                if(column.toLowerCase().indexOf(" as ") != -1){
                    regex = " +[a|A][s|S] +";
                }else if(column.indexOf(" ") != -1){
                    regex = "\\s+";
                }
                column = column.split(regex)[0];
            }
            column = "`"+column+"`";
        }
        return column;
    }
}

jFinal中调用


@ControllerBind(controllerKey = "/admin/me/exam/question",viewPath="/plugins/me/admin/exam/question")
public class ExamQuestionController extends BaseController {
    ...
    public void list(){
        String aColumns[] = new String[]{"p.id","p.title","d.name as depart_name","p.level"};
        String sTables = "`exam_paper` p LEFT JOIN `depart` d on p.`depart_id` = d.`id` ";
        Map<String,Object> rs = DataTablesUtil.query(aColumns, sTables, this);
        renderJson(rs);
    }
    ...
}

你可能感兴趣的:(java,jquery,jFinal,datatables)