5

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>
Mahdi
  • 748
  • 14
  • 24

2 Answers2

3

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>
David Gras
  • 958
  • 2
  • 12
  • 21
1

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>
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53
  • I have this error: SEVERE: The ResourceConfig instance does not contain any root resource classes. and after that java.lang.NullPointerException: null...these all happens when page load.before click on button! – Mahdi Apr 18 '13 at 10:02
  • maybe i make mistake in configure push servlet...when i add push servlet config in web.xml those errors happens. – Mahdi Apr 18 '13 at 10:08
  • My configuration: Push Servlet org.primefaces.push.PushServlet Push Servlet /primepush/* – Rong Nguyen Apr 18 '13 at 10:14
  • Then you create 1.xhtml and 2.xhtml in primepush folder in web folder. – Rong Nguyen Apr 18 '13 at 10:15
  • atmosphere-compat-jbossweb-1.1.0.RC1.jar annotation-detector-3.0.1.jar atmosphere-compat-tomcat-1.1.0.RC1.jar atmosphere-compat-tomcat7-1.1.0.RC1.jar atmosphere-runtime-1.1.0.RC1.jar – Rong Nguyen Apr 18 '13 at 10:20
  • i have all dependency that u write...except annotation-detector-3.0.1.jar – Mahdi Apr 18 '13 at 10:24
  • If you still have issue, i will send you my test project in next week :) – Rong Nguyen Apr 18 '13 at 10:33
  • Hi, this solution is very old I am implementing push notification and in your provided solution `PushContext pushContext = PushContextFactory.getDefault().getPushContext();` is getting deprecated method, and having Null Pointer Exception on it – Sarz Mar 03 '16 at 07:31
  • @Sarz Sorry, I didn't work with jsf for a long time ago. – Rong Nguyen Mar 03 '16 at 07:37