我需要知道如何在index2.xhtml中更改数据时更新index1.xhtml中的DataTable 使用push ...我在index1.xhtml中定义socket,如下所示:
<p:socket channel="/table" onMessage="handle"/>
并在bean中:
public void contract(){
....
PushContext pcont=PushContextFactory.getDefault().getPushContext();
pcont.push("/table",something);
}
我不知道的是,我如何在javaScript中更新dataTable:
<script type="text/javascript">
function handle() {
???
}
</script>
答案 0 :(得分:3)
如果没有jQ技巧,这是一个更好的解决方案:
<p:socket channel="/table" >
<p:ajax event="message" update=":datatable" />
</p:socket>
如果您不想丢失过滤器,这是一个更好的解决方案:
<p:socket channel="/table" >
<p:ajax event="message" oncomplete="PF('datatableWidgetVar').filter()" />
</p:socket>
答案 1 :(得分:1)
这是我的简单测试,当2.xhtml中的soclet从服务器接收到事件时,它会将click事件触发到commandbutton,而这个命令按钮(你可以隐藏它)将更新你想要的目标: 豆:
@ManagedBean(name = "globalCounter")
@SessionScoped // option
public class GlobalCounterBean implements Serializable {
private static final long serialVersionUID = 1L;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void increment() {
count++;
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/counter", String.valueOf(count));
}
}
1.xhtml:
<h:body>
<h:form id="form">
<h:outputText id="out" value="#{globalCounter.count}" styleClass="ui-widget display" />
</h:form>
<p:socket onMessage="handleMessage" channel="/counter" />
</h:body>
2.xhtml:
<h:form id="form">
<h:outputText id="out" value="#{globalCounter.count}" styleClass="ui-widget display" />
<br />
<p:commandButton onclick="alert('test')" id="btn" process="@form" value="Click" update="@parent" />
</h:form>
<p:socket onMessage="handleMessage" channel="/counter" />
<script type="text/javascript">
function handleMessage(data) {
$('#form\\:btn').click();
}
</script>