0

I have a simple table with 3 columns like so

table
  thead
    th Element
    th Owner
    th Progress
  tbody
    == render :partial => "element_row" :collection => @elements :as => table_element

Each element is unique, and one owner may have several elements. There are a few different types of "progress" e.g. "started", "not started", and "completed".

I want to create a few links that filter the table. For example, I want to create an started link where when the user clicks on the link, the table is filtered down to only show rows where "started" is displayed. Another example of a filter is by owner.

Does anyone have any suggestions for how to do this?

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123
jamesfzhang
  • 4,403
  • 8
  • 32
  • 43

2 Answers2

2

If you're looking for a gem to help you, Ransack is a great, popular, and active project for searching (and by extension, filtering) ActiveRecord data.

If you check out Ernie's demo site you can see how the search parameters modify the URL through GET queries. You could easily create links like your desired started link to mock these GET form requests.

deefour
  • 34,974
  • 7
  • 97
  • 90
0

If you want to do this on the server side, add a filter method to the controller that uses link parameters to filter the @elements.

If you selecting @elements from the database using ActiveRecord, you could do:

@elements = Element.where(progress: params[:progress])

If you just want to filter the @elements in memory, you could do:

@elements = @elements.select{ |element| element.progress == params[:progress] }
Jimmy Baker
  • 3,215
  • 1
  • 24
  • 26