0

I have this table:

<table id="datatable" class="table table-striped table-bordered">
  <thead>
    <tr>
     <th>ID</th>
     <th>Agent</th>
     <th>TWWID</th>
     <th>Start Date</th>
     <th>Issue</th>
     <th>Comment</th>
     <th>Overview</th>
     <th>Action</th>
     <th>TL</th>
     <th>Qfinity ID</th>
     <th>MSISDN</th>                    
    </tr>
   </thead>

<tbody>
<?php
//get records from database
$sql_list = "SELECT * FROM `coaching`";
$sql_list_result = $mysqli->query($sql_list);

 if($sql_list_result->num_rows > 0){ 
   while($row = $sql_list_result->fetch_assoc()){ ?>
  <tr>
    <td><?php echo $row['ID']; ?></td>
    <td><?php echo $row['Agent']; ?></td>
    <td><?php echo $row['TWWID']; ?></td>
    <td><?php echo $row['Start_Date']; ?></td>
    <td><?php echo $row['Issue']; ?></td>
    <td><?php echo $row['Comment']; ?></td>
    <td><?php echo $row['Overview']; ?></td>
    <td><?php echo $row['Action']; ?></td>
    <td><?php echo $row['TL']; ?></td>
    <td><?php echo $row['Qfinity_ID_1']; ?></td>
    <td><?php echo $row['MSISDN']; ?></td>
    </tr>
     <?php } }else{ ?>
     <tr><td colspan="5">Nema brojeva u bazi.....</td></tr>
     <?php } ?>

</tbody>        
</table>

In this table i have sortable heders and first column is defoult sorted ASC, but i want the 3'rd column to be sorted by default DESC.

I include in my .php this js (//cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js)

then I add in .php this script

<script>
$(document).ready(function() {
    $('#datatable').DataTable( {
        "aaSorting": [[ 3, "desc" ]]
    } );
} );
</script>

My table still have sort by 1'st column [ID]

how can i change default sorting?

5
  • Wouldnt it be easier to include your default/initial sorting in the SQL query instead of the JS part? Commented Jun 21, 2018 at 10:57
  • yes i try that, and it is sort ok when it loads data. After it is done, then table discard SQL sorting and sort it again by ID (first column) Commented Jun 21, 2018 at 10:58
  • Try to clear localStorage Commented Jun 21, 2018 at 11:25
  • Can you please help me how to clear localStorage. Commented Jun 21, 2018 at 11:51
  • I clear localStorage, even try with other browser but no help Commented Jun 21, 2018 at 12:03

1 Answer 1

1

I faced the same problem and I solved it with the following code. Please try if it can help:

<script>
 $(document).ready(function() {
  $('#datatable').DataTable( {
    "order": [[ 3, "desc" ]]
  });
 });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

i try to add this code into my page but no change, like any script i add does not affect to the my datatable.
which col do you want to sort, TWWID or Start Date ?
I would like to sort by Start_Date (DESC)
Thank you, I manage to get this to work. Mistake was that I initialized script before i load datatables.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.