Don't know how to get row.add() and draw() to work properly.

Don't know how to get row.add() and draw() to work properly.

MidaIganesMidaIganes Posts: 11Questions: 5Answers: 0
$('table#js-datatables').DataTable({
                    pageLength: 5,
                    searching: false,
                    ordering: false,
                    lengthChange: false,
                    info: false,
                    "language": {
                        "paginate": {
                            "previous": "«",
                            "next": "»"
                        }
                    },
                });

$('table#js-datatables.js-ajalugu').DataTable().row.add( [thing1,thing2,thing3] ).draw(false);
So while trying to simply add a row, firstly it doesn't add anything and I don't get an error either.
Secondly the draw will reset the initial options of the table, is it required to put things like the pageLength into each Datatable() function with the draw?
What syntax does it require?

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Looks like your selector, $('table#js-datatables.js-ajalugu'), for the row.add() is not correct. Looks like it should be $('table#js-datatables').

    If you still need help please build a test case showing the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • MidaIganesMidaIganes Posts: 11Questions: 5Answers: 0

    What I've managed to do is to get the row to add now after separating the tables out. Assumed using a single id to initialize the tables, but then a unique class to refer to them was okay.
    Now also there is no longer an issue where searching false, and lengthchange false are reset when using the draw. But it does ignore the paging, the paging is still there and .page( 'last' ) moves the selected page number, yet the content of the table is just all of the rows as if there was no paging. The newly added row will be at the bottom of them all.
    Now clicking on any of the pages will go to the normal table as it was before adding a row and among these ones the new row won't show.

    I'll try to look into the test case soon, but until then, providing this info is a bit easier.

  • MidaIganesMidaIganes Posts: 11Questions: 5Answers: 0
    edited January 2021

    Ah no, the new row added is actually a new record directly added to the database, the row.add() isn't inserting anything currently. But the selectors are identical now, so there should be no issue from there.
    In the array form for row.add(), do each of the items in the array correlate to a cell in the row?

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    I'm not sure what all your questions are but I'll try to explain row.add(). You need to supply an array for all of the cells in the row. The added row will be displayed in the Datatable based on how the table is sorted.

    row.add() is a client only process - the data wont be added to your server database. You will need to use jQuery Ajax to send the row data to the server to be inserted into the database.

    I'm not clear regarding your paging comments. Please provide a link to an example so we can help debug.

    Kevin

This discussion has been closed.