开发者

javascript array and jsf/richfaces

I have the following code in javascript:

        <script type="text/javascript"
            src="#{facesContext.externalContext.requestContextPath}/js/sample-points.js"></script>

        <script type="text/javascript">//<![CDATA[
            var cloudmade = new CM.Tiles.CloudMade.Web({key: 'bbb'});
            var map = new CM.Map('cm-example', cloudmade);
            map.setCenter(new CM.LatLng(51.50874, 22.76367), 4);

            var markers = [];
            for (var i = 0; i < samplePoints.length; i++) {
                markers.push(new CM.Marker(new CM.LatLng(samplePoints[i][0], samplePoints[i][1])));
            }

            var clusterer = new CM.MarkerClusterer(map, {clusterRadius: 70});
            clusterer.addMarkers(markers);
        //]]></script>

"samplePoints" is an array of coordinates which I can use to show markers on the map.

Map is showing here:

<div id="cm-example" style="width: 99.5%; height: 600px"></div>

How can I provide this array from jsf/richfaces withou开发者_Python百科t using file (e.g. I want to fetch those data from db, create array and send to this script)?

Thanks


Just let JSF print it as if it is JavaScript code.

Replace

var markers = [];
for (var i = 0; i < samplePoints.length; i++) {
    markers.push(new CM.Marker(new CM.LatLng(samplePoints[i][0], samplePoints[i][1])));
}

by (assuming Facelets)

var markers = [];
<ui:repeat value="#{bean.samplePoints}" var="samplePoint">
    markers.push(new CM.Marker(new CM.LatLng(#{samplePoint[0]}, #{samplePoint[1]})));
</ui:repeat>

where #{bean.samplePoints} returns a List<BigDecimal[]> or something.


See this Link

using jsFunction you can load any data structure (e.g. Points), and on your clients side you get a javaScript data structure that you can easily access it (point.x).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜