wice_grid
wice_grid copied to clipboard
wice_grid_custom_filter_params on join model
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?
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)
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?
yes please, let it stay
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..
- Remove the [] and continue to allow only one value to be filtered
- Change the filter processor to work if the [] is passed for a text field
- Allow an array of opts to be passed. If more than one opts has the same grid_name/attribute/model, append the [] to all of them