Sebi
Sebi

Reputation: 2584

JSF: Navigation issue (Ajax / Non-Ajax)

When the following (PrimeFaces) button is pressed

<p:commandButton value="Search"
                 actionListener="#{personController.searchPersons}"
                 update="resultTable"/>

I load a list of objects and display them in a table on the current page. This is done via an Ajax call.

Now I'd like to do the following: If only one object is found, don't update the result table but link directly to the "detail page". (e.g. detail.xhtml)

How can this be done?

I know that i can control the navigation via the return value of my searchPersons() method. But It doesn't work as it should. I think it has to do with the update="resultTable" of my button. But I currently don't know how to solve this...

Upvotes: 2

Views: 1032

Answers (1)

BalusC
BalusC

Reputation: 1108557

You can bring in some JS in oncomplete attribute. It will be executed when the bean action and the partial update is completed. You can change the window location in JS by assigning window.location a new URL.

Here's a random kickoff example, assuming that you've a persons table with id persons and that the detail link look something like <a class="detail" href="detail.xhtml?id=123"> somewhere in the table row.

oncomplete="var $persons = jQuery('#persons tbody tr'); if ($persons.length == 1) window.location = $persons.find('a.detail').attr('href');"

Upvotes: 1

Related Questions