2

I want to add JSON data to a existing datatable.

The JSON data looks like this:

[
    ["Trident","Internet Explorer 4.0","Win 95+","4","X","X"],
    ["Trident","Internet Explorer 4.0","Win 95+","4","X","X"],
    ["Trident","Internet Explorer 4.0","Win 95+","4","X","X"],
    ["Trident","Internet Explorer 4.0","Win 95+","4","X","X"]
]

I tried:

$('#' + tab + '_table').dataTable().fnAddData(data);

data is holding the JSON data.

Anything seems wrong there since it adds only one row with this:

enter image description here

1 Answer 1

6

It looks like you're asking dataTables to add raw JSON data to your table. You should parse it first with JSON.parse() and add the resulting Javascript array.

var jsdata = JSON.parse(data);
$('#' + tab + '_table').dataTable().fnAddData(jsdata);
Sign up to request clarification or add additional context in comments.

2 Comments

Okay one more question. How can i remove the existing rows? So that it does not just adds them but removes the old ones before? Thanks
Use fnClearTable() to empty the table completely, or fnDeleteRow() to be more selective. Details are in the API docs for DataTables

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.