Sam
Sam

Reputation: 3485

Rendering a portion of a Play framework 2.0.2 template

I'm new to Play (and to MVC to be honest). And I'm having an issue with something that doesn't seem like it should be so hard.

I just found this identically named post: Rendering a portion of a Play framework 2.0 template

But I can't seem to get this working.

I have a template (index) containing another template (rows) which I want to render when a button is clicked. This is the form in my index template:

@form(routes.Application.getNextRows()) {<input type="submit" value="Next Rows!">}

This is getNextRows:

public static Result getNextRows() {
    return ok(views.html.tags.rows.render(rowsIterator.next()));
}

That is where I think i have my problem, but I have no idea how i should be making that call, or what I should be returning (without getting some NullPointerException).

Here is the bit that needs to be re-rendered (in index):

<table cellspacing="0"  style="table-layout:fixed"  border="2">
    <col width="50%">
    <col width="45%">
    <col width="5%">
    @tags.rows(rowsIterator.first())
</table>

So when I click the button, I need to pass in a new rows element, instead of "rowsIterator.first()" Presumably I'm doing something very wrong, or missing some obvious call that sorts this out.

Thanks for any help (and if it is obvious that I am missing some bigger concept, please feel free to educate me).


Looking through the Zentasks sample, I see code like this, which appears to make a section of the page reload. Any idea how this works?

<a class="name" href="#@routes.Tasks.index(project.id)">@project.name</a>

Upvotes: 3

Views: 1635

Answers (2)

hiltym
hiltym

Reputation: 146

A few possibilities to get that state out of your static field (which definitely won't work well):

  • Give all the rows back to the client and let it do the rest in JavaScript (does not work well if there's too much data)
  • Store the number of the latest row in the session (which is in a cookie)
  • Handle the row number explicitly and let the client return it when it wants the next ones
  • Do RESTful paging (see Paging in a Rest Collection and Pagination in a REST web application)
  • Try to leverage a distributed cache

Upvotes: 1

Marius Soutier
Marius Soutier

Reputation: 11274

Sorry for not providing you with a complete solution, but IMHO you should dive a little more into the basics first.

Your action method returns an entire page, not just a part of it, because it's a full HTTP response. That means whatever you call in that action is what your browser will render. getNextRows needs a parameter to find the next rows. To change only a part of the page, you need AJAX.

The button click should re-render the index page with the next set of rows. Add rows as a parameter to the index template. Call your rows template with these rows.

Upvotes: 3

Related Questions