fnSetColumnVis and sortable columns.
fnSetColumnVis and sortable columns.
cmiller0189
Posts: 6Questions: 0Answers: 0
Hello,
I selectively hide columns in my DataTable using the API function "fnSetColumnVis" which removes the columns from the DOM. However, when I hide columns, DataTables does not remove the hidden column's reference in aoColumns. Because of this, I am subsequently unable to sort columns based on the sorting functions I defined when I initialized DataTables.
In my implementation, after hiding two columns (2 and 3), clicking the header for column 5 sorts with data from column 7 (an offset of 2). Is there any way I can hide columns 2 and 3 but have sorting use the correct data?
Thanks,
Chris
I selectively hide columns in my DataTable using the API function "fnSetColumnVis" which removes the columns from the DOM. However, when I hide columns, DataTables does not remove the hidden column's reference in aoColumns. Because of this, I am subsequently unable to sort columns based on the sorting functions I defined when I initialized DataTables.
In my implementation, after hiding two columns (2 and 3), clicking the header for column 5 sorts with data from column 7 (an offset of 2). Is there any way I can hide columns 2 and 3 but have sorting use the correct data?
Thanks,
Chris
This discussion has been closed.
Replies
I think what you want is this plug-in function: http://datatables.net/plug-ins/api#fnVisibleToColumnIndex - convert the visible index to the column index (i.e. that used in aoColumns).
Allan
Thanks for the quick reply. I'm not sure how I would go about using this function. I assume that I would need to override the default sorting action that occurs when a user clicks on a element to use this new index?
Thanks for the help,
Chris
Sorry I thought you were doing something more complicated that just using the built in sorting. The built in options should all work with hidden columns - for example: http://datatables.net/extras/colvis/ .
Can you link to your own example so I can see what is happening?
Thanks,
Allan
I've set up an example here: http://jsfiddle.net/cmiller0189/v2Bxc/
Notice that when you load the fiddle and sort the "Value" column, my custom sortDataType "dom-input" works as intended and correctly sorts the table. But when you switch page mode (the radio button), and try to sort the "Currency" column, DataTables throws an error.
Thanks,
Chris
Cheers,
Chris
[code]$.fn.dataTableExt.afnSortData['dom-input'] = function(oSettings, iColumn)[/code]
to
[code]$.fn.dataTableExt.afnSortData['dom-input'] = function(oSettings, iColumn, iColumnVis)[/code]
DataTables targets the correct column.
Additionally, I had some minor issues related to my currency parser that caused the values to not sort correctly.
Allan