table.rows().data() does not obtain the rows content after migration to ajax source

table.rows().data() does not obtain the rows content after migration to ajax source

mahussmahuss Posts: 23Questions: 6Answers: 0
edited October 2023 in Free community support

Hello,

I have noticed, that after I migrated my table to ajax-sourced (like below)

           var url = "ajax-link";
            var dt = table.DataTable({
                ajax:{
                    url: apiUrl,
                },
                columns: [
                    {data: 'Id'},
                    {data: 'name'},
                     .....
            });

the table works fine, but I can't obtain the data for further actions in next steps through:

var data = dt.rows().data();

values of rows like - data[0] seem always be "undefined"

So my humble question, is the different way to obtain a table data in ajax-sourced tables?

Thank you in advance for suggestions!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,944
    edited October 2023 Answer ✓

    My guess is you are using var data = dt.rows().data(); directly after the Datatables initialization code. Since Ajax is an asynchronous operation you need to wait until the response is received and Datatables has finished initializing. Try using initComplete. Instead of using the dt variable you will need to use this.api() so it will look something like this:

    var data = this.api().rows().data();
    

    Likely you will want to put the further actions in initComplete.

    However I'm not sure how/when you are using var data = dt.rows().data();. If the above doesn't help then please post a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mahussmahuss Posts: 23Questions: 6Answers: 0
    edited October 2023

    Thank you for the help! It goes exactly the way you advised! My Datatable works perfectly now!

Sign In or Register to comment.