leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 213 forks source link

Why can't we initialize the filter param `:f` like we can with everything else? (i.e. :order, :order_directions, etc) #226

Closed asgeo1 closed 9 years ago

asgeo1 commented 9 years ago

I'm trying to make all of the filtering, sorting, pagination settings be remembered in the session.

So if the user navigates to the detailed page and then returns, all of the settings for the grid are still in place. (this seems easier than passing them through multiple forms...)

This is my current attempt:

job_schedules_grid_status = session[:job_schedules_grid_status].presence || {}

if not params[:job_schedule].present?
  params[:job_schedule] = {}
end

if not params[:job_schedule][:f].present? and job_schedules_grid_status[:f].present?
  params[:job_schedule][:f] = job_schedules_grid_status[:f]
end

@job_schedules_grid = initialize_grid(
  @job_schedules,
  order: (job_schedules_grid_status['order'].presence || 'despatch_at'),
  order_direction: (job_schedules_grid_status['order_direction'].presence || 'asc'),
  per_page: (job_schedules_grid_status['per_page'].presence || 200),
  page: (job_schedules_grid_status['page'].presence || 1),
  conditions: (job_schedules_grid_status['conditions'].presence || nil),
  # why can't I do this??
  # f: (job_schedules_grid_status['f'].presence || nil),
  name: 'job_schedule'
)

session[:job_schedules_grid_status] = @job_schedules_grid.status

Notice I can't pass through the f param so I can preset the filters.

Couldn't you allow this? Everything else can be initialized for the grid except the filters.

Obviously I can work around it by just setting the params hash, but I'd rather not.

Cheers

leikind commented 9 years ago

Couldn't you allow this? Everything else can be initialized for the grid except the filters.

What do you mean? Allow what? What doesn't work in your example?

asgeo1 commented 9 years ago

@leikind - I mean that I tried to pass in the :f option to initialize_grid.

You can't do that, because it gets rejected with an error message as its an unsupported option.

But I think it should be accepted, as there doesn't seem to be any other way of preselecting the filter values. (other than populating params).

asgeo1 commented 9 years ago

Maybe :f isn't the best name for that option. That's just what gets returned though when you call the status method though, so I guess it should be consistent.

leikind commented 9 years ago

Ah, now I understand. Of course, :f is not an option. And it shouldn't be.

Look how I implemented a similar functionality. Controller:

session[:contents_grid_settings] = params[:g] 

and then in my view:

link_to 'navigation.contents'._,  contents_path(session[:contents_grid_settings])