Retrieve Class Name From
Retrieve Class Name From
bryceray1121
Posts: 65Questions: 0Answers: 0
I have specific columns in my table being assigned a class. I want to then check to see if a column has this class. However, i'm having difficulty discovering how to access the class of a column ().
[code] jQuery(tag).dataTable().fnSettings().aoColumns[i];[/code]
With a reference such as this, how would I then get the specified columns class name returned as a text value?
Thanks.
[code] jQuery(tag).dataTable().fnSettings().aoColumns[i];[/code]
With a reference such as this, how would I then get the specified columns class name returned as a text value?
Thanks.
This discussion has been closed.
Replies
As for your question, without knowing the full context, I'd say someelement.className
[code]
onclick="fnShowHide(this, '#labs-table',Array('Details','Summary')"
[/code]
This is the function:
[code]
function fnShowHide( obj, tag, textVal)
{
var tmpTable = jQuery(tag).dataTable();
var tmpCols = tmpTable.fnSettings().aoColumns;
var bVis = null;
for(var i=0;i<(tmpCols.length);i++){
if(tmpCols[i].className == "dataTables_notVisible"){
bVis = tmpTable.fnSettings().aoColumns[i].bVisible;
tmpTable.fnSetColumnVis( i, bVis ? false : true );
}
}
obj.innerHTML = bVis ? textVal[0] : textVal[1];
tmpTable.fnAdjustColumnSizing();
tmpTable.fnDraw();
}
[/code]
Before the datatables are rendered I give select columns a class "dataTables_notVisible". Then when I initiate dataTables I tell it to hide all columns with the class "dataTables_notVisible". The goal of the above function is to toggle the visibly of the columns with this class. I've tried className, but unless I'm doing something wrong it will not work for me. It works for a standard th object but appears not to work for a data tables object.
Thanks for your help. I've been working on this functionality for a while and this is the last piece I need to put it together.
Allan
[code]
function fnShowHide( obj, tag, textVal)
{
var tmpTable = jQuery(tag).dataTable();
alert(tmpTable+"-"+tmpTable.fnSettings());
var tmpCols = tmpTable.fnSettings().aoColumns;
var bVis = null;
for(var i=0;i<(tmpCols.length);i++){
if(jQuery(tmpCols[i].nTh).hasClass("dataTables_notVisible")){
bVis = tmpTable.fnSettings().aoColumns[i].bVisible;
tmpTable.fnSetColumnVis( i, bVis ? false : true );
}
}
obj.innerHTML = bVis ? textVal[0] : textVal[1];
tmpTable.fnAdjustColumnSizing();
tmpTable.fnDraw();
}
[/code]
This code works great in Firefox and chrome
It works most of the time in IE8.
However, in IE6/IE7 I'm running into a problem.
If you see in the above funciton I've printed some value out. What happens is after I run this function on a table in IE6/IE7 I get an error:
fnSettings().aoColumns is null or not an object.
The weird thing is, I'm more likely to get this error on the first half of tables I have then the last half. Once the error happens all the showhide functionality is broken. I understand this is a bizarre issue, let me know if there is any more info i can get you.
How did you do this part?
Then when I render it with datatables I tell datatables to hide columns with this class by default.
[code]
function renderDataTable(id){
var oTable2 = jQuery(id).dataTable( {
"sScrollY": "200px",
"sScrollX": "100%", /* Required for viewing tables with lots of columns at low resolution - otherwise columns are mis-aligned */
"bScrollCollapse":true,
"sDom": 'rt<"bottom"flp>',
"bPaginate": false,
"bStateSave": false,
"aoColumnDefs": [
{"bSearchable": false, "bSortable": false, "aTargets": ["dataTable-exclude"]},
{"bVisible":false,"aTargets":["dataTables_notVisible"]}
]
} );
[/code]
Could it be set somewhere in aoColumns in the Javascript?
I don't think I can even begin to explain that if it's working in other browsers! Does IE8 sometimes give this error as well? Does your alert() always show something useful (like [object Object]) in IE6/7, or is it undefined? What is jQuery(tag).dataTable().length?
Allan
In firefox it always prints out [object Object] - [object Object]
In IE it often ends up printing [object Object] - null
This is when i receive the error mentioned above.
--jQuery(tag).dataTable().length
I'm assuming by this you mean:jQuery(tag).dataTable().fnSettings().aoColumns.length. This is just a method to iterate through all the columns. I don't see any place that I attempt to use length directly on the data table method.
The other variable which is worth inspecting is $.fn.dataTableSettings - perhaps using Firebug Lite in IE you'd be able to see what is in there (should be the settings for each table).
Allan
Does $.fn.dataTableSettings contain sensible information (i.e. objects) when you get the error in IE? I'm floundering a bit with this one...!
Allan
[code]
$(window).resize(function() {
for(var i=0;i<(oTable.length);i++){
$.fn.dataTableExt.iApiIndex = i;
oTable.fnAdjustColumnSizing();
}
});
[/code]
So either just set iApiIndex here back to what it was, or in your fnShowHide function set it to 0. That should do it.
Thanks for packaging up the page to make it easier for me to load!
Allan