I’m new to the play framework and I have a question about implementing ajax.
So I have implemented a search page – which has a form on it that sends back a date range.
And it is all working.
But I wanted to get it to work with ajax so I have a few questions.
Do I need to create a new Form Type in the controller? Like I have done previously? Or can I get the data out manually?
So currently in my html i have the following form:
<form action="@routes.History.search()" class="pull-right" id="formHistory">
MinDate: <input id="mindate" name="mindate" class="input-small" type="text">
MaxDate: <input id="maxdate" name="maxdate" class="input-small" type="text">
<button class="btn" type="submit">Search</button>
</form>
and here is my javascript:
$('#formHistory').submit( function(evt) {
$('#errors').show();
$.ajax({
type: 'POST',
url: jQuery("#formHistory").attr("action"),
data: jQuery("#formHistory").serialize(),
dataType: "json",
success: function(data) {
alert('History Search');
$("tableHistory").find("tr:gt(0)").remove();
alert(data.id[0].source);
},
error: function(data) {
$('#errors').shows();
alert('History search failed');
}
});
return false;
});
and in my history.java file i need to get the json information out. Previously i have done this by using
Form<Login> loginForm = form(Login.class).bindFromRequest();
But i do not have an object with min and max date can i just get the json information out manually?
public static Result search() {
//So here I would like to get the form information
}
The next question would be how to do this in reverse? Once i have gotten the min and max date, gotten the information out of the database i need to send back a response. Should I send back a json response with a list of the data and how do handle this in javascript? or do I create html on the server and send this as a string and append this in the apporpriate element?
Or the other option i have seen in some demos is creating a view and passing that back and just rendering the smaller view
I ended up leaving it and not using ajax.
But if I was going to use it I would use an ajax request:
and use DynamicForm on the server side:
Something like: