在我的JSF 2 Web应用程序中,我使用下面的代码来根据selectedStatus显示和切换一个rich:dataTable的内容。
<h:selectOneRadio id="statusSelection" value="#{backingBean.selectedStatus}" style="width:auto; float:left;">
<f:selectItem itemValue="AVAILABLE" itemLabel="Available" />
<f:selectItem itemValue="INACTIVE" itemLabel="Inactive" />
<f:ajax render="itemsDataTable" execute="#{backingBean.sortByTitel('ascending')}" />
<f:ajax render="itemsDataTable" event="click" />
</h:selectOneRadio>
dataTable中包含了a4j:commandLink s,在某些IE版本中,在改变表格内容后,无意中需要双击它--我发现,执行以下Javascript代码(在IE的调试控制台,在表格内容改变后)可以解决这个问题。
document.getElementById(<dataTableClientId>).focus()
我的问题是:如何实现在表格内容改变后自动执行javascript代码?
为了让JS代码在表的内容发生变化后自动执行 <f:ajax>
成功完成,以下内联解决方案即可。
<f:ajax
render="itemsDataTable"
onevent="function(data) { if (data.status === 'success') {
// Put your JS code here.
document.getElementById('dataTableClientId').focus();
}}" />
或者是以下的外部函数解决方案(注意:不要把括号加在 onevent
,只有函数名)。)
<f:ajax
render="itemsDataTable"
onevent="scriptFunctionName" />
function scriptFunctionName(data) {
if (data.status === 'success') {
// Put your JS code here.
document.getElementById('dataTableClientId').focus();
}
}
也可以看看这个答案。JSF和Jquery AJAX - 如何让它们一起工作?
另外,仔细检查一下是否需要 event="click"
在你 f:ajax
中的默认事件,因为 <h:selectOneRadio>
是 change
我想你最好用... 参见 我可以向f:ajax标签的事件属性传递什么值?