【问题标题】:Display data using json in datatables codeigniter在数据表codeigniter中使用json显示数据
【发布时间】:2016-09-20 13:30:24
【问题描述】:

我想在数据表中使用 json 数据格式显示我的数据,但我没有在表中获取任何数据。谁能帮我通过 JSON 在数据表中显示数据?

这是我的控制器:

public function data_courses(){
    $result=$this->Users_model->get_active_courses();
    echo json_encode($result);
}
public function courses_list(){
    $this->authentication();
    $this->load->view('courses');
}

这是我的看法:

<table class="table table-striped table-hover table-bordered" id="allcourses">
    <thead>
        <tr role="row">
            <th>
                Course Name
            </th>
            <th>
                Indian Price
            </th>
            <th>
                U S Price
            </th>
            <th>
                Course Duration
            </th>
            <th>
                Discount
            </th>
            <th>
                <center>Pick Course</center>
            </th>
        </tr>
    </thead>
</table>

这是我的脚本:

$(document).ready(function() {
    $('#allcourses').dataTable({
        "aLengthMenu": [
                [5, 15, 20, 100, -1],
                [5, 15, 20, 100, "All"]
            ],
 "ajax": "Student/data_courses",
        "aoColumns": [
            {"data": "course_name"},
            {"data": "price_INR"},
            {"data": "price_USD"},
            {"data": "no_of_hours"},
            {"data": "discount"},
            {"data": "status"},
            { "bSortable": false }
        ]
    });
});

这是我的模特

public function get_active_courses(){
     $this->db->select('*');
    $this->db->from('courses');
    $this->db->where('status',1);
    $query=$this->db->get();
    return $query->result();    
    }

我的 JSON 数据

{"data":[{"course_id":"1","course_name":" PHP","price_INR":"25,000","price_USD":"750","no_of_hours":"86","date":"2016-05-19 17:58:47","status":"1","discount":"5.89"},{"course_id":"5","course_name":"Java","price_INR":"15,000","price_USD":"650","no_of_hours":"25","date":"2016-05-14 07:59:24","status":"1","discount":"2.50"},{"course_id":"6","course_name":"Dot net","price_INR":"23,000","price_USD":"250","no_of_hours":"36","date":"2016-05-14 12:16:33","status":"1","discount":"5.63"},{"course_id":"7","course_name":"python","price_INR":"15000","price_USD":"650","no_of_hours":"78","date":"2016-05-14 19:25:34","status":"1","discount":"2.50"},{"course_id":"9","course_name":"HTML & CSS","price_INR":"28,000","price_USD":"980","no_of_hours":"78","date":"2016-05-14 19:31:16","status":"1","discount":"2.36"}]}

【问题讨论】:

  • 您能分享一下生成的 json 吗?

标签: jquery json codeigniter datatables


【解决方案1】:

检查您的 json 是否返回以表格数据为值的“数据”属性,即:

{
    "data" : [
        {...},
        {...}
        ...
    ]
}

为此,只需更改

public function data_courses(){
    $result=$this->Users_model->get_active_courses();
    echo json_encode($result);
}

收件人:

public function data_courses(){
    $result=$this->Users_model->get_active_courses();
    echo json_encode(array("data" => $result));
}

更多信息Here

更新:您还应该检查 html 表(th 元素)中的标题列数是否与 javascript 中的列数相同。目前,您在 javascript 中定义了 7 个列,但在 html 中定义了 6 个标题列。尝试删除列定义中的最后一个 { "bSortable": false }

【讨论】:

  • 谢谢你 sebastianb 实际上我是 JSON 和数据表的新手,如果我将这些列更改为数据,我会得到空表但没有数据显示 colud 你帮助我
  • 尝试检查查询(get_active_courses)是否真的返回了一些东西。
  • 我已经用 last_query 检查了它显示正确的查询@Sebastianb
  • 但是查询返回了什么?尝试在 echo 之前使用 die(var_dump($result)),或者使用浏览器中的开发人员工具检查 ajax 响应。
  • 我正在获取 JSON 格式的数据,但我想在 的表格中显示我的数据,我该怎么做??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2013-12-21
  • 2017-10-05
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多