场景:有一个排序按钮,当用户点击它时,列表将按name
排序(之前按created_at
排序)。
我遇到的问题是,我在 ruby 中对代码进行了排序,但是我如何知道用户何时单击按钮?
例如:
<button id = "sort_btn">Sort by name!</button>
...
<%# before_sort is origin array %>
<% sort_arr = Array.new(before_sort) %>
<%# x[1] is the name attribute %>
<% sort_arr.sort_by!{|x| x[1]} %>
<%# if click sort button %>
<% sort_arr.each do |section_layer1| %>
<%# if did't click sort button or click twice sort button %>
<% before_sort.each do |section_layer1|>
<%= render :partial => 'list', :locals => {:section => section_layer1 }%>
<% end %>
我想我可能会使用Javascript删除原始显示并在用户单击排序按钮后再次渲染部分 View 。
最佳答案
Javascript!您可以在点击时手动添加获取参数:
$(".sort_btn").on('click',function(){
if (url.indexOf('?') > -1){
url += "&sort=sort";
}else{
url += "?sort=sort";
}
}
window.location.href = url;
});
然后,如果参数存在,则打印排序表:
<%- if params[:sort].present? %>
-- print sorted table --
<%- else %>
-- not sorted --
<% end %>
关于javascript - Rails 通过单击按钮对元素进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39118461/