Filtering Array of Objects by attribute and not by array Index

Filtering Array of Objects by attribute and not by array Index

billpullbillpull Posts: 11Questions: 0Answers: 0
edited January 2013 in General
I want to create a date range filter that can be applied to a number of datatables. The problem I am having is even though I am passing
in my aaData value as an array of objects by the time I get to the filter function it is in an array. This wont work because the index of the date value in the array is different in the various dataTables throughout my application and I would much rather be able to do it via dot notation on an attribute something like this.

[code]
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var iMin = document.getElementById('start-datepicker').value;
var iMax = document.getElementById('end-datepicker').value;

// Create Minimum Date Object
var iMinDate = new Date(iMin);

// Create Maximum Date Object
var iMaxDate = new Date(iMax);

// Create Date Column Object
var iDateStr = aData.date;
var iDate = new Date(iDateStr);

if ( iMinDate < iDate && iDate < iMaxDate )
{
return true;
}
return false;
}
);
[/code]

Replies

  • billpullbillpull Posts: 11Questions: 0Answers: 0
    edited January 2013
    This might be a hack but it seems to work got the data via the oSettings.aoData object and the iDataIndex argument.

    [code]
    $.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
    var iMin = document.getElementById('start-datepicker').value;
    var iMax = document.getElementById('end-datepicker').value;

    // Create Minimum Date Object
    var iMinDate = new Date(iMin);

    // Create Maximum Date Object
    var iMaxDate = new Date(iMax);

    // Create Date Column Object
    var rowData = oSettings.aoData[iDataIndex]._aData;
    var iDateStr = rowData.date;
    var iDate = new Date(iDateStr);

    if ( iMinDate < iDate && iDate < iMaxDate )
    {
    return true;
    }
    return false;
    }
    );
    [/code]
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Yup - that looks like one way of doing it at the moment. I think this is either a bug or a lacking feature in the filtering API, as I note in your other thread here: http://datatables.net/forums/discussion/13475/using-array-of-object-when-loading-from-javascript-array-and-not-ajax#Item_4

    Allan
This discussion has been closed.