Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort datatables table by date column

Tags:

datatables

im trying to sort my data table by the 3rd column(Date column) however it only seems to sort it by the 1st number in the date column as shown in picture below how can I get it so that it sorts by date rather than numeric?

 echo "<table>
<thead>
<th>Zone</th>
<th>Popularity</th>
<th>Date</th>
</thead>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['count(location)'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";}
echo "</table>";
mysqli_close($con);
?>
<script>
$('table').DataTable( {
dom: 'Bfrtip',
"order": [[ 2, "desc" ]],
buttons: [
    'copy', 
    {
        extend: 'csv',
        text: 'CSV',
        extension: '.csv',
        exportOptions: {
            modifier: {
                page: 'current'
            }
        },
        title: 'table'
    }, 
    'pdf',
'print', ],

} );
</script>

table

like image 366
Avalon Parkes-barton Avatar asked Aug 08 '26 15:08

Avalon Parkes-barton


1 Answers

I found a possible solution to your problem here

The recommended solutions are either follow this guide to specify a date-format for your date columns (since DataTables might not be able to detect it out of the box). You probably need to use moment.js to fix this.

like image 187
George K Avatar answered Aug 11 '26 10:08

George K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!