Sorting a single column multiple ways

Sorting a single column multiple ways

chuckchuck Posts: 5Questions: 0Answers: 0
edited March 2011 in General
i have a column of data that contains 2 pieces of data such that

[code]
string1 | string2

string1
string2

[/code]

I would really like to be able to sort this column by either string1 or string2

Is this possible?
Any ideas where to start?

Thanks

Replies

  • chuckchuck Posts: 5Questions: 0Answers: 0
    Anyone have any ideas on this?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi chuck,

    What I think would be needed here is a custom sorting function which will be able to pick out the string that you want from your markup (depending on whatever trigger you want to swap between the two). There is some documentation on sorting functions here: http://datatables.net/development/sorting . Already created samples are here: http://datatables.net/plug-ins/sorting .

    There is actually another option for this - might might or might not work for you. You could have two hidden columns with the individual data and then sort on the hidden column based on the switching trigger. DataTables can have one column sorted by the data in another column using the iDataSort property: http://datatables.net/usage/columns#iDataSort . This would need to be changed at run time in the settings object, but I can show you how to do that if this sounds suitable.

    Allan
  • chuckchuck Posts: 5Questions: 0Answers: 0
    Hi Allen,

    thanks for you reply :)

    I think that the second option sounds good I understand how to complete it for the most part with the exception of how to use the column header to switch between sorting modes?

    Thanks for your help !!
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi chuck,

    It is actually reasonably straight forward to change the data sort property of a column - you just need to do:

    [code]
    oSettings.aoColumns[ i ].iDataSort = z;
    [/code]
    where oSettings is the DataTables settings object (from fnSettings()), i is the column index that you want to change the data sort property for, and z is the column index you want to use for the sorting. Once you've changed the property just simply call fnDraw and the table will be reordered.

    Allan
This discussion has been closed.