1

Ive got a problem to solve. With .append() I do add new rows which have input fields. But the problem is, that after appending new rows no methods work on them. No methods like search, responsive button etc.

I tried a lot but I couldnt figure it out...After clicking on a button a new Row gets added to the #table.

var poAddRowTable = $('#table').DataTable();
$('button').on('click', function () {
    addRowFunc(true)
});

addRowFunc = function () {
    var previousRow = $('#table tr:last');
    var newRow = $(previousRow).clone(true, true);

    $('#table tbody').append(newRow);
    $("html, body").scrollTop($(document).height());
}
3
  • from your problem it looks like you are binding the event listener to elements generated dynamically therefore it is not working. if you can please share the html structure I might be able to help Commented Mar 31, 2017 at 9:43
  • 1
    You should use poAddRowTable.row.add(newRow).draw() Commented Mar 31, 2017 at 9:55
  • @davidkonrad you sir are da best ;-) awesome thanks it worked! Commented Mar 31, 2017 at 10:22

2 Answers 2

2

You should consider to use DataTable.row.add() for adding a new row to the data table.
Your addRowFunc should be updated like this

addRowFunc = function () {

    // Clone data from last row
    var lastRow = $('#table tr:last');
    var newRowdata = [];
    $('#table tr:last').find('td').each(function() {
        newRowdata.push($(this).text());
    }); 

    // Add new row to table
    poAddRowTable.row.add(newRowdata).draw();

    $("html, body").scrollTop($(document).height());
}
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you have to refresh your table after appending, the following link can help :

How to refresh a simple Datatables table when adding new rows with jQuery

Comments

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.