我在排序列时遇到问题。我有一个填充了数据的表,它使用DESC
进行组织。我想这样做,以便在点击其中一列时,它会在ASC
和DESC
之间来回切换。
当我点击特定列时,这些参数正在发送:
Parameters: {"search"=>{"meta_sort"=>"date.asc"}}
以及与最初访问时进行相同的查询。
这是我的控制器代码:
def dashboard
@search = Quality.search(params[:search])
@qualities = @search.page params[:page]
@qualities_failed = Quality.where('last_disposition_state = ?','fail').order("strftime('%Y',date) DESC, julian_date DESC, lot DESC, time DESC").limit(10).search(params[:search])
@qualities_passed = Quality.where('last_disposition_state = ?','pass').order("strftime('%Y',date) DESC, julian_date DESC, lot DESC, time DESC").limit(10).search(params[:search])
respond_to do |format|
format.html
format.js
format.xlsx {
send_data Quality.order("strftime('%Y',date) DESC, julian_date DESC, lot DESC, time DESC").limit(10).to_xlsx.to_stream.read, :filename => 'passed.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet"
}
end
end
我想知道如何获取这些参数,并且据我所知,制作一个if
语句,用于评估参数并根据该参数对表进行排序。
if -> == ASC then (query with ASC instead of DESC)
答案 0 :(得分:1)
您希望在Rails或浏览器中进行排序吗?
由于浏览器已经拥有数据,因此可以在页面中的JavaScript中进行排序。用户的体验将更好/更快,并且您不会为其他请求增加Rails和托管硬件和数据库的负担。
jQuery和其他JavaScript库有很多JavaScript插件,可以很容易地实现这一点。