I want to send my arraylist from managedBean to javascript code,
my bean is here:
public void getDataAsJson(){
String [] dizi={"Tokyo","Jakarta","New York","Seoul",
"Manila","Mumbai","Sao Paulo","Mexico City",
"Dehli","Osaka","Cairo","Kolkata",
"Los Angeles","Shanghai","Moscow","Beijing",
"Buenos Aires","Guangzhou","Shenzhen","Istanbul"};
Random rnd =new Random();
JSONObject obj= new JSONObject();
for (int i = 0; i < dizi.length; i++)
obj.put(dizi[i], new Integer(rnd.nextInt(80)));
}
my javascript code is here in xhtml page:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
zoomType: 'xy'
},
title: {
text: 'avarage'
},
subtitle: {
text: ''
},
xAxis: [{
gridLineWidth: 0.5,
categories: [// here is my city names which come from mybean]
}],
yAxis: [{ // Primary yAxis
labels: {
formatter: function() {
return this.value;
},
style: {
color: '#89A54E'
}
},
title: {
text: 'avarage',
style: {
color: '#89A54E'
}
}
}],
series: [{
name: 'avarage',
color: '#89A54E',
type: 'spline',
data: [// // here is my city's avarage which come from mybean],
labels: {
rotation: -90,
align: 'right',
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
}]
});
});
});
//-->
</script>
here is my body in xhtml page:
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
You need to understand that JSF is in the context of this question merely a HTML/JS code generator.
You just need to let JSF print the desired data in such way that it ends up in syntactically valid JS code.
categories: #{bean.dataAsJson}
Wherein getDataAsJson()
returns a String
representing the desired JSON code. E.g. basically:
public String getDataAsJson() {
return "['foo', 'bar', 'baz']";
}
To verify the result, rightclick page in browser and do View Source.
categories: ['foo', 'bar', 'baz']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With