How to pass Arrays from backing bean to JavaScript to draw a chart using Chart.js in an Ajax call

๐Ÿ‘:0

you can try to use jsf expression language in javascript code

For example:

//<![CDATA[
var jsVar = #{managedBean.property}
//]]>

๐Ÿ‘:0

Well, if you are concerned with the calling (and passing values) of JS function from bean, then you can call execute of RequestContext.getCurrentInstance() to directly invoke your scripting method from the bean, as following:

RequestContext.getCurrentInstance().execute("yourJSFunction('" + param1 + "');");

However, I think you will still be missing a trick of converting your JSONArray back to JSON object in JS, which you can do as following:

When passed from bean:

function yourJSFunction(coordinatesArray) {
    coordinatesArray = JSON.parse(coordinatesArray);
}

Or when using as a bean property:

var coordinatesArr = JSON.parse('#{yourBean.strProperty}');

Leave a comment