我们可以使用mongodb和java进行datatables服务器端分页吗?
实际上,我已经使用datatables完成了客户端分页,但现在由于大约100K的大量记录,需求已经发生了变化 我需要实现服务器端分页 为此,我使用了下面的代码 普惠制我们可以使用mongodb和java进行datatables服务器端分页吗?,java,jquery,mongodb,pagination,datatables,Java,Jquery,Mongodb,Pagination,Datatables,实际上,我已经使用datatables完成了客户端分页,但现在由于大约100K的大量记录,需求已经发生了变化 我需要实现服务器端分页 为此,我使用了下面的代码 普惠制 $('#data-grid-table').dataTable( { "processing" : true, "sAjaxSource": dataUrl, "serverSide" : true, "sPagi
$('#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");
});
}
});
控制器
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)
}
dbObjArray=新的基本对象[2]
dbObjArray[0]=cruxLevel
dbObjArray[1]=项目
List pipeline=Arrays.asList(dbObjArray)
如果(!datasetObject?.isFlat&&jsonFor!=“收集网格”){
输出=dataSetCollection.aggregate(管道)
}否则{
//def skipRecords=params.iDisplayStart
//def limitRecords=params.iDisplayLength
//println'params.iDisplayStart'+params.iDisplayStart
//println'params.iDisplayLength'+params.iDisplayLength
打印其他内容-================================================'
DBObject limit=新的BasicDBObject(“$limit”:10);
DBObject skip=新的基本CdbObject(“$skip”:5);
isFlatOutput=true;
dbObjArray=新的基本对象[3]
dbObjArray[0]=项目
dbObjArray[1]=跳过
dbObjArray[2]=限制
List pipeline1=Arrays.asList(dbObjArray)
AggregationOptions AggregationOptions=AggregationOptions.builder()
.批量大小(100)
.outputMode(聚合选项.outputMode.CURSOR)
.allowDiskUse(真)
.build();
SimpleDataFormat sdfDate=新的SimpleDataFormat(“yyyy-MM-dd HH:MM:ss.SSS”);
现在日期=新日期();
println'提取的开始时间-------------------------------'+sdfDate.format(现在)
输出=dataSetCollection.aggregate(管道1,aggregationOptions)
现在日期1=新日期();
println'提取的结束时间-------------------------------'+sdfDate.format(now1)
}
如果(isFlatOutput){
SimpleDataFormat sdfDate=新的SimpleDataFormat(“yyyy-MM-dd HH:MM:ss.SSS”);
现在日期2=新日期();
println'检索的开始时间-------------------------------'+sdfDate.format(now2)
while(output.hasNext()){
dataList.add(output.next());
}
现在日期3=新日期();
println'检索的结束时间-------------------------------'+sdfDate.format(now3)
}
我不知道如何采取限制
和跳过
,所以我硬编码了它
实际结果:显示10个结果,但禁用了下一步
预期结果:点击下一步
按钮,应显示10条结果并获取下10条记录
请告诉我哪里出了问题。def skipRecords
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;
def有限公司记录
if(params.iDisplayStart==null){
skiprecards=0;
}
if(params.iDisplayLength==null){
有限记录=25;
}
def dbObjArrayTotal=新的基本对象[1]
dbObjArrayTotal[0]=项目
List pipelineTotal=Arrays.asList(dbObjArrayTotal)
AggregationOptions AggregationOptions=AggregationOptions.builder()
.批量大小(100)
.outputMode(聚合选项.outputMode.CURSOR)
.allowDiskUse(真)
.build();
def totalCount=dataSetCollection.aggregate(pipelineTotal、aggregationOptions)
totalCount=totalCount.size()
如果(limitRecords==-1){
limitRecords=totalCount
}
DBObject limit=新的BasicDBObject(“$limit”:limitRecords);
DBObject skip=new BasicDBObject(“$skip”:skipprecords);
dbObjArray=new basicdbobobject[3]dbObjArray[0]=project
dbObjArray[1]=跳过
dbObjArray[2]=限制
List flatpipline=Arrays.asList(dbObjArray)
输出=dataSetCollection.aggregate(扁平管道、聚合选项)
def服务器数据=[
“iTotalRecords”:totalCount,
“iTotalDisplayRecords”:totalCount,
“aaData”:您的结果]
返回服务器数据;
以上GSP是正确的用法