Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
crova opened this issue Jan 21, 2018 · 0 comments

Comments

@crova
Copy link

crova commented Jan 21, 2018

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.to_s
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 /^loss_type_/
order("losses.loss_type #{ direction }")
when /^status_/
order("losses.status #{ direction }")
when /^price_/
order("losses.price #{ direction }")
when /^tax_/
order("losses.tax #{ direction }")
when /^total_ttc_/
order("losses.total_ttc #{ direction }")
when /^discount_/
order("losses.discount #{ direction }")
when /^total_/
order("losses.loss_total #{ direction }")
when /^invoice_number_/
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

@crova crova changed the title silterrific_sorting_link() duplicates the result table when filtering by date_at_gte and date_at_lt filterrific_sorting_link() duplicates the result table when filtering by date_at_gte and date_at_lt Jan 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant