使用服务器端时搜索和分页不起作用 [英] Search and Pagination not working while using server-side

查看:171
本文介绍了使用服务器端时搜索和分页不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在服务器端使用数据表.我正在使用ajax获取记录.我的问题是,搜索和分页不起作用.我正在获取搜索和分页以及所有数据.

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.

请检查下图,我每页显示10条记录,但显示全部.

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

我在StackOverflow上检查了有关此主题的服务器问题.我几乎检查了每个问题,但仍然找不到解决方法.

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.

我正在使用以下代码

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"
      }
    ]
  });
});

这是我的输出

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?

推荐答案

问题

您已通过serverSide: true启用服务器端处理模式,该模式要求您处理脚本在服务器端执行的搜索,排序和分页.但是,您的脚本会返回所有数据,这就是为什么您看到所有结果而不仅仅是首页的原因.

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.

  1. 编写正确的服务器端处理代码或使用帮助程序库.

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

例如,DataTables发行版包括 ssp .class.php 帮助者进行评估和示例脚本(如果您使用的是PHP)有助于生成响应.

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

否则,您可以检查已发送参数并根据以下内容对结果进行分页startlength请求参数.

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

通过除去serverSide: true作为初始化选项来切换到客户端处理模式,并返回服务器端响应中的所有数据.请记住,如果您的数据集很大(超过10,000条记录),则此解决方案不是理想的选择.

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).

这篇关于使用服务器端时搜索和分页不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆