5

我需要知道如何在 index2.xhtml 中
使用 push 更改数据时更新 index1.xhtml 中的 DataTable ...我在 index1.xhtml 中定义套接字,如下所示:

<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>
4

2 回答 2

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>
于 2014-12-04T11:51:11.830 回答
1

这是我的简单测试,当 2.xhtml 中的 soclet 从服务器接收到事件时,它会触发点击事件到命令按钮,并且这个命令按钮(你可以不可见)将更新你想要的目标: Bean:

@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>
于 2013-04-16T15:27:11.137 回答