ene*_*nes 4 javascript jsf json
我想将我的arraylist从managedBean发送到javascript代码,
我的豆子在这里:
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)));
}
Run Code Online (Sandbox Code Playgroud)
我的javascript代码在xhtml页面中:
<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>
Run Code Online (Sandbox Code Playgroud)
这是我在xhtml页面中的正文:
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 11
您需要了解JSF在这个问题的上下文中只是一个HTML/JS代码生成器.
您只需要让JSF 以最终语法有效的JS代码的方式打印所需的数据.
categories: #{bean.dataAsJson}
Run Code Online (Sandbox Code Playgroud)
其中getDataAsJson()返回String表示所需JSON代码的a.例如,基本上:
public String getDataAsJson() {
return "['foo', 'bar', 'baz']";
}
Run Code Online (Sandbox Code Playgroud)
要验证结果,请在浏览器中右键单击页面并执行查看源代码.
categories: ['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6355 次 |
| 最近记录: |