如果我将serverSide设置为false并使用ajax发布我的数据,那么这两种方法都可以很好地工作,如下所示。基本上我有几个 dataTable 我想重用server_side.php以便从特定表中获取数据(所以我需要相应地传递值以便服务器端(。我不确定为什么在将数据发布到我的server_side.php时将 serverSide 设置为 true 时显示错误。在设置 serverSide = true 时,我看到的只是客户端在我的数据表中显示处理。仅此而已。但是如果我设置 serverSide = false,则可以完美地生成数据,一切正常。就我而言,我需要使用服务器端,这是真的。
这是我的php页面,我用来显示从服务器端传递的数据。目录
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.21/r-2.2.4/datatables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var example = $('#example').DataTable({
"processing": true,
"serverSide": true,
"responsive": true,
ajax: {
url: "server_side.php",
type: "post",
data: {
view: 'product_upload'
}
},
"order": [],
"columnDefs": [{
targets: [0],
className: "dt-body-center",
"orderable": false,
}, {
targets: [1],
className: "dt-body-center"
}]
});
</script>
这是我的server_side.php。
<?php
session_start();
$whereAll = isset($_SESSION["agent_code"]) ? ($_SESSION["agent_code"]) : '';
$view_type = isset($_POST['view']) ? $_POST['view'] : '';
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simple to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* @license MIT - http://datatables.net/license_mit
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
// DB table to use
$table = $view_type;
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array(
'db' => 'product_name',
'dt' => 0,
'formatter' => function ($d, $row) {
return '<input type="checkbox" id="' . $d . '" class="check_boxes">';
}
),
array(
'db' => 'image_path',
'dt' => 1,
'formatter' => function ($d, $row) {
return "<img src='" . $d . "' style='width:100px' class='text-center' />";
}
),
array(
'db' => 'product_name', 'dt' => 2,
'formatter' => function ($d, $row) {
return 'Category :' . $row['category'] . '<br/>Name :' . $row['product_name'] . '<br/>Description :' . $row['product_description'];
}
),
array('db' => 'quantity', 'dt' => 3),
array('db' => 'weight', 'dt' => 4),
array('db' => 'uom', 'dt' => 5),
array('db' => 'price', 'dt' => 6),
array(
'db' => 'product_name', 'dt' => 7,
'formatter' => function ($d, $row) {
return '<a href="#">Edit</a><br/><a href="#">Update</a><br/><a href="#">Delete</a>';
}
),
array('db' => 'category', 'dt' => 8),
array('db' => 'product_description', 'dt' => 9)
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'ecom',
'host' => 'localhost'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require('ssp.class.php');
echo json_encode(
//SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, "added_by = '" . $whereAll . "' and is_show = '1'")
);
提前谢谢你!
将数据表版本从 1.10.21 降级到 1.10.20 解决了我的问题。
https://cdn.datatables.net/v/bs4/dt-1.10.21/r-2.2.4/datatables.min.js
https://cdn.datatables.net/v/bs4/dt-1.10.20/r-2.2.4/datatables.min.js
不确定是内部错误还是什么。