fnSetColumnVis and sortable columns.

fnSetColumnVis and sortable columns.

cmiller0189cmiller0189 Posts: 6Questions: 0Answers: 0
edited December 2011 in Bug reports
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

Replies

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Hi Chris,

    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
  • cmiller0189cmiller0189 Posts: 6Questions: 0Answers: 0
    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
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Hi 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
  • cmiller0189cmiller0189 Posts: 6Questions: 0Answers: 0
    Hi 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
  • cmiller0189cmiller0189 Posts: 6Questions: 0Answers: 0
    Interestingly enough, if I remove my custom sortDataType, the table sorts as expected. This means something is wrong with the custom sorter. It also means this isn't really a true bug report :). Could you advise on how to fix my custom sorter?

    Cheers,
    Chris
  • cmiller0189cmiller0189 Posts: 6Questions: 0Answers: 0
    So I found a solution. It turns out that if I change my code from
    [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.
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Cool - great to hear you got it sorted! Certainly column data indexes and visible indexes can get somewhat confusing!

    Allan
This discussion has been closed.