How to add multiple rows in datatables jquery
Asked Answered
M

4

14

I have used https://datatables.net/reference/api/rows.add%28%29 link working but the data showing the table as [object,object]. How to show the object to string. i have used JSON.stringify(obj) its also not working.

HTML

<table id="exampleTable">
 <thead>
  <tr>
   <th>Year</th>
   <th>Month</th>
   <th>Savings</th>
  </tr>
 </thead>
 <tbody>
   <tr>
    <td>2012</td>
    <td>January</td>
    <td>$100</td>
   </tr>
   <tr>
    <td>2012</td>
    <td>February</td>
    <td>$80</td>
   </tr>
 </table>

JS

$('#addRows').click(); 

var table3 = $('#exampleTable').DataTable(); 

$('#addRows').on( 'click', function () { 
    table3.row.add(
       [ { "Year": "Tiger Nixon", "Month": "System Architect", "Savings": "$3,120" },
         {"Year": "Tiger Nixon", "Month": "System Architect", "Savings": "$3,120" }]
    ).draw(); 
});
Malherbe answered 24/7, 2014 at 7:14 Comment(3)
Please show your codeHairstreak
put ur code n html in the question by clicking edit, not in commentSharilyn
i have added above question pls refresh and checkMalherbe
P
19

I created two samples in this FIDDLE.

If you want to use objects in rows add you should add columns in your datatable init:

JS

var table3 = $('#exampleTable').DataTable({
    data:[{ "Year": "2012", "Month": "January", "Savings": "$100" },
      { "Year": "2012", "Month": "February", "Savings": "$80" }],
    columns:[{data: 'Year'},
        {data: "Month"},
        {data: "Savings"}]
}); 

but if you don't want to do this you can use next syntax in rows add:

JS

table4.rows.add(
   [[ "Tiger Nixon", "System Architect","$3,120" ],
     ["Tiger Nixon", "System Architect", "$3,120" ]]
).draw(); 

Look fiddle it's more informative.

Poliard answered 24/7, 2014 at 8:44 Comment(0)
D
5

I came across this problem too - I found the documentation to be less than clear. Their example on https://datatables.net/reference/api/rows.add() does not work when I pass my own dynamically created array of objects.

In order to get it working, you have to specify the columns' names when instantiating DataTables.

In any case, the below is a working solution.

var DataTable = $('#tableName').DataTable({
  iDisplayLength: 15,   // number of rows to display
  columns: [
    { data: 'id' },
    { data: 'name' },
    { data: 'car' },
  ]
});

// assume this is a dynamically created array of objects
var persons = [
  {
    id: 1,
    name: 'John',
    car: 'Mercedes',
  }, 
  {
    id: 2,
    name: 'Dave',
    car: 'BMW',
  }, 
  {
    id: 3,
    name: 'Ken',
    car: 'Jeep',
  },  
];

DataTable.rows.add(persons).draw();
Diapause answered 10/12, 2015 at 22:5 Comment(1)
The need to specify the columns was a big gotcha for me as I was initializing the table in one function and updating the data in another function. Thank you for this answerHistoriography
S
1

Try this`

var data = [
  {id : 1, name : 'John'},
  {id : 2, name : 'Joe'},
  {id : 3, name : 'Mathan'},
  {id : 4, name : 'Mani'},
  {id : 4, name : 'Karthik'}
];

//Intialize data table

var DataTable = $('#tableName').DataTable({
 iDisplayLength: 15,  
 columns: [
 { data: 'id' },
 { data: 'name' },
 ]
});

DataTable.rows.add(data).draw();
Semipro answered 2/11, 2016 at 13:46 Comment(0)
C
0

I am returning a view as in a foreach loop, it returning multiple rows.now i ll append in datatable with all functionilities working of datatable.

it work only when you are getting data in html format and only format of multiple rows.

this is my datatable

   $('#SurveyDetails').dataTable({
    "scrollY": "500px",
    "scrollX": true,
    "scrollCollapse": true,
    "paging": false,
    "jQueryUI": false,
    "searching": false,
    "autoWidth": false,      
 });

i am appending 30 rows on scroll of datatable.

on sucess of ajax :

   success: function (data) {
            if (!$.trim(data) == '') {

                data = data.replace(/^\s*|\s*$/g, '');
                data = data.replace(/\\r\\n/gm, '');
                var expr = "</tr>\\s*<tr";
                var regEx = new RegExp(expr, "gm");
                var newRows = data.replace(regEx, "</tr><tr");
                $("#SurveyDetails").DataTable().rows.add($(newRows)).draw();
             }
Citreous answered 18/2, 2019 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.