最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jsf - Trigger JavaScript action after Datatable is loaded - Stack Overflow

programmeradmin6浏览0评论

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 ponent, 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:mandButton value="Login" update=":aComponentHoldingMyDataTable" 
                                    action="#{loginBean.login}" 
                                    onplete="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 etD topic.

In theory I could use the onplete 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
}

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 ponent, 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:mandButton value="Login" update=":aComponentHoldingMyDataTable" 
                                    action="#{loginBean.login}" 
                                    onplete="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 etD topic.

In theory I could use the onplete 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
}
Share Improve this question edited Jun 11, 2012 at 7:33 perissf asked Jun 10, 2012 at 20:02 perissfperissf 16.3k14 gold badges84 silver badges120 bronze badges 8
  • Can you maybe explain what you are trying to acplish from the description of a user story? I am having trouble understanding what you want to acplish because maybe there is a much simpler and more elegant way for you to achieve what you are trying to do. – maple_shaft Commented Jun 10, 2012 at 22:05
  • @maple_shaft I have tried to be as clear as possible, and I dedicated some time in posting my question. Please let me know what is unclear so that I can improve it. – perissf Commented Jun 10, 2012 at 22:14
  • "Immediately after the onplete action has finished, the update attribute renders the dataTable". This part is not clear. onplete is client side action and es after the ajax update. – Matt Handy Commented Jun 11, 2012 at 7:28
  • Thanks @MattHandy , I have reformulated the point. If I understand your suggestion, I could use handleLoginRequest for reading the table's rowkeys. – perissf Commented Jun 11, 2012 at 7:35
  • Yes I am wondering why onplete is not sufficient for you. Do you want to make another ajax call from your javascript function? – Matt Handy Commented Jun 11, 2012 at 7:36
 |  Show 3 more ments

2 Answers 2

Reset to default 4

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

So you can do the following:

<p:mandButton ... onplete="doSomething()"/>

and everything should work fine.

if the page reload, try to call in the document ready:

$(document).ready(function() {
    doSomething();
});
发布评论

评论列表(0)

  1. 暂无评论