Closed brendon closed 2 years ago
I should add that I have managed to just manually implement a set of range fields using text_field
but just wondered if there's a more concise way.
There are multiple ways.
If you are using builtin method datagrid_form_for
pass input_options
to filter
definition:
class MyGrid
filter(:age, :integer, input_options: {from: 18, to: 100})
end
If you are building your forms manually,, just pass option to datagrid_filter
:
<%= f.datagrid_filter(:age, from: 18, to: 100) %>
Sorry, looks like I didn't explain myself properly :D I'm wanting to set actual data-
attributes on the HTML <input>
tags. Something like data-datetime-target="fromInput"
and data-datetime-target="toInput"
so I can reference these things in my javascript stimulus controller.
Certainly I can do it manually by not using datagrid_filter
, just wondering if it's possible to do it with that helper though :)
I gave you two ways of doing that. What is the problem with them?
filter(:created_at, :datetime, input_options: {:"data-datetime-target" => "fromInput"})
<%= f.datagrid_filter(:created_at, :"data-datetime-target" => "fromInput") %>
I think the problem is that datagrid_filter
for a range
doesn't seem to allow one to set different attributes on the from
and the to
inputs. The same attributes get applied to both. I'm trying to achieve something like this:
<div data-controller="daterange">
<input class="order_date date_filter from" multiple="multiple" value="2022-06-30" data-daterange-target="fromInput" type="text" name="identities_grid[order_date][]" id="identities_grid_order_date">
<span class="separator">-</span>
<input class="order_date date_filter to" multiple="multiple" value="2022-07-30" data-daterange-target="toInput" type="text" name="identities_grid[order_date][]" id="identities_grid_order_date">
</div>
No worries if it's not possible. I was just wanting to check :)
Here is what you can do, run rake datagrid:copy_partials
and modify app/views/datagrid/_range_filter.html.erb
:
<%= form.text_field(filter.name, **from_options, **filter.options[:from_input_options]) %>
<span class="separator <%= filter.type %>"> - </span>
<%= form.text_field(filter.name, **to_options, **filter.options[:to_input_options]) %>
Now you can use those options in filter
:
filter(
:order_date, :date, range: true,
from_input_options: {:"data-daterange-target" => "fromInput"},
to_input_options: {:"data-daterange-target" => "toInput"},
)
You can delete the rest of copied partials to make sure they are updated with new versions of datagrid.
Thanks for that @bogdan, that makes sense to me :) Thanks for your help!
I was wondering if it's possible to set
to:
andfrom:
options when callingdatagrid_filter
on a range (e.g. date range) that will get applied to their respective inputs? Specifically I'd like to give each<input>
a particulardata-
attribute for astimulus
controller. I've had a good trawl through the source and I'm thinking it's probably not possible but just wondered if I've missed something as things are fairly abstracted in there :D