Jquery mark new added row in datatable

I have an existing datatable in my html page and I'm trying to add a new line using fnAddData p>

var addId = vandropDatatable.fnAddData(["1", "2"]);

How can I find a newly added row to add some class to it (for example, addClass ("New_item"))

+4
source share
3 answers

Updated to reflect the data in Table 1.10.x. The initial response was aimed at 1.9.x. It is still applicable, but the API 1.10.x method is more powerful:

$("#add").click(function() {
    var row = table.row.add([
        'new',
        'new',
        'new',
        'new',
        'new'
    ]).draw();
    row.nodes().to$().addClass('newRow');
});

1.10.x demo → http://jsfiddle.net/0scq8rkm/

In 1.10.x, you return an API object by holding a string. nodes().to$()allows you to work with the internal string node, since it was a jQuery object.


, <tr> :

tr.newRow {
    background-color: red;
    font-size: 20px;
}

:

<button id="add">add new row</button>

, , rowindex <tr>, fnGetNodes:

$("#add").click(function() {
    var rowIndex = dataTable.fnAddData([
        'new',
        'new',
        'new',
        'new',
        'new'
    ]);
    var row = dataTable.fnGetNodes(rowIndex);
    $(row).removeClass().addClass('newRow');
});

. → http://jsfiddle.net/q4E3Y/

+7

fnRowCallback :

"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
  nRow.className = "your new class";
  return nRow;
}

http://datatables.net/examples/advanced_init/row_callback.html

0

What worked for me was to add a new line, draw, and then add a new line again using node () .

$("#add").click(function() {
    var newRow = table.row.add([
        'new',
        'new',
        'new',
        'new',
        'new'
    ]).draw().node();
    $(newRow).addClass('newRow');
});

This follows the same process as specified in the documentation .

0
source

Source: https://habr.com/ru/post/1531285/


All Articles