Finding column index of hidden column

Finding column index of hidden column

matthttammatthttam Posts: 40Questions: 2Answers: 0
edited November 2012 in DataTables 1.9
Hello again!

I have been working around this problem for some time now and I have broken down and decided to just ask. My webpage is used to keep track of laptops, their problems, their assignments, and the students they were assigned to. Naturally I have 4 tables: WO, Assignments, computers, students.

The WO page has 17 columns (0 through 16). I am working on reducing copy and paste by creating functions in the top of the page and calling these functions when building the datatables object. During the fnRowCallback function in the WO table there is some logic that sets CSS around the word in the 2nd column (id 1) that highlights it a shade of a color. This is determined by the timestamp stored in hidden column id 16 compared to the current timestamp (to find duration). The longer it has been open the darker the color gets.

The problem I am having is that the only way to get the data from the hidden column named timestamp is to do aData[16]. Which of course will break on me when a month down the road I need to add another column for who knows what... In order to make things in general more flexible I have been changing things so that it dynamically determines the index based off the name of the th tag in the tfoot.

For instance I did this in a function I built to create dropdowns based off the css class which accounts for hidden columns that throw the index off by using your fnVisibleToColumnIndex plugin (which didn't have any usage examples but I pushed buttons until it worked).
[code]
function build_dropdowns(tableID, json, refName){
$("#"+tableID+" tfoot th.dropdown").each( function () {
index = refName.fnVisibleToColumnIndex($(this).index());
$(this).html(fnCreateSelect(json.select[index], index));
$('select', $(this)).change( function () {
refName.fnFilter( $(this).val(), refName.fnVisibleToColumnIndex($(this).parent().index() ));
} );
} );
}
[/code]

Now however, I need to get the column index of a column that isn't existing in the DOM.

I initially tried something like: (WHERE unix is a unix timestamp)
[code]
duration = (unix - aData[
wTable.fnVisibleToColumnIndex($('#WO tfoot [name="timestamp"]').index())
])*1;
[/code]

But of course after some testing I realized that $('#WO tfoot [name="timestamp"]') will select something (console.log ouptuts it as just []) but index() returns -1 which I guess means not found or hidden.

So, how within the fnRowCallback function could I dynamically retrieve the index of a column regardless of its existence?

On a side note too... you may notice the code towards the beginning gets passed refName (which is the name of the object that is being assigned datatables(). Is there a way to get this object itself using a select statement so I wouldn't have to pass wTable and WO?

Thanks for the help!
Bil

Replies

  • matthttammatthttam Posts: 40Questions: 2Answers: 0
    I went to lunch and had an idea... I got back and google searched a bit and figured it out.

    This is my function:
    [code]
    function Lookup_RealColumnIndex(table,needle){
    for(var i = 0, m = null; i < table.fnSettings().aoColumns.length; ++i) {
    if(table.fnSettings().aoColumns[i].sTitle != needle)
    continue;
    return(i);
    break;
    }
    }
    [/code]

    You pass it the table name (declared when you did oTable = $('#oTable').datatable({ ... });
    And the needle which is the column header I think? For me it was timestamp. This iterates through the aoColumns and sees if sTitle of each object is the needle. If it is return the index. If not you will get null.

    The second half of my question though is still up for grabs. An comments on this solution are greatly appreciated. I learned javascript on the fly and I've come pretty far but am in no way an expert.

    [quote]
    On a side note too... you may notice the code towards the beginning gets passed refName (which is the name of the object that is being assigned datatables(). Is there a way to get this object itself using a select statement so I wouldn't have to pass wTable and WO?
    [/quote]
  • matthttammatthttam Posts: 40Questions: 2Answers: 0
    The second half of my question has been answered in another conversation I made.
    http://www.datatables.net/forums/discussion/12591/select-the-datatable-instance-in-a-function-under-fnserverdata#Item_2
This discussion has been closed.