Situation:
When I clicking a next-page link for changing content of current page, the chart drawed by C3js doesn’t update with new data.
問題發生情況:
當我按下網頁內容中的下一頁連結,網頁的折線圖並沒有跟著新資料重新繪製。
Environment:
A rails app with kaminari gem, c3-rails gem and gon gem. A controller in rails app uses gon gem to passing values of variables to a .html.erb of view, and then codes in .coffee.js will take values of a gon variable in a .html.erb to draw a line chart for representing trend of data.
問題發生環境:
Rails app,以及搭配使用分頁套件kaminari, 繪圖套件c3-rails和傳遞資料套件gon。Rails app的一個controller使用gon套件傳遞變數的資料值到view的一個頁面,然後一個.coffee.js檔案會使用在view的一個頁面的gon變數值,用它來產生表示資料趨勢的折線圖。
Clue:
Values of a gon variable only be updated when I refreshing the whole page in a browser instead of clicking a link of next-page or back-page. After I use keywords, which are “gon gem rails” and “replace a value in header through javascript”, to finding solutions, I get a solution from this webpage and issue#106 of gon gem at GitHub.com.
解決問題的線索:
只有當我按下瀏覽器的重新整理按鈕,一個gon所管理的變數值才會更新,而不是如我預期的一樣,當我點選下一頁或上一頁的網頁連結,gon管理的變數值也會跟著新的頁面內容一起變成新的。在使用關鍵字「gon gem rails」和「replace a value in header through javascript」搜尋後,我從 stackoverflow網頁 和 gon gem的issue區#106 找到解決方法。
Solution:
Move the statement: <%= include_gon(:init => true) %>
from header area in app/views/layouts/application.html.erb
for setting up gon gem to body area of the page, for example app/views/vegetables/index.html.erb
, that you want to show.
解決方法:
將原本在 app/views/layouts/application.html.erb
中 header 區域設定 gon 套件的程式碼 <%= include_gon(:init => true) %>
, 移到你想要展示折線圖頁面的 body 區域,例如我們想展示的頁面是 app/views/vegetables/index.html.erb
。
Result:
My line chart can update with belowed data in the page when they are changed.
結果:
折線圖終於可以隨著該頁面底下資料變動,而一起重新繪製了。