php - Encode to JavaScript object, function get replaced -
i have issue on highcharts project , charts options saving in database . printing on chart using following code . let me explain issue using sample code .
echo 'var options69 = '.$highcharts_json_options; echo php_eol; echo 'var options69_func = '. json_encode( $functions); echo php_eol; echo '$(document).ready(function() {'; echo php_eol; echo ' chart69 = new highcharts.chart( $.extend(true, options69, options69_func) );'; echo php_eol; echo '});'; the issue $functions not converts correct javascript object . here example of code of $functions
$functions = array( 'chart' => array( 'events' => array( 'click' => "function(){inboxmenu(this.options);}" ) ), 'tooltip' => array( 'formatter' => "function(){ return '<b>'+ this.series.name + ':</b>'+ this.y; }" ) ); currently, json_encode( $functions) outputs
var options69_func = { "chart": { "events": { "click": "function(){inboxmenu(this.options);}" } }, "tooltip": { "formatter": "function(){return '<b>'+ this.series.name + ':<\/b>'+ this.y;}" } } but need value var options69_func like
var options69_func = { "chart": { "events": { "click": function(){ inboxmenu(this.options); } } }, "tooltip": { "formatter": function(){ return '<b>'+ this.series.name + ':<\/b>'+ this.y; } } } then chart click event or formatter works .. these values coming dynamically server database . add callback function json_encode ?
i added php generated js code http://jsfiddle.net/u9w6x/
i won't using eval function.. on client side
after options69_func, if want excute function "click", can try this: eval('('+options69_func.tooltip.click+')'+'();');
Comments
Post a Comment