douzhuan0309 2016-10-09 05:29
浏览 44
已采纳

无法使用Codeigniter和Ajax来填充dataTable

I have gone through several answers on SO as well as a few tutorials and the documentation on ajax/dataTables. My dataTable will still not populate with JSON data.

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 function in Exams controller:

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);
}

From what I can see when navigating to the method, the json_encode outputs correctly, but the dataTable is still empty.

Am I missing anything?

  • 写回答

1条回答 默认 最新

  • dongpo1599 2016-10-09 12:09
    关注

    Ok I finally got it to work... Thanks for giving me the chance to play with this as its all brand new to me. It was a bit of a bugga, but like everything else it turns out to be simple.

    I didn't set this up in CI but that wont matter...

    After drudging through the documentation I came up with this...

    1.Change your "url" to "ajax". I'll assume your path you use is correct. Alter what I have to yours.

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

    2. Remove the colspan="2" in your th tags. It don't like it unless you use some other option I didn't look into...

    3. And finally change your json_encode to...

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

    And hopefully that gets you up and running... The documentation is pretty good so I suggest giving that a good going over.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMware安装虚拟网络驱动程序不成功,如何解决?(操作系统-linux)
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件