Ajax response doesn't draw

Ajax response doesn't draw

Alex1990Alex1990 Posts: 3Questions: 1Answers: 0
edited February 2015 in Free community support

Hi, Guys!

I'm new in datatables.

For load my datatable, I use this example:

$('#dataTable').dataTable( {
    "ajax": "todo/ajax"
} );

My response Get:

[{"id":100004,"done":true,"action":"Done Action3 for User","priority":3,"dueDate":1422993515467},{"id":100003,"done":false,"action":"Action2 for User","priority":2,"dueDate":1422993515463},{"id":100002,"done":false,"action":"Action1 for User","priority":1,"dueDate":1422993515446}]

But my datatable doesn't load this response.

what am I doing wrong ?

This question has an accepted answers - jump to answer

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited February 2015

    Try what I have below. I've never tried including data that was true or false, but I think it will show up as a string in the table.

    JS:

    $('#dataTable').DataTable({
        ajax: "todo/ajax",
        dataType: "json",
        columns:[
            {sTitle:"ID",data:"id"},
            {sTitle:"Done",data:"done"},
            {sTitle:"Action",data:"action"},
            {sTitle:"Priority",data:"priority"},
            {sTitle:"Due Date",data:"dueDate"}
        ]
    });
    

    HTML:

    <table id="dataTable" class="display" cellspacing="0" width="100%"></table>
    
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    You need to use ajax.dataSrc and set it to be an empty string if you are just returning an array.

    Allan

  • Alex1990Alex1990 Posts: 3Questions: 1Answers: 0

    Very thanks for your answers, its helps me for my application.
    I wrote this cod and it work now:

    $('#dataTable').dataTable( {
        ajax: "todo/ajax",
        dataType: "json",
        "sAjaxDataProp": "",
        columns:[
            {sTitle:"ID",data:"id"},
            {sTitle:"Done",data:"done"},
            {sTitle:"Action",data:"action"},
            {sTitle:"Priority",data:"priority"},
            {sTitle:"Due Date",data:"dueDate"}
        ]
    } );
    

    But i don't understand, why I should use property:

    "sAjaxDataProp": ""

    And property:

    "dataSrc": "" doesn't work

    This cod don't work:

    $('#dataTable').dataTable( {
        "dataSrc": "",
        ajax: "todo/ajax",
    
        columns:[
            {sTitle:"ID",data:"id"},
            {sTitle:"Done",data:"done"},
            {sTitle:"Action",data:"action"},
            {sTitle:"Priority",data:"priority"},
            {sTitle:"Due Date",data:"dueDate"}
        ]
    
    } );
    

    It has error: Uncaught TypeError: "Cannot read property 'length' of undefined". In datatables.js in string:

    // Got the data - add it to the table
    for ( i=0 ; i<aData.length ; i++ ) {

    aData is undefine

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Answer ✓

    If you have a look at the documentation for ajax.dataSrc you will see it is a property of ajax - not a top level property. So you would use:

    ajax: {
      url: 'todo/ajax',
      dataSrc: ''
    }
    

    The examples towards the bottom of the documentation page show a number of examples like this.

    Allan

  • Alex1990Alex1990 Posts: 3Questions: 1Answers: 0

    Thanks :)

This discussion has been closed.