1

I want to display my data using json data format in datatables, but I am not getting any data in the table. Could anyone help me to display data in datatable through JSON?

This is my controller:

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

This is my view:

<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>

This is my script:

$(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 }
        ]
    });
});

This is my model

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

My JSON data

{"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"}]}
1
  • Could you please share the resulting json? Commented May 23, 2016 at 16:06

1 Answer 1

1

Check that your json returns a "data" property with the tabular data as the value, i.e.:

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

For this, simply change

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

To:

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

More info Here

update: You should also check that you have the same number of header columns in the html table (the th elements) than columns in the javascript. Currently, you have 7 columns defined in javascript but 6 header columns in html. Try removing the last { "bSortable": false } in the columns definitions.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you sebastianb actually I am new to JSONand datatables, if i change that columns to data i am getting empty tables but no data is displaying colud you help me
Try checking if the query (get_active_courses) actually returns something.
i have checked with last_query its displaying correct query @Sebastianb
But does the query return something? try using die(var_dump($result)) before the echo, or check the ajax response with developer tools in the browser.
I am getting data in JSON formate, but i want to display my data in tables in <td>s how can I ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.