简体   繁体   English

为什么我使用 Datatables 的 Rails 应用程序在我使用后退按钮时无法正确绘制表格,但在我刷新页面时却如此?

[英]Why does my rails app that uses Datatables not draw tables correctly when I use the back button, but does when I refresh the page?

I have built a rails app, using rails 5.2.3 and ruby 2.6.3 for browsing the user logs for another application and editing its users and their status.我已经构建了一个 rails 应用程序,使用 rails 5.2.3 和 ruby 2.6.3 来浏览另一个应用程序的用户日志并编辑其用户及其状态。 It's a simple app and I use rails Datatables to layout the tables.这是一个简单的应用程序,我使用 rails Datatables 来布置表格。 When I enter the url for the site, eg https://example.com/my_app/logs , it displays the log table with sorting, pagination, etc., just as it should.当我为该站点输入 url 时,例如https://example.com/my_app/logs ,它会按应有的方式显示带有排序、分页等的日志表。 If I move to my 'About' page, and from there click on the 'Back' link, it returns to the logs index view and shows the same url in the browser bar, BUT, the tables are not laid out using Datatables.如果我移动到我的“关于”页面,然后从那里单击“返回”链接,它会返回到日志索引视图并在浏览器栏中显示相同的 url,但是,表格不是使用 Datatables 布置的。 If I click the reload web page on the browser, it loads and draws correctly.如果我在浏览器上单击重新加载 web 页面,它会正确加载和绘制。 The back link on the About page is:关于页面的反向链接是:

<%= link_to 'Back', logs_path %>

And the logs_path is /logs而 logs_path 是 /logs

The application.js has: application.js 有:

//= require_self
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require jquery.turbolinks
//= require turbolinks
//= require_tree .

The logs.coffee has: logs.coffee 有:

jQuery ->
        $('#posts-table').dataTable()
          sPaginationType: "full_numbers"
          bJQueryUI: true

And the views/logs/index.html.erb has for the table:并且 views/logs/index.html.erb 具有该表:

<table id="posts-table" class="display" width="900">

So I guess I'm wondering, does the placement of the jQuery Datatable statement in the logs.coffee mean that it is not included when the link is executed?所以我想我想知道,在 logs.coffee 中放置 jQuery Datatable 语句是否意味着执行链接时不包含它? If not, then where should I put the dataTable() statement and what should it look like?如果没有,那么我应该把 dataTable() 语句放在哪里,它应该是什么样子?

Since you're using turbolinks, it looks like an issue with how turbolinks loads and caches the page.由于您使用的是 turbolinks,因此 turbolinks 如何加载和缓存页面似乎存在问题。

You could try placing your code inside a 'turbolinks:load' listener, like this:您可以尝试将代码放在“turbolinks:load”侦听器中,如下所示:

$(document).on 'turbolinks:load', () ->
    $('#posts-table').dataTable()
          sPaginationType: "full_numbers"
          bJQueryUI: true

Anyway, you should consider how turbolinks handles the cached page, you can find more info on the matter in this question and in this article .无论如何,您应该考虑 turbolinks 如何处理缓存页面,您可以在此问题本文中找到有关此问题的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我按下按钮时页面会刷新? - Why does my page refresh when I press buttons? 当我单击按钮时,为什么我的角度应用程序会刷新数据源中的所有行? - Why does my angular app refresh all rows from the data source when I click a button? 为什么我的页面顶部按钮在我的 API 应用程序上滚动到顶部然后返回时会卡顿/滞后/粘滞,但在网页上却正常运行? - Why does my top of page button stutter/lag/stick when I scroll to top and then back down on my API app but acts normal on webpages? 当我单击一个按钮时,页面不会在 React Javascript 中刷新 - When I click a button the page does not refresh in React Javascript 当我通过浏览器打开html页面时,为什么倒计时应用程序可以正常运行,但是当我使用http服务器时却不能正常运行? - Why does my countdown app work fine when I open the html page through the browser, but not when I use an http server? 为什么单击搜索时页面会刷新? - Why does page refresh when i click search? 为什么来自我的 api 的数据只显示一次,当我刷新页面时它会出错 - Why does data from my api only display once and when I refresh the page it gives an error 尝试刷新usestate时为什么程序会中断? - Why does my program break when I try to refresh the usestate? 当我使用jQuery增大或减小字体大小时,为什么整个页面都会刷新? - Why does the whole page refresh when I use jQuery to increase or decrease the font size? 单击浏览器后退按钮时刷新当前页面(chrome) - Refresh the current page when i click the browser back button(chrome)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM