我已经在SO上找到了几个答案以及一些关于ajax / dataTables的教程和文档。我的dataTable仍然不会填充JSON数据。
HTML:
<table id="table" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>Status</th>
<th>Student Name</th>
<th>Exam Name</th>
<th>School</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tfoot>
<th>Status</th>
<th>Student Name</th>
<th>Exam Name</th>
<th>School</th>
<th colspan="2">Action</th>
</tfoot>
</table>
使用Javascript:
<script type="text/javascript">
$(document).ready(function() {
// Datatables
$('#table').DataTable({
"url": "<?php echo site_url('exams/ajax_list'); ?>",
});
});
</script>
ajax_list Exams控制器中的PHP函数:
public function ajax_list() {
$list = $this->exam_model->get_datatables();
$data = array();
foreach ($list as $exam) {
$row = array();
$row[] = $exam->exam_status;
$row[] = $exam->first_name . " " . $exam->last_name;
$row[] = $exam->exam_name;
$row[] = $exam->exam_school;
$data[] = $row;
}
echo json_encode($data);
}
从导航到方法时可以看到,json_encode输出正确,但dataTable仍为空。
我错过了什么吗?
答案 0 :(得分:1)
好的,我终于开始工作......感谢您给我机会玩这个对我而言全新。这有点像一个bug,但就像其他一切一样,事实证明它很简单。
我没有在CI中设置这个但是那不重要......
在浏览文档后,我想出了这个...
1.更改您的&#34; url&#34;到&#34; ajax&#34;。 我假设你使用的路径是正确的。改变我对你的看法。
<script type="text/javascript">
$(document).ready(function () {
// Datatables
$('#table').DataTable({
"ajax": "./ajax_list.php" // change this to suit.
});
});
</script>
<强> 2。删除colspan =&#34; 2&#34;你的标签。 除非你使用我没有考虑的其他选项,否则它不喜欢它。
第3。最后将你的json_encode改为......
echo json_encode(['data'=>$data]);
希望这能让你起步并运行......文档非常好,所以我建议你给它一个好的过程。