Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i refresh dataTable with push in primeFaces

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

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

and in bean :

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

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

<script type="text/javascript">
  function handle() {
          ???
        }
</script>
like image 987
Mahdi Avatar asked Oct 05 '22 09:10

Mahdi


2 Answers

This is a better solution without jQ tricks:

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

And this is a more better solution if you don't wanna lose your filters:

<p:socket channel="/table" >
    <p:ajax event="message" oncomplete="PF('datatableWidgetVar').filter()" />
</p:socket>
like image 121
daVe Avatar answered Oct 07 '22 00:10

daVe


This is my simple test, when soclet in 2.xhtml is received event from server, it will fire click event to commandbutton, and this commandbutton(you can invisible this) will update the target you want : 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>
like image 35
Rong Nguyen Avatar answered Oct 06 '22 22:10

Rong Nguyen