Datatables server-side processing not displaying the output on-page and where condition not working

血红的双手。 提交于 2020-07-09 11:46:42

问题


I am using data tables server-side processing with ajax. Here is the link I am referring https://datatables.net/examples/data_sources/server_side.html

I am getting issues which are as follows

1) I am getting the output on the network tab but it's not displaying on my page.

Network tab screenshot

On the page, It's displaying only Processing

2) I try to use where condition but that also not working.

I tried below code

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

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

This is the second option I tried and it's working

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

fetch.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;

来源:https://stackoverflow.com/questions/62708059/datatables-server-side-processing-not-displaying-the-output-on-page-and-where-co

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