1

Good day!

How can I display my JSON Data into a table? this is the json data that im getting..

{"total":1,"per_page":6,"current_page":1,"last_page":1,"from":1,"to":1,
"data":{"id":999,
   "firstName":"user999",
   "lastName":"lastname999",
   "middleName":"middlename999",
   "company":"999 Company",
   "email":"[email protected]",
   "phone":"09063330756",
   "addressStreet":"999 Street",
   "addressCity":"999City",
   "addressProvince":"999Province",
   "addressPostalCode":2147483647,
   "status":null,
   "notes":"Supplier999"}]
}

for now Im just trying to figure out how to display my data into the table.. then after that I'll try to paginate it, by the way Im using an AJAX call.

here is my ajax code:

function search(){


var keyWord = $("#searchSupplier").val();
$.ajax({
    url: 'api/searchSupplier',
    type: 'get',
    data: {searchKey: keyWord},
    success: function(response) {

        $('table#supplierTable tbody').html("<tr><td>"+response.data+"</td></tr>");
    }
});
}

I tried to:

console.log(response.data) = undefined

and

console.log(response) = the JSON data above.

here is my laravel code:

public function getSuppliers()
{
   $input = Input::get('searchKey');
   return Supplier::where('firstName', 'like', '%'.$input.'%')->paginate(6)->toJson();
}

Thanks in advance.

4
  • 1
    The response should be useable as an object rather than text...add dataType='json' Commented Jan 21, 2014 at 14:37
  • thanks for that comment @Snowburnt, how can I make it useable as an object? sorry I have no idea how to do it. :/ Commented Jan 21, 2014 at 14:40
  • 1
    sorry, if you add dataType='json' to the ajax settings the success callback parameter will be a javascript object and you should be able to use it as a mulitdimensional keyed array Commented Jan 21, 2014 at 14:43
  • @Snowburnt Thank you very much for your Help, thats what Im missing on my code. Commented Jan 21, 2014 at 20:38

2 Answers 2

4

If you're going to do this manually, you need to do something like this:

success: function(response){
  var html = '<table><tbody>';
  response.data.forEach(function(row){
    html += '<tr><td>' + row.id + '</td><td>' + row.firstName + ...;
  });
  html += '</tbody></table>';
  //Set some elements innerHTML to html, or create the table some other way
}

If the server serves the JSON properly, jQuery will automatically parse it to a javascript object that you can access in the above fashion.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Peter for your answer, But Im getting an undefined error on "response.data". when I tried to console.log(response) then I get the whole json data, Im able to access it through dot notation. Sorry for the late response, I had trouble on my internet
I made it working already, Im just need to put dataType: json then I can use the dot notation already, I also used your code and its working. Thank you very much and have a good day
I'm glad I could be of help. If the server sends the JSON with the correct MIME type, application/json, jQuery should be able to detect this and default to parsing the response as an object (or array, in other cases). Since you had to specify dataType, I'm guessing the server sets the wrong MIME when responding.
2

Use the datables plugin, it's open source and easy to use.

http://datatables.net/

2 Comments

Thanks for the link :) Im researching on how to implement this on my code. :)
Did you ever look into impementing this @melvnberd?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.