重新加载charkick pie_chart而不刷新页面

Reload charkick pie_chart without refreshing the page

本文关键字:chart 刷新 pie 新加载 加载 charkick      更新时间:2023-09-26

我的目标是,一旦提交删除操作,图表就应该在不刷新页面的情况下更新其值。这是我的代码:

在index.html.erb:中

<div class="chart" style="float: right; width: 500px" >
    <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
      <%= pie_chart current_user.events.group(:app_name).count %>
</div>

在destroy.js:

   <% if @event.destroyed? %>
   $('#event-' +<%= @event.id %>).slideUp();
   $('.message' ).html('<div class="alert alert-success"><%= escape_javascript(flash.discard(:notice)) %></div>');
   $('.message').toggle(1000);
   $('.chart').html('<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %><%= pie_chart current_user.events.group(:app_name).count %>');
<% else %>
   $('.message' ).html("<div class='alert alert-danger'><%= flash[:error] %></div>");
<% end %>

所以我试图重新加载图表类,但它不起作用,我做错了什么?

you can use location.reload()/window.location.reload().

Location.reload()方法从当前URL重新加载资源。它的可选唯一参数是布尔值,当它为true时,会导致页面始终从服务器重新加载。如果为false或未指定,浏览器可能会从缓存中重新加载页面。

var intervalID = setInterval(function(){
   $('.chart').each(function(){
      $(this).load(this.href);
   })
}, 900);