First of all, thanks for the awesome gem. It has been of great help on sorting/filtering my data inside my application.
I'm having difficulties when trying to sort the table with filterrific_sorting_link(@filterrific, :column_name). It works flawlessly when a search is made with a string, however, when I try sorting the records by a column after filtering between dates, the record table gets duplicated, with the second one being non-functional (as in, the sorting does not apply to the duplicated table).
The sorting works for the first table of records, but the view is messed up.
Question is, have you ever saw this behavior and do you know a fix?
Obs: Sorry for the bad formatting, this is my first Issue ever and I'm still learning how to make them.
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.tos
when /^date/
order("losses.date #{ direction }")
when /^category/
order("losses.category #{ direction }")
when /^source/
order("losses.source #{ direction }")
when /^item_/
order("losses.item #{ direction }")
when /^losstype/
order("losses.losstype #{ direction }")
when /^status/
order("losses.status #{ direction }")
when /^price/
order("losses.price #{ direction }")
when /^tax/
order("losses.tax #{ direction }")
when /^totalttc/
order("losses.totalttc #{ direction }")
when /^discount/
order("losses.discount #{ direction }")
when /^total_/
order("losses.loss_total #{ direction }")
when /^invoicenumber/
order("losses.invoice_number #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
'# This scope was used for 'single' word search | Being kept here for reference
'# scope :search_query, lambda { |query|
'# return nil if query.blank?
'# terms = query.downcase.split(/\s+/)
'# terms = terms.map { |e|
'# (e.gsub('', '%') + '%').gsub(/%+/, '%')
' # }
' # num_or_conds = 5
' # where(
'# terms.map { |term|
' # "(LOWER(losses.category) LIKE ? OR LOWER(losses.source) LIKE ? OR LOWER(losses.item) LIKE ? OR LOWER(losses.loss_type) LIKE ? OR LOWER(losses.status) LIKE ? )"
'# }.join(' AND '),
'# terms.map { |e| [e] * num_or_conds }.flatten
' # )
'# }
# Example image of the view on a browser after filtering by date range and trying to sort the table.
![image](https://user-images.githubusercontent.com/13876051/35195074-366d2c98-febe-11e7-8847-af2c9ad86659.png)
First of all, thanks for the awesome gem. It has been of great help on sorting/filtering my data inside my application.
I'm having difficulties when trying to sort the table with filterrific_sorting_link(@filterrific, :column_name). It works flawlessly when a search is made with a string, however, when I try sorting the records by a column after filtering between dates, the record table gets duplicated, with the second one being non-functional (as in, the sorting does not apply to the duplicated table).
The sorting works for the first table of records, but the view is messed up. Question is, have you ever saw this behavior and do you know a fix?
Obs: Sorry for the bad formatting, this is my first Issue ever and I'm still learning how to make them.
My loss model
class Loss < ApplicationRecord include PgSearch
scopes
filterrific( default_filter_params: { sorted_by: 'date_desc' }, available_filters: [ :search_query, :sorted_by, :search_for, :payed_at_gte, :payed_at_lt ] )
scope :sorted_by, lambda { |sort_option| direction = (sort_option =~ /desc$/) ? 'desc' : 'asc' case sort_option.tos when /^date/ order("losses.date #{ direction }") when /^category/ order("losses.category #{ direction }") when /^source/ order("losses.source #{ direction }") when /^item_/ order("losses.item #{ direction }") when /^losstype/ order("losses.losstype #{ direction }") when /^status/ order("losses.status #{ direction }") when /^price/ order("losses.price #{ direction }") when /^tax/ order("losses.tax #{ direction }") when /^totalttc/ order("losses.totalttc #{ direction }") when /^discount/ order("losses.discount #{ direction }") when /^total_/ order("losses.loss_total #{ direction }") when /^invoicenumber/ order("losses.invoice_number #{ direction }") else raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }") end }
'# This scope was used for 'single' word search | Being kept here for reference '# scope :search_query, lambda { |query| '# return nil if query.blank? '# terms = query.downcase.split(/\s+/) '# terms = terms.map { |e| '# (e.gsub('', '%') + '%').gsub(/%+/, '%') ' # } ' # num_or_conds = 5 ' # where( '# terms.map { |term| ' # "(LOWER(losses.category) LIKE ? OR LOWER(losses.source) LIKE ? OR LOWER(losses.item) LIKE ? OR LOWER(losses.loss_type) LIKE ? OR LOWER(losses.status) LIKE ? )" '# }.join(' AND '), '# terms.map { |e| [e] * num_or_conds }.flatten ' # ) '# }
scope :search_query, lambda { |query| search_by_keywords(query) }
pg_search_scope :search_by_keywords, against: %i[ category source item loss_type status ], using: { tsearch: { prefix: true },
:ignoring => :accents
' # Enf od Ruby Class end
My loss controller
def index @filterrific = initialize_filterrific( Loss, params[:filterrific], :persistence_id => false, ) or return @losses = @filterrific.find.paginate(page: params[:page], per_page: 10).page(params[:page]) respond_to do |format| format.html format.js end
End of Index
end
My index.html.erb view
<%= notice %>
Expenses
<%= render( partial: 'losses/list', locals: { losses: @losses } )%>
My index.js.erb
<% js = escape_javascript( render(partial: 'losses/list', locals: { losses: @losses}) ) %> $("#filterrific_results").html("<%= js %>");
My _list.html.erb
# Example image of the view on a browser after filtering by date range and trying to sort the table. ![image](https://user-images.githubusercontent.com/13876051/35195074-366d2c98-febe-11e7-8847-af2c9ad86659.png)