How to add a row at the end of the table?

How to add a row at the end of the table?

FRS4002FRS4002 Posts: 85Questions: 11Answers: 0

I need to add a row without columns at the end of the table (after initialization), since adding only a row directly in HTML is not possible in datatables, it gives error. Here is the test case of the table: http://live.datatables.net/moxepano/1/edit
How can I achieve this, and also if there is a solution on how to freely add any custom rows and columns directly in HTML (remove datatables detection of adding only a row without column)?

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    The RowGroup extension is an example of rows added to the DOM to display the group information. These rows aren't apart of the Datatable row data meaning they are counted as a row in the info message nor are they used for sorting or searching. The extension refreshes the rows after each table draw.

    You could do something similar. You would use standard jQuery or Javascript methods to insert the tr at the desired position. You will probably want to use the draw event to keep up with the rows you are inserting to make sure they are displayed where you want when for example, sorting changes. There is nothing built into Datatables for this and Datatables knows nothing about the rows you insert.

    Kevin

  • FRS4002FRS4002 Posts: 85Questions: 11Answers: 0
    edited August 2022

    @kthorngren Thanks Kevin, but could you please help me apply this to my test case above? Also, You said "You would use standard jQuery or Javascript methods to insert the tr at the desired position", you mean without RowGroup extension? Or RowGroup extension is a must! If you can help me to apply the row at the end of the table without RowGroup extension would be great!

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Here is one of many possible ways:
    http://live.datatables.net/holejodi/1/edit

    You wouldn't use rowGroup for this.

    Kevin

  • FRS4002FRS4002 Posts: 85Questions: 11Answers: 0

    @kthorngren Yeah, but you added colspan with tds... I don't want to add any columns in the last row... I just need to write immediately in the row... I tried deleting colspan, and it didn't work...

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited August 2022

    I just used colspan as an example. You just need to provide proper HTMl. This is all outside fo Datatables. Stack Overflow is a good resource for general jQuery, Javascript and HTML questions.

    However if you want to update the test case showing what you did we'll take a look.

    Kevin

  • FRS4002FRS4002 Posts: 85Questions: 11Answers: 0

    @kthorngren Thanks Kevin! I fixed it by adding this:

                    $('#mytable tbody').after(`<tbody><tr><td colspan="3" class="text-center">Table Footer</td></tr></tbody>`);
    

    Thanks!

Sign In or Register to comment.