nafeeur10's avatar

How to put GET Requested Data in Datatable

I am using Data Table to show Invoice Details.

In my DataTable I used Collection Filter.

https://datatables.net/extensions/buttons/examples/initialisation/collections-sub.html

Here I want to make an AJAX Request which will give me Collection of Invoice. I have to put these on dataTable.

Data will be like that:

[
{
    InvoiceNumber: 343434,
    companyName: Aspile Inc.
},
{
    InvoiceNumber: 343434,
    companyName: Google Inc.
}
]
$.ajax({
     url: "{{ route('finance') }}",
      method: 'GET',
      success: function(data) {
               ?????
      }, error: function(data) {
               console.log(data);
      }
})

DataTable is as usual........ How is this possible?

0 likes
6 replies
gokulkhatiwada's avatar

Why don't you use server side rendering ? table

 <table id="yourTable" >
	<thead>
		<tr>
			<th class="datatable-nosort">#</th>
			<th>Invoice</th>
			<th>Company</th>
			<th class="datatable-nosort">Actions</th>
		</tr>
	</thead>
	<tbody>
	</tbody>	
</table>
                                    	

script

 var t = $("#yourTable").DataTable({
            scrollCollapse: true,
            autoWidth: false,
            responsive: true,
            processing: true,
            serverSide: true,
            ajax: '{{ route('get.data') }}',
            columns: [

                {data: 'id'},
                {data: 'invoice_no'},
                {data: 'company'},      
                {data: 'actions'},
            ],
            columnDefs: [
                {
                    targets: "datatable-nosort",
                    orderable: false,
                }              
            ],
            buttons: [
                'copy','print','csv','pdf'
            ]
        });
        t.on('order.dt search.dt', function () {
            t.column(0, {search: 'applied', order: 'applied'}).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();
nafeeur10's avatar

@gokulkhatiwada,,

Thank you for your Reply. I am using the AJAX request in the Button Action.

Then what will be the changes?

buttons: [
 {
      text: 'This Week',
      action: function ( e, dt, node, config ) {
      $.ajax({
          url: "{{ route('finance-this-week') }}",
          method: 'GET',
          dataType: "json",
          success: function(data) {
                columns: [
                    { 'data': 'id' },
                    { 'data': 'invoiceNumber' },
                    { 'data': 'customerId' },
                    { 'data': 'actions' }
                ]
            }, error: function(data) {
                    console.log(data);
            }
       })
  }
 }
]
gokulkhatiwada's avatar

With your setup you need to parse JSON response and iterate each with pushing into DOM table body

1 like
gokulkhatiwada's avatar
JSON.parse(response).forEach(function(item,index){
var html ='<tr>'+ 
'<td>'+index+'</td>'+
'<td>'+item.invoiceNumer+'</td>'+
.
.
'</tr>';
$('#table > tbody.last-child').append(html);
}
gokulkhatiwada's avatar

I would rather return html and append to DOM instead of returning JSON

Please or to participate in this conversation.