jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

Version >= 5.2.4 don't work with Select2 #218

Open djpremier opened 1 year ago

djpremier commented 1 year ago

After change of jQuery to Vanilla JS, the change in fields that use Select2 don't trigger filteriffic search.

0llirocks commented 1 year ago

Could be related to https://github.com/jhund/filterrific/issues/220? Try adding an empty div with a .filterrific_spinner class.

GearoidDC commented 8 months ago

I added this to my application.js and it fixed it. If you are using select2 you have jquery anyway so it shouldn't be a problem using jquery here. It is just replacing with what the code was before in filterrific.

I'm going to try figure out what the issue is.

Filterrific.init = function() {
  $('#filterrific_filter').on(
    "change",
    ":input:not(.filterrific-periodically-observed)",
    Filterrific.submitFilterForm
  );

  Filterrific.observe_field(
    ".filterrific-periodically-observed",
    0.5,
    Filterrific.submitFilterForm
  );
};

jQuery(document).ready(Filterrific.init);
jQuery(document).on('page:load', Filterrific.init);

I also have to replace the submitFilterForm method.

Filterrific.submitFilterForm = function(){
  var form = $(this).parents("form"),
      url = form.attr("action");
  // turn on spinner
  $('.filterrific_spinner').show();
  // Submit ajax request
  $.ajax({
    url: url,
    data: form.serialize(),
    type: 'GET',
    dataType: 'script'
  }).done(function( msg ) {
    $('.filterrific_spinner').hide();
  });
};
JosephTico commented 1 month ago

This is mostly a select2 issue. This is a very old library whose developers decided for some dumb reason that it won't trigger the standard change event when the select, well, changes.

Here's more info and a workaround: https://stackoverflow.com/a/77871613/1102840