Data export not working on dynamic table
Data export not working on dynamic table
qute
Posts: 3Questions: 1Answers: 0
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 :-)
This discussion has been closed.
Answers
Your HTML table has no tbody tag.
Thanks. That did not solve the problem though. Still only copies 2 rows. And not the 3 that are in the DOM tree
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 eitherrow.add()
orrows.add()
to let Datatables add the data to the table.Kevin
Thanks kthorngren.
I'm using ajax to populate the table.
So I guess the problem is that I'm mixing bootstrap and datatables?
I've never used bootstrapTable but I suppose you would want to choose one or the other for your tables to avoid conflicts.
Kevin