Cannot display table from json data

Cannot display table from json data

dave_gdave_g Posts: 4Questions: 2Answers: 0

Hi all

I am attempting a really basic display of data, however my page loads and I have no table generated.

My my html code

<div id="storeslist" class="col-md-2">
            <table id="delivery-stores" class="display">

            </table>
        </div>

my javascript

displaystores: function(dateid)
    {
        $.getJSON(dd.handlerUrl + 'getstores&dateparam=' + dateid, function (json) {
            $('#delivery-stores').dataTable({
                aaData: json
            });
        });
      
    },

and finally my debug data

http://debug.datatables.net/abaqen

Can anyone spot what I am missing?

This question has an accepted answers - jump to answer

Answers

  • dave_gdave_g Posts: 4Questions: 2Answers: 0

    Ah, you have to define the table in HTML first :-)

    This is what I ended up with

     var otable = $("#delivery-stores").dataTable(
                {
                    "bPaginate": false,
                    "retrieve": true,
                    "bAutoWidth": true,
                    "bDeferRender": true,
                    "bFilter": true,
                    "columnDefs": [
                        {
                            "targets": [ 0],
                            "visible": false,
                            "searchable": false
                        }]
    
                });
            otable.fnClearTable();
    
    
            $.getJSON(dd.handlerUrl + 'getstores&dateparam=' + dateid, function (json) {
                $.each(json, function (index,data) {
                    otable.dataTable().fnAddData([
                        data.BranchID,
                     data.BranchDescription,
                     data.TotalErrors
                    ]);
                });
            });
    
      <table id="delivery-stores" class="display"  width="100%">
                   <thead>
                       <tr>
                           <th>Hidden</th>
                           <th>Branch</th>
                           <th>Errors</th>
    
                       </tr>
                   </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
    
                        </tr>
                    </tbody>
    
                </table>
    
  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    Glad it worked out for you. I notice that you are using the legacy syntax. If you are new to Datatables it is probably better to use the current syntax.

  • dave_gdave_g Posts: 4Questions: 2Answers: 0

    Thanks, I am working on the new one at the moment, actually finding it quite a bit easier.

This discussion has been closed.