1

I will need to pass a java variable to a javascript function. I have my codes as below. Wondering is that the correct way?

Because I have some problem here when the page first load, and the button is clicked, the chartData and categories is empty.

I am expecting the below:

chartData = [{"name":"Anne","data":[1.0,0.0,4.0]},
             {"name":"Billy","data":[5.0,7.0,10000.0]}]
categories = ["APPLES","BANANAS","ORANGES"]

public String doLoadChartDataAction () {

    String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
    String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

    System.out.println("1.0 " + " _cat:" + _cat);
    System.out.println("1.0 " + " _data:" + _data);

    setCategories(_cat);
    setChartData(_data);

    return "";      
}


 <p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}" 
    oncomplete="renderChart('container','line','Sample Chart','${pc_Test.chartData}', '${pc_Test.categories}');">   
 </p:commandButton>
4
  • I think when you load the page, you can do an ajax call to your server side, then the server(java) returns the data you want; in your javascript, you can use the data for processing. i.e. the java file returns you an json object and callback processing use the json object.. Commented Oct 17, 2013 at 7:36
  • @Robin is correct do you want an json Object form java and then process in javascript Commented Oct 17, 2013 at 7:44
  • @Robin Thanks for your comments. I manage to fix this with the below amended codes. (: Commented Oct 17, 2013 at 7:52
  • @BholaVishwakarma Thanks for your comments. I manage to fix this with the below amended codes. (: Commented Oct 17, 2013 at 7:52

1 Answer 1

1
public String doLoadChartDataAction () {

String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

System.out.println("1.0 " + " _cat:" + _cat);
System.out.println("1.0 " + " _data:" + _data);

//setCategories(_cat);
//setChartData(_data);

RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("chartData", _data);
context.addCallbackParam("categories", _cat);

return "";      
}

<p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}" 
oncomplete="renderChart('container','line','Sample Chart', args.chartData, args.categories);">   

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.