Search and Pagination not working while using server-side

左心房为你撑大大i 提交于 2020-08-15 12:56:48

问题


I am using the data table server-side. I am getting the records using ajax. My issue is, search and pagination not working. I am getting the search and pagination along with all the data.

Please check the below image, I am showing 10 records per page but it is showing all.

I checked on StackOverflow there are server question asked on this topic. I almost checked every question but still, I am not able to find the solution.

I am using below code

if($_REQUEST['action']=='adminList'){

$stmt = $pdo->prepare("SELECT count(*) FROM tbl_admin");
$stmt->execute();
$totalRecords = $stmt->fetchColumn();
$query="SELECT `admin_id`, `a_firstname`, `a_lastname`, `a_email`,  `date_of_created` FROM `tbl_admin` WHERE is_active = 1 order by date_of_created DESC";

try {
      $stmt = $pdo->prepare($query);
      $stmt->execute();
      $result = $stmt->fetchAll();
             
      $data['data'] = [];
      foreach ($result as $row) {

        $arr_result = array(
                    //"id" =>$i++,
                    "name" =>$row['a_firstname'].' '.$row['a_lastname'],
                    "email" => $row['a_email'],
                    "date_of_created" => $row['date_of_created'],
        );


        $data['data'][] = $arr_result;
                }

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

$json_data = array(  
"draw"=> intval( $_REQUEST['draw'] ),
"recordsTotal"    => intval($totalRecords),  
"recordsFiltered" => intval($totalRecords),
"data"            => $data['data']
);

// echo "<pre>";
 //print_r($json_data);
echo json_encode($json_data);
//exit();
}

Js

$(document).ready(function() {
  var dataTable = $('#adminList').DataTable({
    "processing": true,
    "serverSide": true,
    "paging": true,
    "searchable": true,
    "ajax": {
      url: "fetch.php",
      type: "post",
      data: {
        action: "adminList"
      }
    },
    language: {
      sLengthMenu: "Show _MENU_", // remove entries text
      searchPlaceholder: "Search",
      emptyTable: "No record found",
      search: ""
    },
    "pageLength": 10,
    "paging": true,
    "columns": [{
        "data": "name"
      },
      {
        "data": "email"
      },
      {
        "data": "date_of_created"
      }
    ]
  });
});

This is my output

Array
(
    [draw] => 1
    [recordsTotal] => 17
    [recordsFiltered] => 17
    [data] => Array
        (
    // getting my all records
)
)

Can anyone help me out what is the issue with my code?


回答1:


PROBLEM

You have enabled server-side processing mode with serverSide: true which requires you to handle searching, ordering and pagination performed on the server-side by your script. However your script returns all the data, that's why you see all results and not just first page.

SOLUTION

  1. Write proper server-side handling code or use helper libraries.

    For example, DataTables distribution includes ssp.class.php helper calss and sample script to aid in generating response if you're using PHP.

    Otherwise, you can inspect sent parameters and paginate your results based on start and length request parameters.

  2. Switch to client-side processing mode by removing serverSide: true as your initialization option and return all data in your server-side response. Please keep in mind that this solution is not ideal if you have a large dataset (>10,000 records).



来源:https://stackoverflow.com/questions/63360592/search-and-pagination-not-working-while-using-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!