bogdan / datagrid

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

Ajax enum/select filter #295

Closed rbdmorgan closed 1 year ago

rbdmorgan commented 3 years ago

Is it possible to wire up a dynamic enum/select filter to something like this:

https://select2.org/data-sources/ajax

Would be useful when you have a proc/query that returns a lot of results.

bogdan commented 3 years ago

IMO providing endpoints to automatically load the options out of the box is doomed to have issues with different formats. I wouldn't do it out of the box.

You can try something like this:

class MyGrid
  filter(:department, :string, input_options: {class: 'js-department-select'})
end

class DepartmentsSelectController
  def index
     render json: departments: Department.where("name like '%#{params[:q]}%'").limit(30).map {|d| {text: d.name, id: d.id}
  end
$('.js-department-select').select2({
  ajax: {
    url: 'https://api.github.com/search/repositories',
  }
});