datatables ajax异步分页

 $('#sample_1').dataTable({
                 "sAjaxSource": "../table/data",
//                 "bProcessing": true,
                 "bServerSide": true,
                 "fnServerData": function ( sSource, aoData, fnCallback ) {
                     console.log(111)
                     console.log(JSON.stringify(aoData));
                     $.ajax( {
                     "dataType": 'json',
                     "type": "POST",
                     "url": sSource,
                     "data": aoData,
                     "success": fnCallback
                     } );
                 },
                 "bDeferRender": true,// 是否启用延迟加载:当你使用AJAX数据源时,可以提升速度。默认值:False
//                        "aaData": [
//                            /* Reduced data set */
//                            [ "Trident", "Internet Explorer 4.0", "Win 95+", 4, "X" ],
//                            [ "Trident", "Internet Explorer 5.0", "Win 95+", 5, "C" ],
//                            [ "Trident", "Internet Explorer 5.5", "Win 95+", 5.5, "A" ],
//                            [ "Trident", "Internet Explorer 6.0", "Win 98+", 6, "A" ],
//                            [ "Trident", "Internet Explorer 7.0", "Win XP SP2+", 7, "A" ],
//                            [ "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", 1.8, "A" ],
//                            [ "Gecko", "Firefox 2", "Win 98+ / OSX.2+", 1.8, "A" ],
//                            [ "Gecko", "Firefox 3", "Win 2k+ / OSX.3+", 1.9, "A" ],
//                            [ "Webkit", "Safari 1.2", "OSX.3", 125.5, "A" ],
//                            [ "Webkit", "Safari 1.3", "OSX.3", 312.8, "A" ],
//                            [ "Webkit", "Safari 2.0", "OSX.4+", 419.3, "A" ],
//                            [ "Webkit", "Safari 3.0", "OSX.4+", 522.1, "A" ]
//                        ],
                "aoColumns": [
                  { "bSortable": false },
                  null,
                  { "bSortable": false },
                  null,
                  { "bSortable": false },
                  { "bSortable": false }
                ],
                "aLengthMenu": [
                    [5, 15, 20, -1],
                    [5, 15, 20, "All"] // change per page values here
                ],
                // set the initial value
                "iDisplayLength": 5,
                "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page",
                    "oPaginate": {
                        "sPrevious": "Prev",
                        "sNext": "Next"
                    }
                },
                "aoColumnDefs": [{
                        'bSortable': false,
                        'aTargets': [0]
                    }
                ]
            });

 

 

    @RequestMapping("/data")
    @ResponseBody
    public PageResult data(HttpServletRequest request,@ModelAttribute PageRequest pageRequest){
//        
        PageResult result = new PageResult();
        JSONArray aaData = new JSONArray();
        for (int i = pageRequest.getiDisplayStart(); i < pageRequest.getiDisplayStart()+pageRequest.getiDisplayLength(); i++) {
            JSONArray row = new JSONArray();
            row.add("a"+i);
            row.add("b"+i);
            row.add("c"+i);
            row.add("d"+i);
            row.add("e"+i);
            row.add("f"+i);
            aaData.add(row);
        }
        result.setAaData(aaData);
        result.setSEcho(pageRequest.getsEcho()+1);
        result.setITotalRecords(30);
        result.setITotalDisplayRecords(30);
        return result;
    }
import com.alibaba.fastjson.JSONArray;


public class PageResult {
    
    private Integer sEcho;//
    private Integer iTotalRecords;//实际的行数
    private Integer iTotalDisplayRecords;//过滤之后,实际的行数。
    private String sColumns;//可选,以逗号分隔的列名,;
    private JSONArray aaData;
    
    public PageResult() {
        super();
    }

    public Integer getSEcho() {
        return sEcho;
    }

    public void setSEcho(Integer sEcho) {
        this.sEcho = sEcho;
    }

    public Integer getITotalRecords() {
        return iTotalRecords;
    }

    public void setITotalRecords(Integer iTotalRecords) {
        this.iTotalRecords = iTotalRecords;
    }

    public Integer getITotalDisplayRecords() {
        return iTotalDisplayRecords;
    }

    public void setITotalDisplayRecords(Integer iTotalDisplayRecords) {
        this.iTotalDisplayRecords = iTotalDisplayRecords;
    }

    public String getSColumns() {
        return sColumns;
    }

    public void setSColumns(String sColumns) {
        this.sColumns = sColumns;
    }

    public JSONArray getAaData() {
        return aaData;
    }

    public void setAaData(JSONArray aaData) {
        this.aaData = aaData;
    }
    
    
    

}
public class PageRequest {
    
    private int sEcho;//页数
    private int iColumns;//
    private String sColumns;
    private int iDisplayStart;//开始行数
    private int iDisplayLength;//结束行数
    
    public int getsEcho() {
        return sEcho;
    }
    public void setsEcho(int sEcho) {
        this.sEcho = sEcho;
    }
    public int getiColumns() {
        return iColumns;
    }
    public void setiColumns(int iColumns) {
        this.iColumns = iColumns;
    }
    public String getsColumns() {
        return sColumns;
    }
    public void setsColumns(String sColumns) {
        this.sColumns = sColumns;
    }
    public int getiDisplayStart() {
        return iDisplayStart;
    }
    public void setiDisplayStart(int iDisplayStart) {
        this.iDisplayStart = iDisplayStart;
    }
    public int getiDisplayLength() {
        return iDisplayLength;
    }
    public void setiDisplayLength(int iDisplayLength) {
        this.iDisplayLength = iDisplayLength;
    }
    
    

}

 

posted @ 2017-02-08 17:30  晨羲  阅读(9931)  评论(0编辑  收藏  举报