简体   繁体   English

我们可以使用mongodb和Java进行数据表服务器端分页吗?

[英]Can we do datatables server side pagination using mongodb and java?

Actually I have done client side pagination using datatables but now requirement has changed due to large number of records around 100K. 实际上,我已经使用数据表完成了客户端分页,但是由于10万条左右的大量记录,现在的要求已更改。

I need to implement server side pagination. 我需要实现服务器端分页。

For that I used below code 为此,我使用下面的代码

GSP 普惠制

       $('#data-grid-table').dataTable( {
             "processing" : true,
             "sAjaxSource": dataUrl,
             "serverSide" : true,
             "sPaginationType": "full",
             "iDisplayLength": 25,
             "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
             "scrollX": true,
             "bFilter": false,
             "columnDefs": [ {
                  searchable: false,
                  "orderable": false,
                  className: "view-cell",
                  targets: 0
                }],
            "aaSorting": [[1,'asc']],
            "fnDrawCallback": function( oSettings ) {
                var callBackFlag = $("#hidden-view-flag").val()
                if(callBackFlag=="1"){
                    $("#hidden-view-flag").val("2")
                }
                if(callBackFlag == "2"){

                      $("#hidden-view-flag").val("3")
                }
                if(hideViewColumn){
                    $(".view-cell").hide();
                }
                $('.datasetTable,  tbody').find('tr').each(function(){
                    $(this).find('th:nth-child(1)').removeClass("sorting_asc");
                });


            }
        }); 

Controller 控制者

               dbObjArray = new BasicDBObject[2]
                 dbObjArray[0]= cruxLevel
                 dbObjArray[1] = project
                 List<DBObject> pipeline = Arrays.asList(dbObjArray)
                 if (!datasetObject?.isFlat && jsonFor != 'collection-grid') {
                     output= dataSetCollection.aggregate(pipeline)
                 }else{
                 //def skipRecords = params.iDisplayStart
                 //def limitRecords = params.iDisplayLength
                 //println 'params.iDisplayStart' + params.iDisplayStart
                 //println 'params.iDisplayLength' + params.iDisplayLength
                 println 'else-====================='
                 DBObject limit = new BasicDBObject('$limit':10);
                 DBObject skip = new BasicDBObject('$skip':5);
                 isFlatOutput = true;
                 dbObjArray = new BasicDBObject[3]
                 dbObjArray[0]= project
                 dbObjArray[1]= skip
                 dbObjArray[2]= limit
                 List<DBObject> pipeline1 = Arrays.asList(dbObjArray)

                     AggregationOptions aggregationOptions = AggregationOptions.builder()
                     .batchSize(100)
                     .outputMode(AggregationOptions.OutputMode.CURSOR)
                     .allowDiskUse(true)
                     .build();
                      SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                      Date now = new Date();
                      println 'Start time to fetch -------------------------------------' + sdfDate.format(now)
                      output = dataSetCollection.aggregate(pipeline1,aggregationOptions)
                      Date now1 = new Date();
                      println 'End time to fetch-------------------------------' + sdfDate.format(now1)


                 }  

                 if(isFlatOutput){
                     SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                     Date now2 = new Date();
                     println 'Start time to retrieve-------------------------------' + sdfDate.format(now2)
                     while(output.hasNext()) {
                         dataList.add(output.next());
                     }
                     Date now3 = new Date();
                     println 'End time to retrieve-------------------------------' + sdfDate.format(now3)
                 }

I was not getting how to take limit and skip so I have hard coded it. 我没有获得limitskip所以我对其进行了硬编码。

Actual result : 10 results are displaying but next is disabled. 实际结果 :显示10个结果,但next被禁用。

Expected result : 10 results should display and fetch next 10 records on click of next button. 预期结果 :应显示10个结果,并在单击“ next按钮时获取下10个记录。

Kindly tell me where I am going wrong. 请告诉我我要去哪里了。

def skipRecords
def limitRecords
if(params.iDisplayStart == null){
   skipRecords = 0;
}
if(params.iDisplayLength == null){
   limitRecords = 25;
}
def dbObjArrayTotal = new BasicDBObject[1]
dbObjArrayTotal[0]= project
List<DBObject> pipelineTotal = Arrays.asList(dbObjArrayTotal)
AggregationOptions aggregationOptions = AggregationOptions.builder()
                            .batchSize(100)
                            .outputMode(AggregationOptions.OutputMode.CURSOR)
                            .allowDiskUse(true)
                            .build();
def totalCount = dataSetCollection.aggregate(pipelineTotal,aggregationOptions)
totalCount = totalCount.size()
if(limitRecords == -1){
   limitRecords = totalCount
}
DBObject limit = new BasicDBObject('$limit':limitRecords);
DBObject skip = new BasicDBObject('$skip':skipRecords);

dbObjArray = new BasicDBObject[3]                                dbObjArray[0]= project
dbObjArray[1]= skip
dbObjArray[2]= limit
List<DBObject> flatPipeline = Arrays.asList(dbObjArray)
output = dataSetCollection.aggregate(flatPipeline,aggregationOptions)
def serverData = [
    "iTotalRecords" : totalCount,
    "iTotalDisplayRecords" : totalCount,
    "aaData": yourResult]
return serverData;

And above GSP is correct use as it is. 上面的GSP正确使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM