Matthew Oakley
Matthew Oakley

Reputation: 527

Refresh JSF component after custom javascript Ajax call

I have a custom javascript Ajax call in my jsf page. I capture and process the query string of the XMLHttpRequest in a filter. The filter adds a record to a table in the model. Now I want the jsf page, without full page refresh, to reflect the updated model in one of the components (a Primefaces data table).

I guess what I need is a custom xmlHttpResponse...

Can anyone tell me how to do this? I'm scared it might be complicated, but I have no choice but to use the custom javascript...

Upvotes: 1

Views: 8309

Answers (1)

BalusC
BalusC

Reputation: 1109570

The PrimeFaces <p:remoteCommand> is designed for this purpose.

Here's a basic kickoff example for your particular case:

<h:form>
    <p:remoteCommand name="updateTable" action="#{bean.updateTable}" 
        process="@this" update="table" />
    ...
    <p:dataTable id="table">...</p:dataTable>
</h:form>

It generates a JS function updateTable() which will invoke the #{bean.updateTable} method and update the component with (relative) client ID table. You just have to call the function updateTable() in JavaScript context.

updateTable();

You can even pass request parameters if necessary, it has to be sent as JS object:

updateTable({ name1: "value1", name2: "value2 });

No need to fiddle with homebrewed ajax requests and keeping the JSF state in sync.

Upvotes: 5

Related Questions