0

I have javascript which calls a controller method from javascript and returns the json object. Once the json object is returned, I would like to update a table below the search field which will show the results from the json object. Basically I'm trying to list all the objects but want to filter out the results based on some search on a form.

Following is the example code that I use to get json object:

$.ajax({
  type: "GET",
  dataType: "json",
  url: "/students/search/" + this.value,
  success: function(data){
    // logic to update field
  });
});

UPDATE:

JSON object:

[{"student":{"student_type":"D", "student_name":"Blah Blah"}}]

I have a table in my html:

<table id="studentTable">
</table>

I would like to add student_type and student_name into a table

1
  • Can you show what your JSON data would look like? That will help us give a more specific recommendation as to how to approach this. Commented Jan 27, 2011 at 2:47

1 Answer 1

3

Assuming that the data being returned from the controller is an array of objects, I've done something like this in previous projects:

Using the jQuery template plugin:

$.each(data, function(index, element) {
var t = $.template("<tr><td>${student_type}</td><td>${student_name}</td></tr>");

var tdata = {
    student_type: element.student.student_type,
    student_name: element.student.student_name
};

$("#studentTable").append(t, tdata);

});

All of this should go inside the success callback function. If you could provide the html of where this would go in the view, and the JSON you would get back from the controller i can update my answer to better reflect your scenario.

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

2 Comments

See the updated code for the json object response and the html.
Updated my answer to reflect the samples you provided

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.