Can't get array data using Callback Parameters

UI Components for JSF
Post Reply
masofcon
Posts: 9
Joined: 26 Jul 2010, 15:35
Location: Moscow

26 Jul 2010, 16:11

Hello!
I have array of POJO's targetsArray. I need to get it from java script in my page. I am trying to use Callback Parameters.

Bean code

Code: Select all

@ManagedBean(name = "TTAManagerBean")
@RequestScoped
public class TTAManagerBean {
	public Object[] targetsArray;

	public TTAManagerBean() {
		super();
		ArrayList<VTarget> targetsList = new ArrayList<VTarget>();
		targetsList.add(new VTarget("Some target", 1.));
		targetsList.add(new VTarget("Target", 0.6));
		targetsArray = targetsList.toArray();
	}

	public void loadData(ActionEvent actionEvent) {
		RequestContext requestContext = RequestContext.getCurrentInstance();
		requestContext.addCallbackParam("tararray", targetsArray);
	}
}


public class VTarget implements Serializable {
	private static final long serialVersionUID = 1L;

	public String name;
	public double priority;

	public VTarget(String name, double priority) {
		super();
		this.name = name;
		this.priority = priority;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPriority() {
		return priority;
	}

	public void setPriority(double priority) {
		this.priority = priority;
	}
}
Web page

Code: Select all

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.prime.com.tr/ui">

	<ui:decorate template="../template/content.xhtml">
		<ui:define name="body">

			<p:panel id="tree" header="Tree">
			<p:commandButton value="render" actionListener="#{TTAManagerBean.loadData}" oncomplete="render(xhr, status, args)"/>
				<div id="portfolioTree" style="height: 600px;">
                                <script type="text/javascript">

                            	function render(xhr, status, args){
                        		var tararray = args.tararray;
                        		alert(tararray.length);
                        		for (target in tararray)
                         		{
                        			alert(target.name + " " + target.priority);
                        		}
                        	}
                               </script>
                               </div>
			</p:panel>

		</ui:define>
	</ui:decorate>

</ui:composition>

when I press button I get messagebox with text "undefined" and nothing more happens. How can I solve this problem.
Thanks.
PrimeFaces 2.2.RC1, Mojarra 2.0.2, Apache Tomcat 6.0.26

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

26 Jul 2010, 23:50

Not sure if our JSON serializer handles arrays, can you try what the following returns;

Code: Select all

new org.primefaces.json.JSONObject(targetsArray).toString()

masofcon
Posts: 9
Joined: 26 Jul 2010, 15:35
Location: Moscow

27 Jul 2010, 10:40

Thanks for advice.

Finally, looking through repository I found JSONArray class. And using

Code: Select all

new org.primefaces.json.JSONArray(targetsArray, true).toString()
in server side and converting a JSON text into an object in client side solves the problem.

Here is working code:

Code: Select all

@ManagedBean(name = "TTAManagerBean")
@RequestScoped
public class TTAManagerBean {
   public Object[] targetsArray;

   public TTAManagerBean() {
      super();
      ArrayList<VTarget> targetsList = new ArrayList<VTarget>();
      targetsList.add(new VTarget("Some target", 1.));
      targetsList.add(new VTarget("Target", 0.6));
      targetsArray = targetsList.toArray();
   }

   public void loadData(ActionEvent actionEvent) {
      RequestContext requestContext = RequestContext.getCurrentInstance();
      requestContext.addCallbackParam("tararray", new org.primefaces.json.JSONArray(targetsArray, true).toString());
   }
}


public class VTarget implements Serializable {
   private static final long serialVersionUID = 1L;

   public String name;
   public double priority;

   public VTarget(String name, double priority) {
      super();
      this.name = name;
      this.priority = priority;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public double getPriority() {
      return priority;
   }

   public void setPriority(double priority) {
      this.priority = priority;
   }
}
Web page

Code: Select all

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.prime.com.tr/ui">

   <ui:decorate template="../template/content.xhtml">
      <ui:define name="body">

         <p:panel id="tree" header="Tree">
         <p:commandButton value="render" actionListener="#{TTAManagerBean.loadData}" oncomplete="render(xhr, status, args)"/>
            <div id="portfolioTree" style="height: 600px;">
                                <script type="text/javascript">

                               function render(xhr, status, args){
                              var tararray = eval('('+ args.tararray +')');
                              alert(tararray.length);
                              for (target in tararray)
                               {
                                 alert(target.name + " " + target.priority);
                              }
                           }
                               </script>
                               </div>
         </p:panel>

      </ui:define>
   </ui:decorate>

</ui:composition>
Best regards
PrimeFaces 2.2.RC1, Mojarra 2.0.2, Apache Tomcat 6.0.26

moxprox
Posts: 20
Joined: 15 Sep 2012, 10:40

23 Oct 2014, 12:57

You made my day! Thx a lot.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 19 guests