bogdan / datagrid

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

Filter with multiple columns #274

Closed trselva closed 4 years ago

trselva commented 5 years ago

I am new to Datagrid. I would like to know How can I apply filter with multiple columns. Example, Filter all the rows with column Country = 'India', and then City='Bangalore' and then Employee Type = 'Contractor'. Like this I need to allow user to pass many columns with nested filter.

I can do this adding filter for each column. But I need to handle this with one filter option and applying filter for multiple columns.

Tried with Dynamic Filter. But when apply filter in Dynamic filter. the previous filter is cleared. when I apply filter for country and then again apply filter for city. The filter on country is cleared. Below is the Dynamic filter I used.

filter(:condition,:dynamic, select: [["Country", :country], ["City", :city], ["Employee Type", :emptype]], :header => "Search")

I want it to be nested filter.

bogdan commented 5 years ago

I don't think datagrid can incorporate such a complex API for dependent filters. In my project, we include all values for nested filters on backend and filter those values on frontend like this:

filter(:country_id, :integer, select: -> { ... })
filter(:city_id, :integer, select: -> { ... })

= f.datagrid_filter :country_id, class: 'js-country-select'
= f.datagrid_filter :city_id, class: 'js-city-select' do
  - City.all.each do |city|
    %option{value: city.id, data: {country_id: city.country_id}, selected: city.id == @grid.city_id}= city.name
jQuery(document).on('.js-country-select', 'change', function() {
  var countryId = $(this).val()
  $('.js-city-select option:not[data-country-id='+ countryId+ ']').
     attr('hidden', true).attr('selected', false);
   $('.js-city-select option[data-country-id='+ countryId + ']').attr('hidden', false);
});