我正在开发一个CodeIgniter应用程序,我想在其中将MySQL数据库中的数据显示为数据表。
控制器返回所需数据,但不显示任何内容。相关的代码段是:
<!--table--><div class="widget-body no-padding">
<table id="datatable_tabletools" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th data-class="expand">StaffId</th>
<th data-hide="phone">Narration</th>
<th data-hide="phone">Account Number</th>
<th data-hide="phone">Bank</th>
<th data-hide="phone,tablet">Amount</th>
</tr>
</thead>
</table>
</div>
<!-- end widget content -->
初始化数据表的JavaScript显示在下面
<script type="text/javascript">
// DO NOT REMOVE : GLOBAL FUNCTIONS!
$(document).ready(function() {
/* TABLETOOLS */
$('#datatable_tabletools').dataTable({
"bServerSide":true,
"bProcessing": false,
"sServerMethod": "POST",
// Load data for the table's content from an Ajax source
"ajax":{'url':"<?php echo APP_URL.'/index.php/payments/seed';?>",
'type':'POST',},
"aoColumns": [
{ "data": "reference" },
{ "data": "narration" },
{ "data": "accountnumber" },
{ "data": "amount" },
{ "data": "bank" }
],
"sSwfPath": "<?php echo base_url("js/plugin/datatables/swf/copy_csv_xls_pdf.swf"); ?>"
},
"autoWidth" : true,
"preDrawCallback" : function() {
// Initialize the responsive datatables helper once.
if (!responsiveHelper_datatable_tabletools) {
responsiveHelper_datatable_tabletools = new ResponsiveDatatablesHelper($('#datatable_tabletools'), breakpointDefinition);
}
},
"rowCallback" : function(nRow) {
responsiveHelper_datatable_tabletools.createExpandIcon(nRow);
},
"drawCallback" : function(oSettings) {
responsiveHelper_datatable_tabletools.respond();
}
});
/* END TABLETOOLS */
})
</script>
我的控制器是:
public function seed()
{
$aColumns = array('reference','narration','accountnumber','amount','bank');
// DB table to use
$sTable = 'g';
//
$iDisplayStart = $this->input->get_post('iDisplayStart', true);
$iDisplayLength = $this->input->get_post('iDisplayLength', true);
$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
$iSortingCols = $this->input->get_post('iSortingCols', true);
$sSearch = $this->input->get_post('sSearch', true);
$sEcho = $this->input->get_post('sEcho', true);
// Paging
if(isset($iDisplayStart) && $iDisplayLength != '-1')
{
$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
}
// Ordering
if(isset($iSortCol_0))
{
for($i=0; $i<intval($iSortingCols); $i++)
{
$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
if($bSortable == 'true')
{
$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
}
}
}
if(isset($sSearch) && !empty($sSearch))
{
for($i=0; $i<count($aColumns); $i++)
{
$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
// Individual column filtering
if(isset($bSearchable) && $bSearchable == 'true')
{
$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
}
}
}
// Select Data
$rResult=$this->db->query('select SQL_CALC_FOUND_ROWS reference,narration,accountnumber,amount,bank_name bank
from g join bank_data on bank_data.bank_code =g.destinationcode')
;
$iFilteredTotal =$rResult->num_rows();
// Total data set length
$iTotal = $rResult->num_rows();
// $iTotal = $this->db->count_all($sTable);
// Output
$output = array(
'sEcho' => intval($sEcho),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData'=>array()
);
foreach($rResult->result_array() as $aRow)
{
$row = array();
foreach($aColumns as $col)
{
$row[$col]=$aRow[$col];
}
$output['aaData'][]=$row;
}
echo json_encode($output);
}
调用控制器时返回的json样本是:
{"sEcho":0,"iTotalRecords":22631,"iTotalDisplayRecords":22631,"aaData":[{"reference":"0015432969","narration":"BVFFEEW","accountnumber":"gfhfhf","amount":"100.00","bank":"NANJK"}
答案 0 :(得分:0)
代码终于在经过很长时间的延迟后工作,这使我假设没有返回任何数据。延迟是由于结果集的大小,我使用的自定义查询没有强制执行分页,这是我必须通过提出一个新问题来解决的问题。谢谢大家