fnGetFilteredData()

fnGetFilteredData()

mikejmikej Posts: 8Questions: 0Answers: 0
edited December 2009 in Plug-ins
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

Replies

  • allanallan Posts: 57,730Questions: 1Answers: 9,203 Site admin
    Hi mikej,

    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
  • fvantfvant Posts: 6Questions: 0Answers: 0
    When i access the the returned array, i get the whole table when i do alert( a[0][1] ) instead of row 1. What am i missing ?
  • mikejmikej Posts: 8Questions: 0Answers: 0
    fvant here's what I do:

    [code]
    var data, i, row;

    data = gTbl.fnGetFilteredData();

    for (i = 0; i < data.length; i++) {
    row = data[i];
    doStuff(row);
    }

    [/code]
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    This is perfect, except that instead of iLen=oSettings.aiDisplay.length , I want to get the total number of results. I can't find any reference on how to get that number.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    oSettings._iRecordsTotal ?
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    Nope, that returns the total count of all records, not the count of filtered records. I'll go digging through the JS tonight to see what I can find.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    _iRecordsDisplay
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    edited July 2011
    BINGO! Thanks :)

    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.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    oTable / oSettings doesn't exist until after you intialize your table. can't do it before the $('...').dataTable();

    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.
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    I'm using it to save the filtered results for post-processing and printing to a PDF when the Save button is clicked (#btnSave). Here is the code I'm using:

    [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
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    edited July 2011
    More info gotten from firebug...the error doesn't occur until i=25 (which is the pagination limit that is currently selected). i can see it increment properly, and the result array getting the data properly appended, but as soon as i=25...*BOOM*, it all falls apart.

    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.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited July 2011
    don't use _iDisplayLength, this is the number of rows MAX on a page (the value from your Length select drop-down list), when you might have fewer than that number

    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.
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    [quote]fbas said: don't use _iDisplayLength[/quote]
    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.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    even with server side processing, the oSettings.aoData array is filled in. yes, it will only have one page worth (maximum) of results, but the values are there and that routine will work.

    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
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    [quote]fbas said: 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 [/quote]

    This would be done something like this, right?
    [code]$.fn.dataTableExt.afnFiltering.push(....)[/code]
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    see http://api.jquery.com/jQuery.ajax/

    [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.
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    Suh-weeeet. That's just what I needed. Once I get all the kinks worked out, I'll post the link to show it in action. Thanks a bazillion. I owe you a beer :)
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    Consider making a small donation to keep Allan's project developing. that's more than enough for me.
  • dmolavidmolavi Posts: 65Questions: 0Answers: 0
    Did that already yesterday. May have to do it again today :)
This discussion has been closed.