简体   繁体   中英

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 without using file (eg 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 (eg Points), and on your clients side you get a javaScript data structure that you can easily access it (point.x).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM