Child Rows do not export to Excel in applied sort order

Child Rows do not export to Excel in applied sort order

mj0sephmj0seph Posts: 9Questions: 3Answers: 0

Hello! I've adopted Kevin's Child Row Export code from the thread referenced below, but found that child table rows are not exported in the applied sort order.

  • In this test case, the default order is descending by "AR Total".
  • When expanding child table, you can see the current order of the parent table is successfully applied to the child table.
  • When exporting to Excel, however, the child table order is the same as its load order (in production, child data arrives with an ajax call).

I thought this might be handled in the childTable For loop...

              // The child data is an array of rows
              for (c = 0; c < childTable.rows().count(); c++) {
                // Get row data.
                rowData = childTable.row(c, { order: "applied"}).data();
                // order is specified as applied, yet initial order is used instead.

but it doesn't make a difference

Here's the thread discussing the child row export:
https://datatables.net/forums/discussion/comment/160693

Here's the test case:
https://live.datatables.net/toguvute/1/edit

Thanks!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited September 3 Answer ✓

    Clever solution, took me a minute to figure out what you are doing :smile:

    This is a tricky one. You are passing an index to the row() API. The row index is set during initial load of the Datatable data and does not change. Passing index 0, for example, will choose the same row no matter how the table is sorted.

    You will need to use rows().data() with the selector-modifier of {order: "applied"} to get all the rows in order. I added the following to your test case:

                var allRows = childTable
                  .rows({ search: "none", order: "applied" })
                  .data();
                
                console.log('All child rows:', allRows)
    

    You will need to iterate over allRows to get the applied order. Instead of this:

                  for (c = 0; c < childTable.rows().count(); c++) {
                    // Get row data.
                    rowData = childTable.row(c, { order: "applied" }).data(); // order is specifci as applied, yet inital order is used instead.
    ...
    

    Kevin

  • mj0sephmj0seph Posts: 9Questions: 3Answers: 0

    Very helpful, sir! Using the rows().data() API was indeed the answer.

Sign In or Register to comment.