Sort multiple columns by default

Sort multiple columns by default

mac173173mac173173 Posts: 16Questions: 3Answers: 0

First: I am not a coder. I have a little knowlege of javascript, but just enough to get myself in trouble.....

I need to have the table sort the first column, then the third column by default. The code I have now is:

$('#example').DataTable( {
                    "ajax": 'JSON table',
                    "order": [[ 0, "desc" ]],
                    "lengthMenu": [ 10, 25, 50, 75, 100 ],
                    "aoColumnDefs": [
                      {"sType": "my-currency", "aTargets": [3,4,5]}
                    ]
                } );

So I am getting the first column sorted correctly on the third line. How do I add the third column sorting in decending order?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 14,210Questions: 25Answers: 3,357

    So I am getting the first column sorted correctly on the third line. How do I add the third column sorting in decending order?

    Like this:
    "order": [[ 0, "desc" ], [ 2, "desc" ]],

    The docs have an example:
    https://datatables.net/reference/option/order

    Kevin

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0

    Thank you Kevin. I did search for examples but did not find that one.

    The sort is working, but there is one odd thing. The column 2 sort is of numbers from single digit to six digits. The sorting is not going from the highest number to lowest number, it is sorting the first digit, then the second digit, etc. Look:

    http://careerconnections.nj.gov/careerconnections/prepare/skills/demand/demand_occupations_list.shtml

    Is there a way to make it sort by the full value?

  • kthorngrenkthorngren Posts: 14,210Questions: 25Answers: 3,357

    Since you have text (N/A) in the column its sorting as text. You might try the Natural sorting plugin. I haven't tried it in this case but I think it should work.

    Kevin

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0

    I cannot add a plugin. Like I said, I am not a coder and don't have access to the program files, just the web page.
    Is there a way I can get it to sort in the order that the JSON table is in? I generate the table and can control the ordering.

  • kthorngrenkthorngren Posts: 14,210Questions: 25Answers: 3,357
    Answer ✓

    You can disable Datatables ordering if you wish. Use "order": [] to order the table by the order it is received. If you also want to disable the user's ability to order the table you can use "ordering": false.

    You could also just copy the JS code into the web page. Here is an example:
    http://live.datatables.net/tifotase/1/edit

    I simply copied the text in the Plugin Code section into the script (above $(document).ready()) and added the type: "natural" to the column. Now it sorts the numbers as numbers.

    Kevin

  • mac173173mac173173 Posts: 16Questions: 3Answers: 0

    YES! The"order": [ ], did the trick.

    As that is the simplest solution, I will use it. The data is annual, so the data does not need to be regenerated.

    Thanks so much for your help.

This discussion has been closed.