Sebastian
Sebastian

Reputation: 865

JQuery DataTables Default Sorting not working

My datatable default sorting is not working for some reason that I haven't been able to figure out. The data is displayed correctly though. Pagination is not working either. The first column contains images, but I'm setting it for default to not execute any sort.

Any help is really appreciated.

Here's part of the code I'm using:

var oTable = $('#datatables').dataTable( {
        "aaSorting": [[2, 'asc']],
        "bProcessing": true,
        "bServerSide": true,
        "sPaginationType": "full_numbers",
        "sAjaxSource": "process.php",
        "aoColumns": [
            {
                "mData": null,
                "aTargets": [0],
                "bSerchable": false,
                "bSortable": false,
                "sDefaultContent": '<div class="expand /">',
                "sWidth": "30px"
            },
            { 
                "mDataProp": "email",
                "aTargets": [1],
                "bSearchable": true,
                "bSortable": true
            },
            { 
                "mDataProp": "lastName",
                "aTargets": [2],
                "bSearchable": true,
                "bSortable": true
            },
            { 
                "mDataProp": "firstName",
                "aTargets": [3],
                "bSearchable": true,
                "bSortable": true
            },
            { 
                "mDataProp": "dateRegistered",
                "aTargets": [4],
                "sClass": "center",
                "bSearchable": true,
                "bSortable": true
            }
        ]
    } );

I just added the "aTargets", works the same with our without them.

Here's the file that returns the json array:

<?php

        try {
            $conn = require_once 'dbConnect.php';

            $sql = "SELECT email, lastName, firstName, dateRegistered, state FROM Users";

            $result = $conn->prepare($sql) or die ($sql);

            if(!$result->execute()) return false;

            if($result->rowCount() > 0) {
                $json = array();
                while($row = $result->fetch()){
                    $json[] = array(
                        'email' => $row['email'],
                        'lastName' => $row['lastName'],
                        'firstName' => $row['firstName'],
                        'dateRegistered' => $row['dateRegistered'],
                        'state' => $row['state']
                    );
                }

                $response = array(
                    "iTotalRecords" => strval(count($json)),
                    "iTotalDisplayRecords" => strval(count($json)),
                    "aaData" => $json
                );

                echo json_encode($response);

            }
        } catch(PDOException $e) {
            echo 'Error: ' . $e->getMessage();
        }

?>

Thanks.

Upvotes: 2

Views: 6132

Answers (1)

Sebastian
Sebastian

Reputation: 865

After wandering around for a while I was finally able to solve it.

  • Delete or comment the line "bServerSide": true,
  • Instead of using "aoColumns" use "aoColumnDefs"

And it should work just fine.

Upvotes: 5

Related Questions