Plug-in for extracting column values (fnGetColumnData)
Plug-in for extracting column values (fnGetColumnData)
bforchhammer
Posts: 2Questions: 0Answers: 0
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...
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...
This discussion has been closed.
Replies
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
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.
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?
[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?
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;
}
Regards,
Allan