ฉันมีแฮชด้านล่าง:
library = {"1"=>{"title"=>"bbb", "money"=>10}, "2"=>{"title"=>"aaa", "money"=>12}}
และ application_helper.rb ของฉัน:
def sortable_columns
%w[title money]
end
def sort_column
sortable_columns.include?(params[:column]) ? params[:column] : "title"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sort_link(column, title)
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
icon = sort_direction == "asc" ? "fa fa-arrow-down" : "fa fa-arrow-up"
icon = column == sort_column ? icon : ""
link_to "#{title} <i class='#{icon}'></i>".html_safe, {column: column, direction: direction}
end
ตารางของฉันใน table.html.erb:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<% sortable_columns.each do |column| %>
<th><%= sort_link column, column %></th>
<% end %>
</tr>
</thead>
<tbody>
<% library.sort_by {|k| k[1][sort_column]}.each_with_index do |data, i| %>
...
<% end %>
</tbody>
</table>
และตอนนี้ - หลังจากคลิกในส่วนหัว "ชื่อ" หรือ "เงิน" - ASC การเรียงลำดับของตาราง ฉันต้องการให้แสดง DESC เมื่อคลิกอีกครั้ง (พร้อมไอคอนลูกศรลง) สิ่งนี้สามารถทำได้อย่างไร? ฉันคิดถึง "ย้อนกลับ" หลังจาก library.sort_by {|k| k[1][sort_column]}
แต่ไม่ได้ผล
tap
ใน Ruby ขอบคุณ. :) - person Kamil Pikula   schedule 22.05.2019