After datatables().clear cannot add new row?

After datatables().clear cannot add new row?

nemosmartnemosmart Posts: 6Questions: 1Answers: 0

hello all, I'm making a website that displays the data dynamically based on parameters without having to change their web pages, the obstacles that I experienced was after the command DataTable (). clear; I can not add a new line, although with the same data as beginning,

here my code:

$r = '[{"Id":"1","OrderNum":"1","Caption":"Profiles","Hint":"not yet","Action":"(none)","Parameter":""}]';
dbTable.clear().draw();
dbTable.row.add(r).draw();

on first load it's work and data is loaded, i don't what when wrong,

Regard's, Anton

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    You're specifying the row with a $, as $r, but calling just r in dbTable.row.add(r).draw()

  • nemosmartnemosmart Posts: 6Questions: 1Answers: 0

    sorry typo, the right code is var r = '[{"Id":"1","OrderNum":"1","Caption":"Profiles","Hint":"not yet","Action":"(none)","Parameter":""}]';

    it's always return with message DataTables warning: table id=dataTables - Requested unknown parameter 'Id' for row 0. For more information about this error, please see http://datatables.net/tn/4

    What make me confuse is everything is doing fine, until table clear() and load new data

    Regard's, Anton

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    Can you show all of the DT related code?

    Just making sure, you have the columns named Id, OrderNum, Caption, etc, inside the columns config right?

    Also, just noticed, the r variable you have, the JSON shouldnt be in quotes.. meaning this will do:

    var r = [{"Id":"1","OrderNum":"1","Caption":"Profiles","Hint":"not yet","Action":"(none)","Parameter":""}];
    
  • nemosmartnemosmart Posts: 6Questions: 1Answers: 0

    dbTable = $('#dataTables').DataTable({ responsive: true, order: [[ 1, "asc" ]], sAjaxSource: "menus-data.php?id=0", sAjaxDataProp: "", aoColumns: [ { "mData": "Id" }, { "mData": "OrderNum" }, { "mData": "Caption" }, { "mData": "Hint" }, { "mData": "Action" }, { "mData": "Parameter" }], columnDefs: [ { "visible": false, "targets": [0] }, { "type": "num", "targets": [0] } ] });

    output from php like this:
    [
    {
    "Id":"1",
    "OrderNum":"1",
    "Caption":"Profiles",
    "Hint":"not yet",
    "Action":"(none)",
    "Parameter":""}
    ]

    Regard's, Anton

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    Can you use proper formatting? Makes it easier to read..

    Also, I created this quick demo for ya: http://live.datatables.net/juhacoze/1/edit?js,output

    If you're using row.add(), then you dont need to specify an array with a single object as var r, rather just a single object.. if you're using rows.add(), then you would need to specify an array with one or more objects.

    So if you can change the JSON thats returned by the PHP to output just:

      var r =   
       {  
          "Id":"1",
          "OrderNum":"1",
          "Caption":"Profiles",
          "Hint":"not yet",
          "Action":"(none)",
          "Parameter":""
       };
    

    Then it should work, if not, change the row.add() to rows.add()

    Also.. just an FYI, you can chain the methods, so this will work as well:

    table.clear().row.add(r).draw();
    
  • nemosmartnemosmart Posts: 6Questions: 1Answers: 0

    $.get('menus-data.php?id='+id, function(r) { dbTable.clear().draw(); dbTable.row.add(r).draw(); });

    after i call this code, error showed..

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Edit: I changed it a little, you dont have to specify the columns.name, just columns.data

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    If the literal output from PHP is an array of a single object.. then use rows.add(), instead of row..

    http://live.datatables.net/siloxave/1/edit?js,output

  • nemosmartnemosmart Posts: 6Questions: 1Answers: 0

    i understand, but sometimes it's result more than 1 row depands parameter that i send

    ex. 'menus-data.php?id='+23,

    $.get('menus-data.php?id='+id, function(r) {
    dbTable.clear().draw().row.add(r).draw();
    });

    what is wrong with my code, it's using same php file from ajax and result same format

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    Answer ✓

    lol.. Ive said it 2 or 3 times....

    One more time.

    use rows.add() instead of row.add() .. then you can pass an array of any number of objects

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Also, instead of

    dbTable.clear().draw().row.add(r).draw();
    

    You just need one draw().. So:

    dbTable.clear().row.add(r).draw();
    

    The less draws, the better

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Also, one more thing.. it might be a good idea to either specify the dataType as json in your jQuery request, or if you cant do that, then you can use parseJSON, but I recommend the former

  • nemosmartnemosmart Posts: 6Questions: 1Answers: 0

    oh, i dont see that, thank you jLinux it's work

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Ok, cool, glad I could help

This discussion has been closed.