简体   繁体   中英

Sortable table columns based on hash & key array

I would like to create sortable columns for my table which tabulates data based on hashes. By following http://railscasts.com/episodes/228-sortable-table-columns?view=asciicast . From what I understand, order method would only work sorting out a Model. What's the best way of sorting out columns for such tables. I have 29 similar tables as mentioned. My codes are as below :-

admins_controller.rb

   class AdminsController < ApplicationController

     array =[]
     User.each do |user|
        company_name = Company.find(user.company_id).name
        array.push(company_name)
     end
     company = array.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }
   end

Results of the above the above query would look like this :-

 array = [["3M", 4], ["A.P. Møller-Mærsk Group", 10], ["ABB", 14], ["Abbott Laboratories", 12]]

On views :-

admins.html.erb

    <table class="table">
    <tr>
      <th><%= link_to "Company", remote: true, :sort => "hash" %></th>
      <th><%= link_to "Value", remote: true, :sort => "key" %></th>
      <th><%= link_to "Percentage", remote: true, :sort => "Percentage"  %></th>
    </tr>
    <% if @mentors.try(:any?) %>
        <% @mentors.each do |key, value| %>
          <tr>
            <td><%= key %></td>
            <td><%= value  %> </td>
            <td><%= ((value.to_f/@total_co.to_f).to_f * 100 ).round(2)%> </td>
          </tr>
        <% end %>
    <% else %>
        <td> nil </td>
        <td> nil </td>
        <td> nil </td>
    <% end %> 
    </table>

You could go for a JavaScript / jQuery solution like http://tablesorter.com/docs/

This would allow for sorting in frontend and you would not need to adjust the backend.

If you want to have a backend solution, you could go for sorting by column index. A simple solution would be something like this:

Controller:

class AdminsController < ApplicationController
  def index
    array =[]
    User.each do |user|
      company_name = Company.find(user.company_id).name
      array.push(company_name)
    end

    company = array.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }

    sort_column = params.fetch(:sort, 0).to_i
    company.sort! { |a, b| a[sort_column] <=> b[sort_column] }
  end
end

View:

<table class="table">
  <tr>
    <th><%= link_to "Company", remote: true, :sort => 0 %></th>
    <th><%= link_to "Value", remote: true, :sort => 1 %></th>
    <th><%= link_to "Percentage", remote: true, :sort => 1  %></th>
  </tr>
  <% if @mentors.try(:any?) %>
  <% @mentors.each do |key, value| %>
  <tr>
    <td><%= key %></td>
    <td><%= value  %> </td>
    <td><%= ((value.to_f/@total_co.to_f).to_f * 100 ).round(2)%> </td>
  </tr>
  <% end %>
  <% else %>
  <td> nil </td>
  <td> nil </td>
  <td> nil </td>
  <% end %>
</table>

For reverting sort order, you would need to also pass a direction state and probably reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM