我试图通过单击标题而不加载页面来对表列进行排序。

时间:2018-11-12 15:51:47

标签: ruby-on-rails ruby ajax

我想做的是当用户单击排序链接旁边的标题时,我希望它加载而不刷新整个页面。如何将其添加到我的代码中?

助手

module ListsHelper
  def sort_link(column, title = nil)
   title ||= column.titleize
   direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
   icon = sort_direction == "asc" ? "fas fa-chevron-up" : "fas fa-chevron-down"
   icon = column == sort_column ? icon : ""
   link_to "#{title} <i class='#{icon}'></i>".html_safe, params.merge(:sort => column, direction: direction)
 end
end

观看次数

<div class="demo">
  <br>
  <table class="table is-responsive is-fullwidth">
    <h1>Click header to sort</h1>
    <thead>
      <tr>
        <th><%= sort_link 'Name'%></th>
        <th><%= sort_link 'Date' %></th>
        <th><%= sort_link 'Number' %></th>
        <th><%= sort_link 'Description' %></th>
        <th colspan="3"></th>
      </tr>
    </thead>
    <% @lists.each do |list| %>
    <tbody>
      <tr>
        <td><%= list.name %></td>
        <td><%= list.date %></td>
        <td><%= list.number %></td>
        <td><%= list.description %></td>
        <td><%= link_to 'Show', list %></td>
        <td><%= link_to 'Edit', edit_list_path(list) %></td>
        <td><%= link_to 'Destroy', list, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>

    </tbody>
    <% end %>
  </table>

</div>

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题。

问题是我有遥控器:内部真实

link_to "#{title} <i class='#{icon}'></i>".html_safe, params.merge(:sort => column, direction: direction, remote: true)

代替

link_to "#{title} <i class='#{icon}'></i>".html_safe, params.merge(:sort => column, direction: direction), remote: true