【发布时间】:2020-08-08 09:16:04
【问题描述】:
我正在实现一个复选框来选择所有行,但只选择当前页面中的行。
所以我试图访问返回的数据对象,以便我可以实现我的逻辑来检查所有复选框。
这是我的代码:
$.fn.dataTable.pipeline = function ( opts ) {
// Configuration options
var conf = $.extend( {
pages: 5, // number of pages to cache
url: opts.url, // script url
data: opts.data, // function or object with parameters to send to the server
// matching how `ajax.data` works in DataTables
method: 'post' // Ajax HTTP method
}, opts );
// Private variables for storing the cache
var cacheLower = -1;
var cacheUpper = null;
var cacheLastRequest = null;
var cacheLastJson = null;
return function ( request, drawCallback, settings ) {
var ajax = false;
var requestStart = request.start;
var drawStart = request.start;
var requestLength = request.length;
var requestEnd = requestStart + requestLength;
if ( settings.clearCache ) {
// API requested that the cache be cleared
ajax = true;
settings.clearCache = false;
}
else if ( cacheLower < 0 || requestStart < cacheLower || requestEnd > cacheUpper ) {
// outside cached data - need to make a request
ajax = true;
}
else if ( JSON.stringify( request.order ) !== JSON.stringify( cacheLastRequest.order ) ||
JSON.stringify( request.columns ) !== JSON.stringify( cacheLastRequest.columns ) ||
JSON.stringify( request.search ) !== JSON.stringify( cacheLastRequest.search )
) {
// properties changed (ordering, columns, searching)
ajax = true;
}
// Store the request for checking next time around
cacheLastRequest = $.extend( true, {}, request );
if ( ajax ) {
// Need data from the server
// console.log(requestLength)
if ( requestStart < cacheLower ) {
requestStart = requestStart - (requestLength*(conf.pages-1));
if ( requestStart < 0 ) {
requestStart = 0;
}
}
cacheLower = requestStart;
cacheUpper = requestStart + (requestLength * conf.pages);
request.start = requestStart;
request.length = requestLength*conf.pages;
// Provide the same `data` options as DataTables.
if ( typeof conf.data === 'function' ) {
// As a function it is executed with the data object as an arg
// for manipulation. If an object is returned, it is used as the
// data object to submit
var d = conf.data( request );
if ( d ) {
$.extend( request, d );
}
}
else if ( $.isPlainObject( conf.data ) ) {
// As an object, the data given extends the default
// console.log('=====================================')
// console.log('yes its a data')
$.extend( request, conf.data );
}
return $.ajax( {
"type": conf.method,
"url": conf.url,
"data": request,
"dataType": "json",
"cache": false,
"success": function ( json ) {
console.log(json)
cacheLastJson = $.extend(true, {}, json);
if ( cacheLower != drawStart ) {
json.aaData.splice( 0, drawStart-cacheLower );
}
if ( requestLength >= -1 ) {
json.aaData.splice( requestLength, json.aaData.length );
}
drawCallback( json );
}
} );
}
else {
json = $.extend( true, {}, cacheLastJson );
json.draw = request.draw; // Update the echo for each response
json.aaData.splice( 0, requestStart-cacheLower );
json.aaData.splice( requestLength, json.aaData.length );
drawCallback(json);
}
}
};
// Register an API method that will empty the pipelined data, forcing an Ajax
// fetch on the next draw (i.e. `table.clearPipeline().draw()`)
$.fn.dataTable.Api.register( 'clearPipeline()', function () {
return this.iterator( 'table', function ( settings ) {
settings.clearCache = true;
} );
} );
function triggerTable(data){
var table = $('#example').DataTable({
// fixedHeader: true,
"lengthMenu": [[10, 100, 500], [10, 100, 500]],
'processing': true,
'serverSide': true,
'serverMethod': 'post',
"order": [[ 5, "desc" ]],
"ajax": $.fn.dataTable.pipeline( {
url: '<?php echo base_url()?>Masters_cntrl/orgPartnerEmpList_AJAXPagination',
data: data,
pages: data.fetchCount, // number of pages to cache
} ),
'columns': [
{ data: 'checkbox' },
{ data: 'status' },
{ data: 'member_fullname'},
{ data: 'member_mobile_no' },
{ data: 'organization_name' },
]
});
return table;
}
var data = {
'org_id' : '<?php echo $org_id ?>',
'query' : '<?php echo $Query ?>',
'fetchCount' : 5,
};
var table = triggerTable(data);
而且,下面是我试图访问所有记录的代码
$('#selectAll').on('click', function(){
$("#example").find("tr").each(function (e) {
console.log(e)
});
});
但它只显示当前页面的十条记录。
【问题讨论】:
-
你有没有偶然尝试过this?
-
是的,它来自我正在开发的应用程序的代码。这是一个工作代码。我唯一需要的是访问返回的数据。
-
我需要遍历所有表格行,但是我无法循环超过前十条记录
标签: jquery datatables pipeline