bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Ability to set data attributes for from and to in a range #301

Closed brendon closed 2 years ago

brendon commented 2 years ago

I was wondering if it's possible to set to: and from: options when calling datagrid_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 particular data- attribute for a stimulus 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

brendon commented 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.

bogdan commented 2 years ago

There are multiple ways. If you are using builtin method datagrid_form_forpass 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) %>
brendon commented 2 years ago

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 :)

bogdan commented 2 years ago

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") %>
brendon commented 2 years ago

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 :)

bogdan commented 2 years ago

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.

brendon commented 2 years ago

Thanks for that @bogdan, that makes sense to me :) Thanks for your help!