Plug-in for extracting column values (fnGetColumnData)

Plug-in for extracting column values (fnGetColumnData)

bforchhammerbforchhammer Posts: 2Questions: 0Answers: 0
edited March 2009 in Plug-ins
I've created a plugin some time ago and thought I'd post it here so it may help other people who are looking for the same feature...

The plugin allows developers to retrieve a list of all values from a certain column in a dataTable. By default it only returns unique non-empty values and respects active filters on the dataTable. (This behavior can be changed by adjusting respective function parameters).

var oDataTable = $('#table).dataTable();
var sColumnOneValues = oDataTable.fnGetColumnData(1);

The code is available here: http://m2bf.pastebin.com/f3f0174f8
Allan, please feel free to add it to the plug-ins section on the website...

Replies

  • allanallan Posts: 63,683Questions: 1Answers: 10,498 Site admin
    Hi,

    Fantastic plug-in. Thanks for sharing with us. I've put it up on the plug-ins page: http://datatables.net/plug-ins#api_fnGetColumnData

    Regards,
    Allan
  • jfablejfable Posts: 1Questions: 0Answers: 0
    edited February 2011
    Just a quick heads up for people that are using this plug-in on large tables (2k+ rows).

    I was getting an IE "stop running this script" error on and was having trouble debugging. After upgrading to 1.7.5 and reading some threads on IE and the inArray function, I finally figured out that the inArray call in fnGetColumnData was what was causing the error.

    What I did was set bUnique to false, then run the array of all column data, including duplicates, through the uniqueArray jQuery plugin (http://plugins.jquery.com/project/uniqueArray) which doesn't invoke inArray.

    Long story short, IE error is gone and the result is the same as having bUnique set to true.
  • andre2011andre2011 Posts: 2Questions: 0Answers: 0
    edited March 2011
    . please delete my comment
  • mirkecmirkec Posts: 8Questions: 0Answers: 0
    edited June 2011
    Good job on plugin.

    But, I experienced some issues with DT version 1.8.

    If you use "mDataProp" for defining column data, you get error saying "Cannot read property 'length' of undefined"!
    Since I'm bad at scripting, I just altered this line of code
    [code]
    // ignore empty values?
    if (bIgnoreEmpty == true && sValue.length == 0) continue;
    [/code]
    with
    [code]
    // ignore empty values?
    if (bIgnoreEmpty == true && ( sValue == null || sValue.length == 0)) continue;
    [/code]

    Is this ok?
  • pip8786pip8786 Posts: 1Questions: 0Answers: 0
    edited October 2011
    To solve the above and make this work with datatables that use mDataProp, insert this line:

    [code]if (typeof oSettings.aoColumns != "undefined") iColumn = oSettings.aoColumns[iColumn].mDataProp;[/code]

    after this line:
    [code] if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true; [/code]

    Not sure where to post this to have it updated in the plugins. Is there a better location?
  • gzhugzhu Posts: 3Questions: 0Answers: 0
    @ jfable:

    I got around the same slowness using a hash. even in IE the lookup is really fast :)

    var asResultData = new Array(), hash = {};
    ...
    else if (bUnique == true && hash[sValue]) continue;
    ...
    else {
    asResultData.push(sValue);
    hash[sValue] = true;
    }
  • allanallan Posts: 63,683Questions: 1Answers: 10,498 Site admin
    @gzhu: Nice idea. I've added it to my list to update the plug-in.

    Regards,
    Allan
This discussion has been closed.