2

I'm using jQuery DataTables in a console app.

The following code adds a row to the DataTable:

$('#datatable-table').dataTable().fnAddData([
    '<h1>test</h1>',
    'test',
    'test',
    'test',
    'test',
    'test',
    'test',
    'test',
    'test',
    'test',
    'test'
]);

The problem is, when you programmatically add a row, it resets the user's pagination and filtering. So if a user is on page 3, when I update the table, he will go back to page 1. Which doesn't really work for me considering I'm constantly needing to add/remove/update rows.

Any suggestions?

1

1 Answer 1

4

Version 1.10 of jQuery DataTables has a page() method which allows you to get or set the current page of your table.

For example:

    var table = $('#myTable').DataTable();

    $('#btn').click(function() {
       table.row.add( [
           'new 1',
           'new 2'
        ]);

        var currentPage = table.page();  
        table.page(currentPage).draw(false);        
    });

After adding a new row, the code gets the current page, and then sets the table to that page when it is re-drawn.

jsfiddle demo here. Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

There is no need to set a page, calling draw(false) is enough, see this jsFiddle.
Yes true but setting the page object gives you more options, such as moving to the next / previous page for example. I was just trying to demonstrate this.

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.