![](/img/trans.png)
[英]Passing json_encoded PHP array to JavaScript function
[英]Passing a JavaScript function from JSON encoded PHP
PHP - 這是由PHP函數(json_encoded)返回的Highchart配置數組的一部分
'plotOptions' => array(
'pie' => array(
'allowPointSelect' => TRUE,
'cursor' => 'pointer',
'dataLabels' => array(
'enabled' => TRUE,
'color' => '#000000',
'connectorColor' => '#000000',
'formatter' => "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %' }"
)
)
)
JavaScript - 從編碼數據中訪問此信息
container.highcharts(r.hc);
// r.hc is the array that contains the highchart information above
我遇到的問題是:
Uncaught TypeError: Object function() { return '<b>'+ this.point.name +'</b>: '+
this.percentage +' %' } has no method 'call'
如何更改此設置以將其識別為功能? 或者這甚至可能嗎?
從遠程源執行javascript是一個壞主意,因為你不能相信它。 但是,如果你必須:
包含在json中的函數是一個字符串,因此您需要對其進行評估:
pie.dataLabels.formatter = eval('('+pie.dataLabels.formatter+')');
對於需要轉換的對象的每個方法。
但是,正如一開始所說,幾乎可以肯定的是,解決方案不需要您以這種方式獲取javascript。
我決定只返回餅圖高圖的系列數據...而不是整個高圖配置數組... plotOptions現在在javascript中定義,而不是由json_encoded PHP數組返回..
我不太清楚為什么我這樣做但感謝eval的所有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.