Trigger JavaScript action after Datatable is loaded

javascript, jsf, primefaces

Solution

The javascript method from the `oncomplete` attribute is called after the ajax request has finished and thus after the dataTable is loaded.

So you can do the following:

<p:commandButton ... oncomplete="doSomething()"/>

and everything should work fine.

Problem

In a `JSF 2.1` + `PrimeFaces 3.2` web application, I need to trigger a JavaScript function after a `p:dataTable` is loaded. I know that there is no such event in this component, so I have to find a workaround. In order to better understand the scenario, on page load the dataTable is not rendered. It is rendered after a successful login: ``` <p:commandButton value="Login" update=":aComponentHoldingMyDataTable" action="#{loginBean.login}" oncomplete="handleLoginRequest(xhr, status, args)"/> ``` As you can see from the above code, I have a JavaScript hook after the successful login, if it can be of any help. The `update` attribute forces the dataTable's rendered attribute to revaluate: ``` <p:dataTable var="person" value="#{myBean.lazyModel}" rendered="#{p:userPrincipal() != null}" /> ``` After the datatable is loaded, I need to run a JavaScript function on each row item, in order to subscribe to a cometD topic. In theory I could use the `oncomplete` attribute of the login Button for triggering a property from `myBean` in order to retrieve once again the values to be displayed in the dataTable, but it doesn't seem very elegant. The JavaScript function should do something with the `rowKey` of each row of the dataTable: ``` function javaScriptFunctionToBeTriggered(rowKey) { // do something } ```

Original source