Data export not working on dynamic table

Data export not working on dynamic table

qutequte Posts: 3Questions: 1Answers: 0
edited November 2017 in Free community support

I'm trying to use Datatables to export a table.

The problem I run into is that I have the following table:

<table id="tbl" class="display nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th data-field="ID">ID</th>
<th data-field="Name">Name</th>
<th data-formatter="operateFormatter" data-events="operateEvents"></th>
</tr>           
</thead>

<tr data-index="0"><td class="sorting_asc" style="">1</td><td class="sorting" style="">asadasd</td><td class="sorting" style="">-</td></tr>
<tr data-index="1"><td class="sorting_asc" style="">2</td><td class="sorting" style="">qwqweqwe</td><td class="sorting" style="">-</td></tr>
</table>

I use this to add the buttons to export:

$('#tbl').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print'
    ]
} );

This will always export only 2 rows. No matter how many I add to the table with javascript.

var $tableBody = $('#tbl').find("tbody"),
$trLast = $tableBody.find("tr:last"),
$trNew = $trLast.clone();
$trLast.after($trNew);

What am I missing?

Thanks :-)

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Your HTML table has no tbody tag.

  • qutequte Posts: 3Questions: 1Answers: 0

    Thanks. That did not solve the problem though. Still only copies 2 rows. And not the 3 that are in the DOM tree

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    If you add directly to the table, not using Datatables API's then you will need to tell Datatables the HTML data has been updated. One option is to use rows().invalidate(). To avoid this you can use either row.add() or rows.add() to let Datatables add the data to the table.

    Kevin

  • qutequte Posts: 3Questions: 1Answers: 0

    Thanks kthorngren.

    I'm using ajax to populate the table.

                                $('#tbl').bootstrapTable('destroy');
                                $('#tbl').bootstrapTable({
                                    data: objDataAll,
                                    escape: 'false'
                                });
    

    So I guess the problem is that I'm mixing bootstrap and datatables?

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    I've never used bootstrapTable but I suppose you would want to choose one or the other for your tables to avoid conflicts.

    Kevin

This discussion has been closed.