I have a user table using DataTables that will contain more than 200 rows. It looks so great when I use the default DataTables "pageLength": 10 , and this is an example table.
Username | Type | Request | user01 1 request01 user02 1 request02 user03 2 request03 user04 1 request04 user05 1 request05 user06 1 request06 user07 1 request07 user08 1 request08 user09 1 request09 user10 1 request10
Showing 1 to 10 of 200 entries First Previous 1 2 3 ... 20 Next Last
So, to reduce load time, I decide to use "processing": true and "serverSide": true . Then I got some problems with this "serverSide" : true , It prints 200 rows of data in a table.
Showing 0 to 0 of 0 entries (filtered from NaN total entries) . Then the pagination still prints, and after I click on page 2 , it does nothing.
I do not want DataTables to receive 10 data for the first, after clicking page 2 , it will get another 10, etc.
I am using CodeIgniter, here is my code:
On my Views + Js :
<select name="task" id="task"> <option value="1">Task 1</option> <option value="2">Task 2</option> </select> <table id="user-request" class="table"> <thead> <tr> <th>Username</th> <th>Type</th> <th>Request</th> </tr> </thead> </table> <script> ... on task change ... ... var task = $("#task").val(); ... $('#user-request').DataTable({ 'processing': true, 'serverSide': true, 'ajax': { 'type': 'POST', 'url': base_url+'user/get_user_request', 'data': {"task":task,"csrf_token":$("input[name=csrf_token]").val()} } }) </script>
Note : A task is another group, for example, class 1 or class 2, Orchard University or Harvard University.
On my controller :
$task = $this->input->post('task', TRUE); $user_request = $this->model->all_user_request(task); foreach ($user_request as $ur) { $arr = array(); $arr[] = $ur->username; $arr[] = $ur->type; $arr[] = $ur->request; $data[] = $arr; } $output = array( "data" => $data ); if (COUNT($output) > 0) { echo json_encode($output); }
On my Model :
public function all_user_request($task_id) { $query = "SELECT * FROM user_request WHERE task_id = ?"; return $this->db->query($query, $task_id)->result(); }
Note The model actually uses 2 INNER JOIN , I just simplify the selection just for the query here. (turning into a denormalization table only here).
I tried to add draw , recordsTotal , recordsFiltered to $output in my controller, just using numeric data. Example
$output = array( "draw" => 5, "recordsTotal" => 5, "recordsFiltered" => 5, "data" => $data ); if (COUNT($output) > 0) { echo json_encode($output); }
I was looking for an answer, but I think the problem is here, but I still don't know where I should get the data draw - recordsTotal - recordsFiltered . I see a different answer from others, they use "draw" => $_POST['draw'] , then I tried it and it does nothing.
So I'm trying to use numerical data, but the result is still the same. I need help. It still prints 200 rows of data in a table.
Showing 0 to 0 of 0 entries (filtered from NaN total entries) . Then the pagination still prints, and after I click on page 2 , it does nothing.