2

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This did not solve my issue, but pointing to "bServerSide" helped me remember I'm using ajax source, which means you have to implement sorting to your SQL query. :) So tnx, for that!

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.