首先抱歉我的解释不好。目前,我有一个表可能会插入数据直到3000,我得到人们建议使用datatables.But我真的很新,我已经尝试了另一个数据表例子,但仍然无效。
我已经为服务器端创建了数据表,它是对象数据源但似乎不能像我预期的那样运行良好。请帮我展示如何通过json对象数据源创建数据表的正确方法.Below是我的代码:
<script type="text/javascript" charset="utf8" src="../cdc/datatables/media/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="../cdc/datatables/media/js/jquery.dataTables.min.js"></script>
<script language="javascript">
$(document).ready(function() {
$('#cdcTracking-list').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "../cdc/load/jsonTrack.php",
"columns": [
{ "elements": "vesselCode" },
{ "elements": "voyage" },
{ "elements": "chasisNo" },
{ "elements": "plateNo" },
{ "elements": "bookingRef" },
{ "elements": "serviceTerm" }
]
} );
} );
</script>
<table id="cdcTracking-list" class="display">
<thead>
<tr>
<th>No. </th>
<th>Vessel </th>
<th>Voyage </th>
<th>Chasis No</th>
<th>Plate</th>
<th>Booking Ref</th>
</tr>
</thead>
</table>
$getSessionList = getVehicleTrkDetail();
if (count($getSessionList) > 0) {
$data = array();
for ($i = 0; $i < count($getSessionList); $i ++) {
$getSessionListRecord = $getSessionList[$i];
$data[$i] = array(
vesselCode => $getSessionListRecord['vesselCode'],
voyage => $getSessionListRecord['voyage'],
chasisNo => $getSessionListRecord['chasisNo'],
plateNo => $getSessionListRecord['plateNo'],
bookingRef => $getSessionListRecord['bookingRef']
);
}
$json = array(
status => "success",
elements => $data
);
} else {
$json = array(
status => "failure"
);
}
echo json_encode($json);
{"status":"success","elements":[
{"vesselCode":"CE",
"voyage":"V01",
"chasisNo":"PL82A53DR61302244 ",
"plateNo":null,
"bookingRef":"V007\/E\/-00006"},
{"vesselCode":"CE",
"voyage":"V01",
"chasisNo":"PL1C21LNR6B101100",
"plateNo":null,
"bookingRef":"V007\/E\/-00006"}
请有人帮助我初学者体验。谢谢
答案 0 :(得分:2)
当我试图将我的第一个装有ajax的表放在一起时,我也遇到了一些麻烦。你可以从数组结构或类结构中提供json,我的方法使用stdClass。
javascript:
要设置json中的值,您必须引用“数据”&#39;领域。如果您打算使用任何样式
<script language="javascript">
$(document).ready(function() {
$('#cdcTracking-list').dataTable( {
"processing": true,
"serverSide": true,
"ajax": "../cdc/load/jsonTrack.php",
"columns": [
{ "data": "number" },
{ "data": "vesselCode" },
{ "data": "voyage" },
{ "data": "chasisNo" },
{ "data": "plateNo" },
{ "data": "bookingRef" }
]
} );
} );
</script>
php:
该表还希望您提供的数据位于包含“数据”数据的数组中。密钥或带有&#39;数据的类?属性。所以无论你在哪里看到#stdClass&#39;如果您愿意,可以用数组代替。
<?php
$getSessionList = getVehicleTrkDetail();
//As I stated before, I used a class for my data.
//You can also use an array but it still needs a 'data' field
$tableData = new stdClass();
$tableData->data = array();
if (count($getSessionList) > 0) {
for ($i = 0; $i < count($getSessionList); $i ++) {
$getSessionListRecord = $getSessionList[$i];
$data = new stdClass(); //Changed to class here
$data->number = $i;
$data->vesselCode = $getSessionListRecord['vesselCode'];
$data->voyage = $getSessionListRecord['voyage'];
$data->chasisNo = $getSessionListRecord['chasisNo'];
$data->plateNo = $getSessionListRecord['plateNo'];
$data->bookingRef = $getSessionListRecord['bookingRef'];
array_push($tableData->data, $data);
//Since you don't declare this in the javascript for the table,
//you shouldn't have it served up.
//Otherwise you will get a DataTables error
//$data->serviceTerm = 'Service Term';
}
$tableData->status = "success";
} else {
//Note: this will also cause an error because you're not serving up any
// fields that the API is expecting. You might want to at least have
// empty fields or default data so it displays something.
$tableData->status = "failure";
}
echo json_encode($tableData);
?>