How to get column information

How to get column information

peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
edited October 2010 in General
If I have an element that I know is in row n, how can I get the column name and/or and pointer to the column element? fnGetData and fnGetNodes only return data rows. I thought perhaps Row 0 would be the header info.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    If you have the row node, you can just do $('td', tr_node) to get all TD elements, and then pick the one you want. However, I suspect that this might not be exactly what you are looking for... Could you explain in a little more detail?

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    No, I mean that have some some arbitrary element, say row r and column c. I want to get the column name for column c. I guess I just expected there would be a DataTables api call for it, similar to fnGetData
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    [code]
    $('#table_id thead th:eq('+c+')').html();
    [/code]
    Just a DOM query will do it - no need for DataTables to provide an API function for it.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Ok, I have no problem using a DOM query, but the problem now is that $('#table_id thead th') returns twice as many columns as there really are because of the FixedHeader. I can't even figure out how it is possible for the FixedHeader to have the same id as the table, since I thought id's were unique. But how can I do a query and only get each column once?
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Allan,
    Any thoughts on this? FixedHeader doubles the number of columns because of the way it's defined.

    Also, using the DOM doesn't work if I have hidden columns using ColVis. How can I obtain a list of all the columns, hidden or not?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    To get all columns you need to loop over fnSettings().aoColumns[].nTh. Could easily be made into a plug-in API function.

    For the ID issue - I'm surprised at what happened there as well, but obviously FixedHeader should be removing the ID. I've just committed a fix and you can grab the nightly version which has this in it.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Thanks. It looks like the nTh items are the Jquery representation of the element, is that right? Will it have all the appropriate classes on it?
    Would fnSettings().nTHead also provide me with similar information?
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Hmm, I think these hidden columns are really messing me up. Does fnGetData return the entire row or only the visible elements? If only the visible elements, how can I get them all? Most of my processing relies on my having access to all the data, ignoring whether or not it is being shown.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    nTh is just a node element. If you want to 'jQuery it' just $(nThVar) would do. Likewise nTHead is just the THEAD node that is used in the table.

    For fnGetData - the array it returns is the data for all columns, regardless of if they are hidden or not.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    edited November 2010
    Actually, I meant to ask about fnGetNodes. Does that return just the visible columns?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Effectively yes, since it returns the TR elements, not the TDs.

    The plug-in API function fnGetTd ( http://datatables.net/plug-ins/api#fnGetTd ) can be used to get a full list of TD elements for a row.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Ahh, ok. Didn't know about that one. So that only returns 1 TD at a time, right? Guess I would have to use that instead of fnGetNodes and loop through the row getting 1 element at a time. Would be nice to incorporate that functionality into fnGetNodes and just have an option to return all nodes instead of only those that are visible.
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    I'm using fnSettings().aoColumns[].nTh, but it looks like for any hidden columns, the node returned does not have my classes applied. It has the 'sorting' class that Datatables adds, but not any of the classes that were specified with aoColumnDef when the table was initialized.
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    What's the best way to know how many times I can safely loop through fnGetTd, i.e., the number of columns. Would it be fnSettings().aoColumns.length?
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Allan,
    I think this is definitely a bug. Even when I unhide a column, the classes that were specified in aoColumnDef are not being applied. They appear to be ok in the elements, but not in the .
    This only happens to columns that are hidden at initialization, with bVisible.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Here is a modification of the function which can be used to get an array of the TD elements: http://datatables.net/plug-ins/api#fnGetTds

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Excellent! Thank you.

    Still having the problem with nodes returned by fnSettings().aoColumns[].nTh not having the classes applied for hidden columns
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Just committed a fix for the TH issue - the code for that was a bit iffy :-). It's available in the "nightly" now.

    Allan
  • peterkronenbergpeterkronenberg Posts: 112Questions: 0Answers: 0
    Thanks!
This discussion has been closed.