【问题标题】:Can't get dataTable to populate with Codeigniter and Ajax无法使用 Codeigniter 和 Ajax 填充 dataTable
【发布时间】:2016-10-09 05:29:50
【问题描述】:

我已经阅读了一些关于 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 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 仍然为空。

我错过了什么吗?

【问题讨论】:

  • 你是否仔细检查了ajax_list的路径?

标签: php jquery ajax codeigniter datatable


【解决方案1】:

好的,我终于让它工作了...感谢您给我机会玩这个,因为它对我来说是全新的。这有点麻烦,但和其他所有事情一样,结果很简单。

我没有在 CI 中进行设置,但这并不重要...

在苦读文档后,我想出了这个......

1.将“url”更改为“ajax”。 我会假设您使用的路径是正确的。将我拥有的变为你的。

<script type="text/javascript">
    $(document).ready(function () {
        // Datatables
        $('#table').DataTable({
            "ajax":  "./ajax_list.php" // change this to suit.

        });
    });
</script>

2。删除 th 标签中的 colspan="2"。 它不喜欢它,除非您使用我没有考虑过的其他选项...

3.最后将您的 json_encode 更改为...

echo json_encode(['data'=>$data]);

希望这能让你启动并运行......文档非常好,所以我建议好好回顾一下。

【讨论】:

  • 我爱你@TimBrownLaw。做这三件事都奏效了。我想我只是没有足够彻底地阅读文档。表格现在已填充!!!
  • @TimothyFisher 好吧,谢谢你就足够了,但我很高兴现在这一切都发生在你身上。干杯!
  • 在您的标签中丢失 colspan="2":您能详细说明一下吗。 @TimBrownlaw
  • @ankit - 翻译...意思是删除 colspan="2"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多