Hidden cols and fnRowCallback crash

Hidden cols and fnRowCallback crash

big-dealbig-deal Posts: 38Questions: 0Answers: 0
edited January 2011 in General
Hey.

I am showing and hideing columns dynamically.
It works great.
I am using fnRowCallback (to decide of a background color for each cell dynamicaly and sum the data at the footer).
It works great as well.

BUT - when I try to use fnRowCallback - when there is a hidden col, the code seem to work in an unwanted way.
It seems like the hidden cols are ignored (instead of it being still at index i, index i is now the next col).

What can I do?

thenks in advanced.

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited February 2011
    [edit: damn it, I always misread the questions... I'll leave my answer in case it helps someone someday, but I can see now that it's not solving the problem of DYNAMICALLY adding and removing columns]

    Original misguided response follows:

    The important thing is to distinguish between the index of retrieved data and the index of rendered data.

    Assuming the variable aData (like in the examples) for the retrieved data, when you have hidden columns, aData[4] will NOT be the same column as 'td:eq(4)'.

    I haven't worked out how to make it all more dynamic (DataTables is so deep... there's probably a way that I'm overlooking) but if you manually work out which td:eq(x) you want to target, you can still get up and running.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    I think actually that GregP's answer is quite close to the mark. The way that the DataTables works is that when you hide a column, it actually removes the TD elements for that column from the DOM - hence there is (as GregP says) the concept of visual columns and data columns. The visual columns is the actual DOM, and the data columns (for lack of a better phrase) is a data array, and the 'hidden' elements are stored in a dynamic array.

    Because of this if you are accessing the DOM elements, then the indexes will be off by one (or more if you hide more columns) _after_ the column which has been hidden.

    So the answer to the question requires another question :-) - it depends what you want to do... do you just want the data - in which case the object parameter passed into fnRowCallback provides this in the second parameter: http://datatables.net/usage/callbacks#fnRowCallback , so do you actually need to TD elements. For the latter you can use the internal function _fnGetTdNodes to get an array of the nodes of the row. Something like var an = this.oApi._fnGetTdNodes( nRow );

    Allan
  • big-dealbig-deal Posts: 38Questions: 0Answers: 0
    I needed all of the td elements, one after the other - of all rows.
    What I did was:
    At the function itself I checked for each column if it is hidden (using datatables api).
    I sumed every hidden cell - and thus I knew which visible td I need to go to in the curent td (his regular position minus the hidden previos cells).
    Beware that I need to sum the hidden cells every time - because I dont need to reduce all of the hidden cells from the position of every cell, but the hidden cells that are before the current cell (If there are 4 hidden cells in positions 2,4,5,6 will get the first cell at position 1, the usualy third cell in position 2 (3 minus 1 hidden cells before him), and the usualy seventh cell will be at position number 3 (7 minus 4 hidden cells before him).

    Thenks.
This discussion has been closed.