How can I pass a html table row into DataTable.net fnAddData
Asked Answered
A

8

21

I am using the DataTable.net plugin and I am wondering how can I add dynamically a row to a existing table?

http://datatables.net/examples/api/add_row.html

I am looking at this example and they have it like this

/* Global variable for the DataTables object */
var oTable;

/* Global var for counter */
var giCount = 2;

$(document).ready(function() {
    oTable = $('#example').dataTable();
} );

function fnClickAddRow() {
    oTable.fnAddData( [
        giCount+".1",
        giCount+".2",
        giCount+".3",
        giCount+".4" ] );

    giCount++;
}

but I am wondering what happens if I want I have a table row already rendered?

Say this is my table.

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table> 

Now I have this

var newRow = '<tr><td>row 3, cell 1</td><td>row 3, cell 2</td></tr>';

How can I add it through addRow?

I tried oTable.fnAddData(newRow); but that seems not to work.

So I am not sure how to do this.

Arundel answered 16/5, 2010 at 19:4 Comment(0)
U
6

Call the fnAddData with an array of the values you want to add, like this:

oTable.fnAddData(["row 3, cell 1", "row 3, cell 2"]);

From version 1.10 use row.add() method described by @froilanq

You can read more details from the API here, here's the arguments it takes:

  1. array strings : The data to be added to the table. This array must be of the same length as the number of columns on the original HTML table.
    or
    array array strings : 2D array of data to be added to the table. The inner array must be of the same length as the number of columns on the original HTML table.
  2. boolean : optional - redraw the table or not after adding the new data (default true)
Uzzi answered 16/5, 2010 at 19:8 Comment(6)
So I can't just add a rendered html string into it?Arundel
@Arundel - Not if you want it on the datatable, it's based on an object structure, you can .append() to the table, but that won't do what you're after I don't think.Uzzi
Hmm. That sucks. Another problem I see. Is each of my table rows has a inline style. How would I stick that on it?Arundel
@Arundel - Inline styling is usually lost when you sort, etc anyway, can you give an example of the inline styling your after? (Any reason this can't be a class? There are many class options available).Uzzi
Hi. When I render the table for the first all the inline styling does not seem to get lost. the reason why I don't use a class is each row has a unquie background color and font color set by the user. I am sure with some programming I could some how make it into classes but it would take time I don't have.Arundel
Another problem is. Each of my td's have there own class attached to it. How do I add those as well?Arundel
V
62

Solved simple:

var newRow = "<tr><td>row 3, cell 1</td><td>row 3, cell 2</td></tr>";
var table = $('table').DataTable();
table.row.add($(newRow )).draw();
Vorous answered 1/8, 2014 at 13:39 Comment(4)
I agree, this should be the correct answer. By using DataTable api directly - you can add an extra row, but you cannot add extra arguments like data-... to the rows tr or td elements.Speedwriting
I get an error trying this on the latest version. "Cannot set property '_DT_CellIndex' of undefined." Happens when i try to sent it string of a <tr> as well.Roomette
Why do you use table.row.add($(newRow )).draw(); instead of table.row.add(newRow).draw(); ?Cothran
I also received the error "Cannot set property '_DT_CellIndex' of undefined.". Folllowing the answer by "Decoy" here datatables.net/forums/discussion/32575/… after passing the same number of columns the table contained I no longer received the error.Radbourne
H
9

If you still have this problem, have a look to the DataTables plug-in fnAddTr.

I think it can solve your issues.

Helmand answered 31/12, 2010 at 10:41 Comment(0)
U
6

Call the fnAddData with an array of the values you want to add, like this:

oTable.fnAddData(["row 3, cell 1", "row 3, cell 2"]);

From version 1.10 use row.add() method described by @froilanq

You can read more details from the API here, here's the arguments it takes:

  1. array strings : The data to be added to the table. This array must be of the same length as the number of columns on the original HTML table.
    or
    array array strings : 2D array of data to be added to the table. The inner array must be of the same length as the number of columns on the original HTML table.
  2. boolean : optional - redraw the table or not after adding the new data (default true)
Uzzi answered 16/5, 2010 at 19:8 Comment(6)
So I can't just add a rendered html string into it?Arundel
@Arundel - Not if you want it on the datatable, it's based on an object structure, you can .append() to the table, but that won't do what you're after I don't think.Uzzi
Hmm. That sucks. Another problem I see. Is each of my table rows has a inline style. How would I stick that on it?Arundel
@Arundel - Inline styling is usually lost when you sort, etc anyway, can you give an example of the inline styling your after? (Any reason this can't be a class? There are many class options available).Uzzi
Hi. When I render the table for the first all the inline styling does not seem to get lost. the reason why I don't use a class is each row has a unquie background color and font color set by the user. I am sure with some programming I could some how make it into classes but it would take time I don't have.Arundel
Another problem is. Each of my td's have there own class attached to it. How do I add those as well?Arundel
B
5

jQuery DataTables 1.10 allows you to do this natively without the need for a plugin. Grab the most recent development branch here: https://github.com/DataTables/DataTables/tree/1_10_wip/media/js

Here is some sample code on how to use it:

$(document).ready(function() {
  var t1 = $('#t1').DataTable();
  var t2 = $('#t2').DataTable();

  $('#t1 tbody').on( 'click', 'tr', function () {
    t1.row( this ).remove().draw();
    t2.row.add( this ).draw();
  } );

  $('#t2 tbody').on( 'click', 'tr', function () {
    t2.row( this ).remove().draw();
    t1.row.add( this ).draw();
  } );
} );

Reference: http://www.datatables.net/forums/discussion/comment/52595#Comment_52595

More Info

Note the call above is to DataTable() and not dataTable(). If your object is dataTable() access it as follows:

t1 = $('#t1').dataTable();
t1.DataTable().row.add(this).draw();
Beat answered 8/10, 2013 at 18:49 Comment(0)
A
5
var dataTable = $('.table').DataTable();

// get the html table rows then
dataTable.destroy();

$("tbody").empty().promise().done(function(){
    $("tbody").html(data);
    dataTable = $(".table").DataTable();
});

This works if you want to apply the whole html row if you have like inline css or extra html tags inside the table divisions.

Axseed answered 15/2, 2016 at 7:33 Comment(0)
K
1

hi my friends: this code worked well for me.

var oTable = $('#datatable').dataTable();
var api = oTable.api(true);
var newRow = {
                colName1: '',
                colName2: 0,
                colName3: 0,
                colName4: 0
             };
api.row.add(newRow).draw();

good luck.

Korrie answered 7/6, 2015 at 10:45 Comment(0)
M
1

let tr = html.createElement("tr");
tr.innerHTML = '<td>row 3, cell 1</td><td>row 3, cell 2</td>';
oTable = $('#example').dataTable();
oTable.row.add(tr).draw();
Macur answered 17/11, 2022 at 14:46 Comment(0)
P
0

To add multiple rows from JSON file using MVC.

    function fnClickAddRow(obj) {
        $('#tableID').dataTable().fnAddData([
            obj.column1,
            obj.column2,
            obj.column3,
            obj.column4,
            obj.column5,
            obj.column6,
            obj.column7,
            obj.column8,
            obj.column9,
            obj.column10]);
    }
    function rowAdd() {
        $.ajax({
          type: 'POST',
            url: '@Url.Action("Action", "Controller")',
        success: function (data) {
            $.each(data, function (i, obj) {
                fnClickAddRow(obj);
            });
            }
        });

    }
Predesignate answered 9/7, 2018 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.