首页 > 解决方案 > 数据表服务器端处理未在页面上显示输出并且条件不起作用

问题描述

我正在使用带有 ajax 的数据表服务器端处理。这是我指的链接 https://datatables.net/examples/data_sources/server_side.html

我遇到以下问题

1) 我在网络选项卡上得到输出,但它没有显示在我的页面上。

网络选项卡屏幕截图

在此处输入图像描述

在页面上,它只显示处理

在此处输入图像描述

2)我尝试使用 where 条件,但这也不起作用。

我试过下面的代码

<table id="list" class="table table-striped table-bordered">
  <thead>
    <tr class="table-column-heading">
      <th>Name</th>
      <th>Email_Id</th>
      <th>Payment id</th>
      <th>Date Of Added</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
<script>
  $(document).ready(function() {
    var dataTable = $('#list').DataTable({
      "processing": true,
      "serverSide": true,
      "ajax": {
        url: "fetch.php",
        type: "post",
        //data: {'action': 'servicelist'}
      }
    });
  });
</script>

获取.php

$table = 'tbl_participantdetails';
$primaryKey = 'pd_id';
$columns = array(
    array( 'db' => 'pd_name', 'dt' => 0 ),
    array( 'db' => 'pd_email',  'dt' => 1 ),
    array( 'db' => 'paid',   'dt' => 2 ),
    array( 'db' => 'payment_id', 'dt' => 3,),
    array( 'db' => 'date_of_added','dt' => 4,
        'formatter' => function( $d, $row ) {
            return date( 'd-m-Y', strtotime($d));
        }
    )
);
require( 'datatables/ssp.class.php' );
$where = "paid =1";
 
echo json_encode(
    SSP::simple( $_GET, $pdo, $table, $primaryKey, $columns,$where )
);

这是我尝试的第二个选项并且它正在工作

<table id="list" class="table table-striped table-bordered">
  <thead>
    <tr class="table-column-heading">
      <th>Name</th>
      <th>Email_Id</th>
      <th>Paid</th>
      <th>Payment id</th>
      <th>Date Of Added</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
$(document).ready(function() {
  var dataTable = $('#list').DataTable({
    "processing": true,
    "serverSide": true,
    "paging": true,
    "searchable": true,
    "ajax": {
      url: "fetch.php",
      type: "post"
    },
    language: {
      sLengthMenu: "Show _MENU_", // remove entries text
      searchPlaceholder: "Search",
      emptyTable: "No record found",
      search: ""
    },
    "pageLength": 25,
    "paging": true,
    "columns": [{
        "data": "pd_name"
      },
      {
        "data": "pd_email"
      },
      {
        "data": "paid"
      },
      {
        "data": "payment_id"
      },
      {
        "data": "date_of_added"
      }
    ]
  });
});

获取.php

$stmt = $pdo->prepare("SELECT count(*) FROM  tbl_participantdetails WHERE is_active = 1 and paid=1");
$stmt->execute();
$totalRecords = $stmt->fetchColumn();
$query="SELECT `pd_id`, `pd_name`, `pd_pic`, `pd_email`, `paid`, `payment_id`, `date_of_added` FROM `tbl_participantdetails` WHERE is_active= 1 and paid=1";
try {
                  $stmt = $pdo->prepare($query);
                  $stmt->execute();
                  $result = $stmt->fetchAll();
                 
              $data['data'] = [];
              foreach ($result as $row) {
        $arr_result = array(
                    //"id" =>$i++,
                    "pd_name" =>"<img src='".$row['pd_pic']."'>'".$row['pd_name'],
                    "pd_email" => $row['pd_email'],
                    "paid" => $row['paid'],
                    "payment_id" => $row['payment_id'],
                    "date_of_added" => $row['date_of_added']
        );


                 $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 json_encode($json_data);
exit;

标签: phphtmljquerydatatables

解决方案


尝试使用 SSP::complex() 而不是 SSP::simple()

而且它应该是 PHP 中的 SSP::complex($_POST...) ,因为您在 ajax 请求中使用 POST 类型。

ajax 中的 url 属性没有任何问题。


推荐阅读