【问题标题】:'Escape' javascript function within rails statementrails 语句中的“转义”javascript 函数
【发布时间】:2014-05-02 11:14:59
【问题描述】:

我有以下代码(使用 chartkick gem):

<%= pie_chart [[t('result.positive'), @rating_positive], [t('result.negative'), @rating_negative], [t('result.neutral'), @rating_neutral]],
    colors: ['#B3D986', '#E04848', '#C9C9C9'],
    library: {
      chart: {
        backgroundColor: '#EEEEEE',
        width: 300,
        height: 250
      },
      plotOptions: {
        pie: {
          allowPointSelect: true,
          cursor: 'pointer',
          dataLabels: {
              enabled: false
          },
          showInLegend: true,
          events: {
            click: function(event, i) {
               alert("test");
            }
          }
        }
      }
    }
  %>

我只想将点击事件函数作为参数传递,但 rails 会解释它。 html_safeand 辅助函数没有用。有什么建议吗?

【问题讨论】:

  • “将点击事件函数作为参数传递,但rails解释它”请详细说明此问题
  • 我收到以下错误:pastebin
  • 事件在被点击之前没有被初始化:function(event, i)
  • 它正在工作here...

标签: javascript ruby-on-rails chartkick


【解决方案1】:

目前,chartkick 无法接受函数,因为它使用 json 作为参数的方式。我发现这个github discussion 讨论了这个。他们提到一个可能的解决方案是将函数作为字符串传递,然后修改 json 的解析方式,类似于article

另一个可能的解决方案,因为我认为chartkick 对其js 使用highcharts 的印象是,在页面/图表已经加载后更新事件。我相信它看起来像这样

$("#container").highcharts().series[0].update({
  events:{
    click: function (event, i) {
      alert(event.point.name);
    }
  }
})

【讨论】:

  • 我使用了您的更新建议,效果很好!
猜你喜欢
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
相关资源
最近更新 更多