我正在使用 Codeigniter 和 JQuery 数据表来通过 AJAX/JSON 请求实时更新来自 MYSQL 数据库的数据。我不是这段代码的原始开发者,这就是我猜我无法解决这个问题的原因。
我所拥有的是一个包含 26 列的表,并非所有列都已填充,但似乎所有列都直接来自数据库。这是 json_table 函数的一段代码的样子:
$this->load->model('aircraft_model');
$this->load->model('nats_model');
$aColumns = array('FlightSts', 'TblOrder', 'Callsign', 'Destination', 'NAT', 'SELCAL', 'CldFL', 'CldMach', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'FlightID');
$sTable = 'naecs_aircraft';
$stable 似乎是正确的 mysql 表,$acolumns 似乎是 mysql 表 naecs_aircraft 中的所有字段。这一切都是正确的,它甚至可以工作,但在倒数第二列中,就在 FlightID 之前,我想填充一个按钮。所以基本上在倒数第二列我想要一个按钮。
怎么做?
问候, Maciej。
@edit1
这是控制器 ajax.php 中的全部功能:
public function json_table(){
$this->access->requires(ACCESS_USER);
$this->load->model('aircraft_model');
$this->load->model('nats_model');
$aColumns = array('FlightSts', 'TblOrder', 'Callsign', 'Destination', 'NAT', 'SELCAL', 'CldFL', 'CldMach', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'FlightID');
$sTable = 'naecs_aircraft';
$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);
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')
{
if ($aColumns[$iSortCol] != ' '){
$this->aircraft_model->json_table_sort($aColumns, $iSortCol, $sSortDir);
}
}
}
}
if(isset($sSearch) && !empty($sSearch))
{
for($i=0; $i<count($aColumns); $i++)
{
if ($aColumns[$i] != ' '){
$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
if(isset($bSearchable) && $bSearchable == 'true')
{
$this->aircraft_model->json_table_filter($aColumns, $i, $sSearch);
}
}
}
}
$rResult = $this->aircraft_model->json_table_select($aColumns, $sTable);
$iFilteredTotal = $this->aircraft_model->json_table_found($aColumns);
$iTotal = $this->aircraft_model->count_all($sTable);
$output = array(
'sEcho' => intval($sEcho),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => array()
);
$headers = array('3active' => FALSE, '2cleared' => FALSE, '4closed' => FALSE, '1entered' => FALSE, '5disconnected' => FALSE);
foreach($rResult->result_array() as $aRow)
{
$row = array();
foreach($aColumns as $col)
{
@$row[] = $aRow[$col];
}
$databit = $row;
$databit['DT_RowId'] = $aRow['FlightID'];
$databit['DT_RowName'] = $aRow['Callsign'];
$output['aaData'][] = $databit;
$headers[$aRow['FlightSts']] = TRUE;
}
foreach ($headers as $key => $value){
if ($value == FALSE){
$output['aaData'][] = array($key, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
}
}
$output['aaData'][] = array('No More Aircraft', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
echo json_encode($output);
}
tableinit.js:
function initTable(){
oTable = $('#main-table').dataTable({
"bProcessing": true,
"bServerSide": true,
"bSort": true,
"sServerMethod": "GET",
"sAjaxSource": "ajax/json_table",
"oLanguage": { "sProcessing": "<i class='icon-refresh icon-spin'></i>" },
"sDom": "<'pull-right'r><'row'<'span8'l>>tS",
"sScrollY": "400px",
"bPaginate": false,
"bScrollCollapse": true,
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ 1, 25 ] },
],
"aaSorting": [[ 1, "asc" ]],
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
$('td:eq(0)', nRow).attr("name", "callsign");
$('td:eq(1)', nRow).attr("name", "destination");
$('td:eq(2)', nRow).attr("name", "nat");
$('td:eq(3)', nRow).attr("name", "selcal");
$('td:eq(4)', nRow).attr("name", "lvl");
$('td:eq(5)', nRow).attr("name", "mach");
},
}).rowGrouping({ iGroupingColumnIndex: 0, sGroupingClass: 'nodrop', bSetGroupingClassOnTR: true });
}