Adis Azhar
Adis Azhar

Reputation: 1022

How to use with Data Table jQuery with ajax?

I really need your help here. I am new with jQuery DataTable. What I am doing is, I have a url (backend laravel php) that returns a JSON response like so:

[
{"kode_pt":1,"nama":"title","SK_path":"\/folder","email_PJ":"\/images","validasi":0},
{"kode_pt":2,"nama":"title","SK_path":"\/folder","email_PJ":"\/images","validasi":0}
]

josn response

What I am doing currently is using ajax jquery to GET the data, and add it to the table dynamically. Like this:

  var content="";
  $.ajax({
    url: '{{route('get.pt')}}',
    method: "GET",
    dataType: "json",
    success: function(data){
      for(i=0; i<data.length; i++){
        content+='<tr>'+
          '<td>'+data[i].kode_pt+'</td>'+
          '<td>'+data[i].nama+'</td>'+
          '<td>'+data[i].validasi+'</td>'
          +'<td><button id="accept" data-id='+data[i].kode_pt+' class="btn btn-success">Accept</button><button data-id='+data[i].kode_pt+' class="btn btn-danger">Decline</button><button id="view" data-id='+data[i].kode_pt+' class="btn btn-info">View</button></td>';
      }

  $("#verify-pt-body").html(content);
},
error: function(){
  console.log("Ajax Error - getPT");
}
  });

It works. But when I use the sort and search functionality of data table, the data shown onscreen disappears. After browsing, it turns out I cannot apply it to the table like that. I have to use the DataTable's functions to retrieve the json. I need help with that please. I have looked at few documentations but I have failed to get it working. Help would be much appreciated!!

Syntax of data tables I tried but failed:

$('#dataTable-verify-pt').DataTable({
    responsive: true,
    ajax:{
      url: '{{route('get.pt')}}'
    },
    columns:[
      {data: "kode_pt"},
      {data: "nama"},
      {data: "validasi"},

    ]

  });

This is the format I'm trying to achieve with DataTable:

enter image description here

Upvotes: 2

Views: 2848

Answers (2)

Abhijeet Kale
Abhijeet Kale

Reputation: 389

try it !!

var queryData = {

    //yourinput parameter
}

$.ajax({
        type : "GET",
        url : "your url",
        data : queryData,
        timeout : 100000,
        success : function(data) {
            console.log("SUCCESS: ", data);
            setData(data);  //call function & pass your JSON data to it.
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });

function setData(data) {

var salearr = JSON.parse(JSON.stringify(data)); //parse JSON data.

//create datatable dynamically on the fly 

var tableStr = ' ';
tableStr += "<table id=mydatatable class=mydatatable border=\"1\"  class=\"display\" style='border-collapse: collapse; border-spacing: 0;background:#FFFFFF'>"+

//your table header appears here

//access JSON parsed data using for loop
 for(var k=0; k<salearr.length; k++) {

        //your code with dynamic html

 }

 and finally  apply dataTable to it!!
  $("#mydatatable").dataTable();

}

Upvotes: 0

Amr Aly
Amr Aly

Reputation: 3905

the docs uses this syntax:

$('#dataTable-verify-pt').DataTable({
    responsive: true,
    ajax: "{{route('get.pt')}}",
    columns:[
      {data: "kode_pt"},
      {data: "nama"},
      {data: "validasi"},
    ]
  });

Edit


you need to modify you response so it's an object like so:

{

"data": [
{"kode_pt":1,"nama":"title","SK_path":"\/folder","email_PJ":"\/images","validasi":0},
{"kode_pt":2,"nama":"title","SK_path":"\/folder","email_PJ":"\/images","validasi":0}
]

}

Edit 2


You can achieve so by modifying your response like so:

return response()->json(['data' => $data]);

Upvotes: 2

Related Questions