plamb
plamb

Reputation: 5636

Play framework render HTML & values on same page via ajax

I'm using ajax within the play (scala) framework to run some code after a click event occurs on a page. I want to return the results of that code execution on the same page.

EDIT: the goal of this is to display some rows from the database, along with form elements to fill out to update cells of the databse, all without reloading/or going to a new page

Is there a way to pass html + (scala) values into

Ok(...).as(HTML) 

to display without directing to new a page?

I know Ok can take things like:

Ok(<h1>it works</h1>).as(HTML) 

but I can't figure out how to combine that with scala values as well. Is there a smarter way to do this?

EDIT: the ajax looks like this:

var ajaxCall = function() {
var ajaxCallBack = {
    success : onSuccess,
    error : onError
}

jsRoutes.controllers.Application.scrape().ajax(ajaxCallBack);
};

var  onSuccess = function(data) {
$("#results").append(data);
}

var onError = function(error) {
alert(error);
}

Upvotes: 1

Views: 1119

Answers (1)

Andriy Kuba
Andriy Kuba

Reputation: 8263

For sure you can. You can use TWIRL templates:

Create a template "views/Application/contacts.scala.html

@(users: List[User])

<h1>Users</h1>

<ul>
@for(user <- users) {
  <li>@user.name</li>
}
</ul>

in the controller:

Ok(views.html.Application.contacts(users))

Upvotes: 2

Related Questions