Datatables Server-side processing

参考:
https://www.datatables.net/manual/server-side
http://www.tuicool.com/articles/NBBnum

cdn加载文件

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js">script>
//serverside
function initDataTableServerSide(){
    $("#table_id").html("");
    $(".spinner").show();

    $('#table_id').DataTable({
        "language": {
            url:'//cdn.datatables.net/plug-ins/1.10.15/i18n/Chinese.json'
        },
       "scrollX":true, 
       "order": [[ 7, "desc" ]],
       "processing": true, 
       "serverSide": true, 
       "ajax": {
           url:ctx+"/index/qidian/getServersideDataList",
           dataFilter: function(data){
                var json = jQuery.parseJSON(data);
                json=json.data;
                json.draw = json.draw;
                json.recordsTotal = json.recordsTotal;
                json.recordsFiltered = json.recordsFiltered;
                json.data = json.data;

                return json; // return JSON string
            }
       },
       "columns": [
                      { data: "ip_name" ,title:"名称",orderable: false},
                      { data: 'q_author' ,title:"作者",orderable: false},
                      { data: 'q_label1' ,title:"label1",orderable: false},
                      { data: 'q_label2' ,title:"label2",orderable: false},
                      { data: 'q_label3' ,title:"label3",orderable: false},
                      { data: 'q_label4' ,title:"label4",orderable: false},
                      { data: 'q_label5' ,title:"label5",orderable: false},
                      { data: 'q_month_count' ,title:"月票"},
                      { data: 'q_month_order_num' ,title:"月票排名" },
                      { data: 'q_rec_count' ,title:"周推荐票数" },
                      { data: 'q_rec_order_num' ,title:"周推荐票数排名" },
                      { data: 'q_score' ,title:"分数" },
                      { data: 'q_week_click_num' ,title:"周点击数" },
                      { data: 'q_user_count' ,title:"评价人数" },
                      { data: 'q_week_rec_num' ,title:"周推荐数" },
                      { data: 'q_total_click_count' ,title:"总点击数" },
                      { data: 'q_thread_cnt' ,title:"作品讨论数" },
                      { data: 'q_month_order_num' ,title:"月票排名" },
                      { data: 'source_url' ,title:"来源地址",orderable: false}
                  ], 
        bProcessing: true
    });
    $(".spinner").hide();
}

后端用的jfinal框架

public void getServersideDataList(){
        Enumeration<String> paramNames = getParaNames();
        /**
         * paramNames:
         * [draw, columns[0][data], columns[0][name], columns[0][searchable], columns[0][orderable], columns[0][search][value], columns[0][search][regex], 
         *        columns[1][data], columns[1][name], columns[1][searchable], columns[1][orderable], columns[1][search][value], columns[1][search][regex], ……, 
         *        order[0][column], order[0][dir], start, length, search[value], search[regex], _]
         */
        while(paramNames.hasMoreElements()){
            String paraName = paramNames.nextElement();
            System.out.println("paraName:"+paraName+" paraValue:"+getPara(paraName));
        }

        String draw = getPara("draw");
        String start = getPara("start");
        String length = getPara("length");

        int pageNumber = Integer.valueOf(start)/Integer.valueOf(length)+1;
        StringBuilder select = new StringBuilder(" select * ");
        StringBuilder sqlExceptSelect = new StringBuilder(" from T_IP_MATERIAL where source_id = '0' ");

        String searchValue = getPara("search[value]");
        if(StringUtil.isNotEmpty(searchValue)){

        }

        String orderColumnNo = getPara("order[0][column]");
        if(StringUtil.isNotEmpty(orderColumnNo)){
            String orderColumnName = getPara("columns["+orderColumnNo+"][data]");
            String orderDir = getPara("order[0][dir]");
            sqlExceptSelect.append(" order by cast(" + orderColumnName + " as decimal) " + orderDir);
        }
        Page<IPMaterialQD> page = IPMaterialQD.getPaginate(pageNumber,Integer.valueOf(length),select.toString(),sqlExceptSelect.toString());
        renderSuccess(toJson(page.getList(), page.getTotalRow(),Integer.valueOf(draw)) ,null);
    }

    public String toJson(List<IPMaterialQD> list,int count,int draw){
        String json = null;
        String aaData = JsonKit.toJson(list);
        json = "{\"draw\":"+draw+",\"recordsTotal\":"+count+",\"recordsFiltered\":"+count+",\"data\":"+aaData+"}";
        return json;
    }

你可能感兴趣的:(前端)