DATA TABLE EXPORTS ONLY THE FIRST ROW OF THE TABLE.

DATA TABLE EXPORTS ONLY THE FIRST ROW OF THE TABLE.

nalexg1nalexg1 Posts: 2Questions: 1Answers: 0
edited February 2022 in Buttons

When I click the submit button, a Golang function gets data from the database and populate the HTML table.
On clicking any of the export buttons, either excel, csv or pdf. Only the first row gets exported.
I have tried everything I can. This is my first time using data-tables. I can't seem to figure out what I amm doing wrong.

Here is the code for initializing the table.

$(document).ready(function () {
  $('#report').DataTable({
    dom: 'Bfrtip',
    buttons: ['copy', 'csv', 'excel', 'print'],
  }),
    $('.buttons-copy, .buttons-print, .buttons-excel, .buttons-csv').addClass(
      'btn btn-primary mr-1'
    );
});

Here is the html code table.


<table id="report" class="table table-hover text-nowrap"> <thead> <tr> <th scope="col">Machine Name</th> <th scope="col">Transaction Time</th> <th scope="col">Card Number</th> <th scope="col">Reciept Number</th> <th scope="col">Total Amount</th> <th scope="col">Amount Paid</th> <th scope="col">Reward Points</th> <th scope="col">Redeemed Points</th> <th scope="col">Cashier Name</th> </tr> </thead> {{range .Reports}} <tbody> <tr> <td>{{ .MachineName}}</td> <td>{{humanDate .TransactionTime}}</td> <td>{{.CardNumber}}</td> <td>{{.RecieptNumber}}</td> <td>{{.TotalTransAmount}}</td> <td>{{.AmountPaid}}</td> <td>{{.RewardPoints}}</td> <td>{{.RedeemdPoints}}</td> <td>{{.CashierName}}</td> </tr> </tbody> {{end}} </table>

range is a golang template function that loops over the array of data that is stored in the variable Reports.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited February 2022 Answer ✓

    {{range .Reports}}
    <tbody>

    Looks like each row is in their own tbody. The Datatables HTML requirements docs state only one tbody is supported. Try moving the tbody outside the loop.

    I suspect you will see the info will show something like this Showing 1 to 1 of 1 entries and sorting searching don't work as expected. Its because Datatables processes the rows in the first -tbody only.

    Kevin

  • nalexg1nalexg1 Posts: 2Questions: 1Answers: 0

    Thank you so much Kevin. You have been a life saver.
    This worked perfectly fine.
    Next time I will read the docs properly.

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

    Next time I will read the docs properly.

    Only if you can find them :smile: There are lots of docs!

    Kevin

Sign In or Register to comment.