drupal > views > exposed filter > submit on change
Asked Answered
H

5

10

I've got a view with a single exposed filter (a select). It's using ajax to re-populate when the user clicks "Apply". I'd like them not to have to click that and just re-populate when the select is changed. I'm assuming I'm going to need some JS more or less like this (though this doesn't quite seem to be working):

$('#edit-tid').change(function(){
  $('#views-exposed-form-MYVIEW-page-1').submit();
});

First, I would think that would do it, but it's not getting submitted. Anyone know why?

Second, what's the best way to inject that code? I'm thinking of using the View footer because it's easy, but any other better ideas?

UPDATE: The above code is working (injected via the views footer), but only the first time. I guess the select is getting overwritten by the ajax call, but the behavior isn't getting reattached (or something). Hmm...

UPDATE #2: For simplicity's sake, I'm gonna ditch the ajax.

Herbst answered 28/9, 2009 at 19:1 Comment(0)
E
11

In order to have this code re-attached after the ajax call, it should initially be attached via Drupal.behaviors. Something like this:

Drupal.behaviors.myCustomModule = function(context) {
  $('#edit-tid', context).change(function(){
    $('#views-exposed-form-MYVIEW-page-1').submit();
  });
}

Note that the context argument is passed into the selector. Drupal.behaviors should get called again on the new content loaded via ajax.

Update: I didn't notice you were inserting the js via the views footer. The above should still work, just replace 'myCustomModule' with some unique identifier so you don't override other behaviors.

Eckstein answered 28/9, 2009 at 19:36 Comment(1)
- can you tell me where should i write this function ?Conti
L
6

I think this feature works out of the box (at least in Drupal 7). Edit your view and under Exposed form choose

Exposed form style -> Settings

Then there is an option

Autosubmit

where you can choose if you want to "Automatically submit the form once an element is changed". There is also the possibility to use the option

Hide submit button

which is explained by "Hide submit button if javascript is enabled".

Levigate answered 19/7, 2012 at 10:32 Comment(0)
C
1

@pradeep: you could insert it into a themed view template (see http://www.group42.ca/theming_views_2_the_basics) of your view. (sorry, could not write this as comment according to the forum ui)

Cecilla answered 24/9, 2011 at 17:59 Comment(0)
B
0

If you need AJAX to work when changing a select option in your exposed view

Drupal.behaviors.myCustomModule = {
attach: function (context, settings) {
    $('select[data-drupal-selector="edit-YOUR-FIELD"]', context).change(function(){
        $('#views-exposed-form-YOUR-VIEW-block-1 input.form-submit').trigger('click');
    });
}

};

Bickering answered 30/11, 2016 at 20:46 Comment(0)
C
0

Autosubmit of Better Exposed Filters doesn't work on initial page load but it works when you change or click on options.

Cromer answered 7/11, 2019 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.