Hello, thanks for the great module.

I noticed that the code snippet in the documentation to automatically submit the view filter is missing a crucial if statement. Here is the code as it stands:

Drupal.behaviors.bef_live_filter = function() {
    // Hide the apply button.
    $('.views-exposed-form input:submit').hide();
  
    // When the change event fires, run the submit handler
    $('.views-exposed-form input').change(function(event) {
      $(this).parents('form').submit();
    }); 
} 

The problem is that Drupal will call this behaviors function everytime there is loaded content on the site. This includes the first page load when the DOM is complete, and any module that loads in content thru AJAX. When views reloads the content of the view, this (and all over behavior functions) are called again. This results in the change handler being applied over and over again to all of the inputs, which will submit the function exponentially more and more as the user alters the filter.

The fix is to utilize the passed in 'context' argument, which indicates the context of the loaded content. In this case, we only need to execute the code once: when the complete DOM is loaded for the first time. Fixed:

Drupal.behaviors.bef_live_filter = function(context) {
  // Only execute on first document ready
  if (context.nodeName == '#document') {
    // Hide the apply button.
    $('.views-exposed-form input:submit').hide();
  
    // When the change event fires, run the submit handler
    $('.views-exposed-form input').change(function(event) {
      $(this).parents('form').submit();
    }); 
  }
}

Comments

mikeker’s picture

Status: Needs review » Closed (fixed)

You're absolutely correct. Thanks for posting the fix. I've updated the handbook page accordingly.

mandreato’s picture

I've used Better Exposed Filters in conjunction with Views Hacks - Views Filters Auto-submit module (http://drupal.org/project/views_hacks#views_filters_autosubmit).
They seem to work fine togheter: checkboxes from BEF and auto-submit from Views Hacks...

Hope this helps !

ajs17’s picture

Version: 6.x-1.0-beta4 » 6.x-1.0

I am using 6.x-1.0 with the improved code above, on a page view with taxonomy terms exposed. The auto-submit functionality works as expected when the page initially loads, but when I click on a term checkbox, and the ajax call is complete, the vanilla exposed form with the apply button is re-displayed and the auto-submit functionality disappears.

I have a feeling that a small tweak to the context in the code is the answer, but I have to admit that even after reading the excellent jQuery documentation, I am not sure what to do...

Any help is greatly appreciated.

ajs17’s picture

Its always fun to reply to your own post...

Very strangely, when I turn off ajax in the view Basic Settings (temporarily giving up), the submit button does not reappear after multiple term selections. Again, suggests a change to the context is in order...