bart2puck
bart2puck

Reputation: 2522

dataTables properly parsed json response from ajax

I am trying to create a table using datatables 1.10 jquery plugin. i am trying to use:

$(function(){
$('#example').dataTable(
{
                    "ajax":{
                            url:"getTicketList.php",
                            "columns": [
                                    { "data": "id" },
                                    { "data": "company" }
                            ]
                    }
});
});

and getTicketList.php

foreach ($ticketList as $k => $v){
    $tickets['data'][$a]['id'] = $v['ticket_id'];
    $tickets['data'][$a]['company'] = $v['listed_company'];
    $a++;
}
echo json_encode($tickets);

which results in:

{"data":{"1":{"id":"20523","company”:”Acme Inc”},”2”:{“id":"23148","company”:”Walmart”}}}

and the html:

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Company</th>
        </tr>
    </thead>

Yet i get no data in table. Any thoughts why? this is my first attempt at using datatables.....

with ChrisV's suggestion I now get the following error:

DataTables warning: table id=ticketList - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

resolved. I had some extra code floating around on the page.

Upvotes: 0

Views: 754

Answers (2)

jorrin
jorrin

Reputation: 512

@ChrisV is right. So it will go something like this:

<code>
$(function(){
$('#example').dataTable(
{
                    "ajax":{
                            url:"getTicketList.php"
                    },
                      "columns": [
                                    { "data": "id" },
                                    { "data": "company" }
                            ]
});
});
</code>

Upvotes: 0

ChrisV
ChrisV

Reputation: 1309

The columns property should appear directly in the .dataTable( declaration, not nested in the ajax property.

Upvotes: 1

Related Questions