Can you disable sorting on the datatable but also have a default ordering for the first column?

Can you disable sorting on the datatable but also have a default ordering for the first column?

shaneamondosshaneamondos Posts: 10Questions: 5Answers: 0
edited February 2017 in Free community support

Im trying to order my datatable by first name (first column) and i am only able to do if sorting isnt disabled for the table.

This scenario 100% requires that the table must not be sortable by the user, but does this also mean that i cant apply a default order on the first column?

I have tried this but obviously this alone allows sorting by the user

{
   order: [[0, "asc"]]
}

So i then tried this but this doesnt order the first column

{
   order: [[0, "asc"]],
   bSort: false,
}

So i then tried this but again it did not work

{
 bSort: false,
 columnDefs: [ 
      {
          targets: [0, 1],   //first name & last name
          orderable: true 
      },
      {
          targets: [ 0 ],
          orderData: [ 0, 1 ]
       }, 
       {
          targets: [ 1 ],
          orderData: [ 1, 0 ]
        }
  ]
}

I know i can specify ordering for each column but this table is a dynamic table and there is a varying number of columns depending on the user. It could have 3 columns or it could have 15 so this is not applicable to my scenario.

Any help is massively appreciated. Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,852Questions: 1Answers: 10,519 Site admin

    bSort: false,

    That is global. You need to remove that, to allow ordering to happen at all.

    Then use the columns.orderable option to disable user ordering of the columns.

    Allan

  • shaneamondosshaneamondos Posts: 10Questions: 5Answers: 0
    edited February 2017

    Thanks but doesn't this mean that, although the table is now ordered by first name, the user can sort the later columns. Which is what i don't want.

    I want to disable sorting and order by firstname

  • kthorngrenkthorngren Posts: 21,569Questions: 26Answers: 4,996
    Answer ✓

    This config will disable the ability of the user to sort any column:

        columnDefs: [
          { orderable: false, targets: '_all' }
      ]
    

    To allow Datatables to initially sort the data you need to remove bSort: false, from the config.

    Kevin

  • shaneamondosshaneamondos Posts: 10Questions: 5Answers: 0

    Thanks it worked! :smile:

This discussion has been closed.