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

wice_grid_custom_filter_params on join model #184

Open rongworks opened 10 years ago

rongworks commented 10 years ago

Hi, I have a problem with HTML link generation for filters (from http://wicegrid.herokuapp.com/custom_filter_params). There are 2 models Card, Customer with a " Customer - 1:n - Card " relation. In my cards_grid I define a column for the customer reference:

g.column name: Customer.human_attribute_name(:customer_reference), attribute: 'customer_reference', model: 'Customer'  do |card|
    link_to card.customer.customer_reference, card.customer
end

In my Customer-View i'm using wice_grid_custom_filter_params:

= link_to 'cards', cards_path(wice_grid_custom_filter_params( |
    grid_name: 'cards', |
    attribute: 'customer_reference', |
    model: 'Customer', |
    value: @customer.customer_reference  ) ) |
  %p

When using these params for normal attributes (attributes of Card model) I get a filtered grid, like expected. But the link above creates the following url: http://localhost:3000/cards?cards[f][customers.customer_reference][]=XXX which doesn't filter anything. When I change the url manually to http://localhost:3000/cards?cards[f][customers.customer_reference]=XXX deleting the last bracket pair, I get a filtered grid.

I can get it to work by following these examples http://wicegrid.herokuapp.com/custom_filters2 and using the customer_id attribute and a custom_filter, but I don't want to use dropdowns for filtering the customer_reference but a text field.

I'm stuck now.. is there anything wrong with the way I'm declaring the filter_params?

leikind commented 10 years ago

I don't think you are declaring it incorrectly, could be a bug.

While this helper is not working properly why don't you just add the parameter to your link helper manually?


cards_path('cards[f][customers.customer_reference]' => @customer.customer_reference)
rongworks commented 10 years ago

You are right. The most obvious solution was out of my sight, too focused on getting this part to work. Thanks for pointing it out. Should the issue stay open as a potential bug report?

leikind commented 10 years ago

yes please, let it stay

joelw commented 8 years ago

I just ran across this issue as well. The wice_grid_custom_filter_params method returns the following:

  { "#{options[:grid_name]}[f][#{attr_name}][]" => options[:value] }

The final [] is hard-coded. This works fine for custom drop-down filter columns, but not for free-text fields. It looks like you only need [] if you are passing multiple filter values to a single attribute, and the method doesn't currently let you pass multiple parameters.

A couple of suggestions, based on my admittedly superficial understanding of how the grid works..