fnGetFilteredData()
fnGetFilteredData()
This is a new API function. Its like fnGetFilteredNodes() but instead returns the data.
I needed this because I wanted to work on the post-filtered data before it had be rendered.
[code]
/*
* Function: fnGetFilteredData()
* Purpose: Retrieve an array with all data that survived filtering
* by mikej
*/
$.fn.dataTableExt.oApi.fnGetFilteredData = function ( oSettings ) {
var a = [];
for ( var i=0, iLen=oSettings.aiDisplay.length ; i
I needed this because I wanted to work on the post-filtered data before it had be rendered.
[code]
/*
* Function: fnGetFilteredData()
* Purpose: Retrieve an array with all data that survived filtering
* by mikej
*/
$.fn.dataTableExt.oApi.fnGetFilteredData = function ( oSettings ) {
var a = [];
for ( var i=0, iLen=oSettings.aiDisplay.length ; i
This discussion has been closed.
Replies
Nice one! Thanks very much for posting that. I've put it on the plug-ins page now as I'm sure others will find it useful as well: http://datatables.net/plug-ins/api#fnGetFilteredData
If you would like a link on the credit, or for it to be in any other way altered, please just give me a shout.
Regards,
Allan
[code]
var data, i, row;
data = gTbl.fnGetFilteredData();
for (i = 0; i < data.length; i++) {
row = data[i];
doStuff(row);
}
[/code]
Edit:
OK, maybe it's been a long day, but I'm putting this code before my $(document).ready(function() { in my header (and thus before my datatables object gets initialized, but after the datatables source is included), but firebug is spitting out this:
oSettings.aoData[oSettings.aiDisplay[i]] is undefined
a.push(oSettings.aoData[ oSettings.aiDisplay[i] ]._aData);
I'm running datatables 1.8.0.
you can't get oSettings until you have your table, and call fnSettings() on it. and that amount will change with any change in the number of rows or filter.
what are you using this for? you probably want to put it in one of your callback functions.
[code] var oTable, oSettings;
$(document).ready(function() {
$('input:submit,button').button();
oTable = $('#item_table').dataTable({
"bFilter": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"iDisplayLength" : 25,
"sDom": '<"H"lfr>t<"F"ip>',
"bSort": true,
"bProcessing": true,
"bServerSide":true,
"sAjaxSource": "edit.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "member_id", "value": member_id } );
aoData.push( { "name": "ref", "value": "get_all" } );
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
"bAutoWidth": false,
"oLanguage": {
"sSearch": "Search:"
},
"aoColumns": [
{ "bSortable": false, "sClass": "control center read_only", "mDataProp": null, "sDefaultContent":'' },
{ "bSearchable": true, "bSortable": true, "sClass": "read_only" },
{ "bSearchable": true, "bSortable": true, "sClass": "read_only" },
{ "bSearchable": true, "bSortable": false, "sClass": "read_only" },
{ "bSearchable": true, "bSortable": true, "sClass": "read_only" },
{ "bSearchable": true, "bSortable": false, "sClass": "read_only" },
{ "bSearchable": false, "bSortable": false, "bVisible":false, "sClass": "read_only" },
{ "bSearchable": false, "bSortable": false, "bVisible":false, "sClass": "read_only" },
{ "bSearchable": false, "bSortable": false, "bVisible":false, "sClass": "read_only" },
]
}).makeEditable({});
$('#btnSave').button({icons: {primary:'ui-icon-disk'},
disabled: false
}).click(function() {
oSettings = oTable.fnSettings();
var filteredData = oTable.fnGetFilteredData();
$.ajax({
type: "POST",
dataType: "json",
data: filteredData,
url: "tprint.php"
});
});
});
$.fn.dataTableExt.oApi.fnGetFilteredData = function ( oSettings ) {
var a = [];
for ( var i=0, iLen=oSettings._iRecordsDisplay ; i
Upon further debug, it appears that aoData only has 25 elements (again, based on the number of displayed rows).
Since I want to be able to pull *all* the filtered rows, what's the best way to do this, without forcing the user to manually change the pagination? I have a nagging suspicion that since I'm using server-side processing, this may not be possible, or if it is, it's just barely beyond my understanding of datatables to make it work...
Edit: Bah, I was hoping the fnGetHiddenTrNodes plugin would do it, but that doesn't appear compatible with server side processing...dangit.
use oSettings.aoData.length (or aiDisplay.length, like the example in the initial post)
apparently in your sample run, you had 25 real rows (starting at 0 to 24), so it blew up at #25.
If you mean not to set that in my initialization, removing it had no bearing on the outcome. Like I said, I think it has to do with the fact that I'm using server side data, so aoData will never contain all of the filtered results, only those that are being displayed.
if you need all the matching records with a filter, craft your own ajax call to the server side code, without specifying the iDisplayLength limitation (or set it to -1) and start your iDisplayStart at 0
This would be done something like this, right?
[code]$.fn.dataTableExt.afnFiltering.push(....)[/code]
[code]
var oSettings = oTable.fnSettings();
var aCols = array();
var iCols = oSettings.aoColumns.length;
for (i=0; i< iCols; i++)
aRows.push(oSettings.aoColumns[i].sName);
var sCols = aCols.join(',');
var sSrch = get_search_string_from_wherever();
$.ajax({
url: "server_processing.php",
datatype: 'json',
data: {
sSearch: sSrch,
sColumns: sCols,
iDisplayStart: 0,
iColumns: iCols
},
success: function(data, textStatus, jqXHR) {
// data contains your json.. do whatever you need
}
};
[/code]
something like that. you'll have to check the details at http://www.datatables.net/usage/server-side to put together all the parameters that your server side script might want.