How can I update dataTable using push in primeFaces

I need to know how I can update a DataTable in index1.xhtml when changing data in index2.xhtml
using push ... I define a socket in index1.xhtml like this:

<p:socket channel="/table" onMessage="handle"/>

and bean:

public void contract(){
 ....
PushContext pcont=PushContextFactory.getDefault().getPushContext();
pcont.push("/table",something);
}

the thing i don't know is how i can update dataTable in javaScript:

<script type="text/javascript">
  function handle() {
          ???
        }
</script>
+5
source share
2 answers

This is the best jQ trick-free solution:

<p:socket channel="/table" >
    <p:ajax event="message" update=":datatable" />
</p:socket>

And this is a more efficient solution if you do not want to lose your filters:

<p:socket channel="/table" >
    <p:ajax event="message" oncomplete="PF('datatableWidgetVar').filter()" />
</p:socket>
+3
source

This is my simple test, when soclet in 2.xhtml receives an event from the server, it will fire a click event in commandbutton, and this command (you can make it invisible) will update the desired target: 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>
+1
source

All Articles