fnGetColumnData

Get the data from a column

  • Author: Benedikt Forchhammer
  • Deprecated: This plug-in has been deprecated and replaced with other functionality. Please see the detailed description below for more information.

Return an array of table values from a particular column, with various filtering options.

DataTables 1.10+ provides the column().data() method, built-in to the core, to provide this ability. As such, this method is marked deprecated, but is available for use with legacy version of DataTables. Please use the new API if you are used DataTables 1.10 or newer.

Plug-in code

jQuery.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
    // check that we have a column id
    if ( typeof iColumn == "undefined" ) {
        return [];
    }

    // by default we only wany unique data
    if ( typeof bUnique == "undefined" ) {
        bUnique = true;
    }

    // by default we do want to only look at filtered data
    if ( typeof bFiltered == "undefined" ) {
        bFiltered = true;
    }

    // by default we do not wany to include empty values
    if ( typeof bIgnoreEmpty == "undefined" ) {
        bIgnoreEmpty = true;
    }

    // list of rows which we're going to loop through
    var aiRows;

    // use only filtered rows
    if (bFiltered === true) {
        aiRows = oSettings.aiDisplay;
    }
    // use all rows
    else {
        aiRows = oSettings.aiDisplayMaster; // all row numbers
    }

    // set up data array    
    var asResultData = [];

    for (var i=0,c=aiRows.length; i<c; i++) {
        var iRow = aiRows[i];
        var sValue = this.fnGetData(iRow, iColumn);

        // ignore empty values?
        if (bIgnoreEmpty === true && sValue.length === 0) {
            continue;
        }

        // ignore unique values?
        else if (bUnique === true && jQuery.inArray(sValue, asResultData) > -1) {
            continue;
        }

        // else push the value onto the result data array
        else {
            asResultData.push(sValue);
        }
    }

    return asResultData;
};

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

var table = $('#example').dataTable();
  table.fnGetColumnData( 3 );