dataTable Serverside processing pagination with draw

2k Views Asked by At

I'm using jquery DataTable 1.10.13 . I'm getting showing data as serverside processing. currently my data table shows correct pagination numbers on bottom but all the data shown 4 all paginate pages. [pagination shows 4 pages. but all 4 pages load same data load.]

I saw on tutorial they are passing a variable called 'draw' from model. How can I get that?

below is my code

 $('#userTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "type": "GET",
            "url": ",
            "dataSrc": "data",
            "contentType": "application/json; charset=utf-8",
            "dataType": "json",
            "processData": true
        },
        "columns": [
            { "data": "id" },
            { "data": "email" },
            { "data": "company" },

        ]
    } );

and this is my controller

 public function getUserList1()
    {
    $data = $this->user->getUserListData();
   echo json_encode($data);
}

below is my model.

  public function getUserListData()
    {

    $sql = 'SELECT * FROM users  ORDER BY regdate DESC';
        $query = prepare($sql);
        try {
            $query->execute($params);
            $data = $query->fetchAll();
            $total = $query->rowCount();
        } catch (PDOException $e) {
            \debug::error('MySQL errno ' . $e->getCode() . ': "' . $e->getMessage() . '" when executing: ' . $query->queryString);
        }
        $response = array(
            "draw" => '',
            "recordsTotal" => $total,
            "recordsFiltered" => $total,
            "data" => $users
        );

        return $response;

How to get value for "draw" => ?

1

There are 1 best solutions below

7
On

Remove the following option unless you decode request parameters from JSON string:

"contentType": "application/json; charset=utf-8",

Also you're not performing server-side processing correctly. You need to use ssp.class.php helper available in jQuery DataTables distribution or switch to client-side processing mode.

To switch to client-side processing mode, remove the following options in your JavaScript code:

"processing": true,
"serverSide": true,

In your PHP code, just return your data as follows:

$response = array(
    "data" => $data
);

Also your data is in $data not $users.