开发者

Pass a PHP string (which represents a JS function) through json_encode()

I'm using this PHP/CodeIgniter library for jQuer开发者_如何学运维y Highcharts: http://www.crustiz.com/php-jquery/highcharts-library-for-codeigniter/

The way that library does things is that it builds a PHP array of options, then converts it to json using json_encode (see line 273 of that library) which is then used by the jQuery Highcharts plugin. This is fine, except the option I'm trying to use is the tooltip formatter, which needs to be a javascript function, not a string (see http://www.highcharts.com/ref/#tooltip).

Since the library doesn't have a tooltip function, I created one as a test:

  function set_tooltip() {
    $this->a_options['tooltip']['formatter'] = 'function() { return this.series.name + "<br>" + this.x + ": " + this.y }';
    return $this;
  }

But this doesn't work as the JS function is output as a string, not a function. Anyone know if there is a way to have it be a function after passing through json_encode without rewriting that part of the library?


JSON isn't meant to convey functions. If you take a look at the documentation, there are only specifications for four types - object, array, string and number - plus the values true, false and null.

If you insist on doing this, perhaps an article like Sending Javascript Functions Over JSON will give you a hand.


JSON doesn't have a "function callback" type so I think this is not possible. It knows only the basic types: string / number / object / array

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜