jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

filterrific_sorting_link() duplicates the result table when filtering by date_at_gte and date_at_lt #157

Open crova opened 6 years ago

crova commented 6 years ago

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

}

scope :payed_at_gte, lambda { |reference_time| where('losses.date >= ?', reference_time)}
scope :payed_at_lt, lambda { |reference_time| where('losses.date < ?', reference_time)}

' # 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

<%= button_to 'Add New Expense', new_loss_path %>


<%= 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


<%= page_entries_info @losses %>

<%= form_for_filterrific @filterrific, html: { id: 'filterrific-no-ajax-auto-submit' } do |f| %>
Search (All fields available) <%= f.text_field( :search_query ) %> <%= f.submit 'Filter' %> | <%= link_to 'Reset', (reset_filterrific_url(format: :html)) %>
<%= render_filterrific_spinner %> <% end %>

Date range:
<%= f.label(:start_date) %> <%= f.text_field :payed_at_gte, :data => { :provide => :datepicker, :date_format => "dd-mm-yyyy" } %> <%= f.label(:end_date) %> <%= f.text_field :payed_at_lt, :data => { :provide => :datepicker, :date_format => "dd-mm-yyyy" } %>
<% @losses.each do |loss| %> <% end %>
    <%= filterrific_sorting_link(@filterrific, :date) %> <%= filterrific_sorting_link(@filterrific, :category) %> <%= filterrific_sorting_link(@filterrific, :source) %> <%= filterrific_sorting_link(@filterrific, :item) %> <%= filterrific_sorting_link(@filterrific, :loss_type) %> <%= filterrific_sorting_link(@filterrific, :status) %> <%= filterrific_sorting_link(@filterrific, :loss_total) %> Delete Expense
<%= link_to(image_tag("eyeicon.png", :size => '25x25', :alt => "details"), {:action => 'show', :id => loss.id}, :class => 'back-link') %> <%= link_to(image_tag("pencil.png", :size => '25x25', :alt => "details"), {:action => 'edit', :id => loss.id}, :class => 'back-link') %> <%= loss.date.strftime("%b %e, %Y") %> <%= loss.category %> <%= loss.source %> <%= loss.item %> <%= loss.loss_type %> <%= loss.status %> <%= loss.loss_total%> € <%= link_to 'Delete', loss, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= will_paginate losses %>

# 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)