A nice touch would be a checkbox in the 'exposed filters' section when creating a view that allows you to select if your exposed filters should use a 'reset' button next to the submit button. It would make reseting filtered forms very easy and user friendly.

Comments

mshaver’s picture

That would be a nice addition. I modified the theme_views_filters function in my template.php file to add l('reset', $view->url). Very simple, but it works great!

Keith Adler’s picture

Could you explain just how you did that ... it's a great piece of functionality. Thank you in advance.

mshaver’s picture

Add this code to your template.php file and it should work for you. You can see, I simply added a link to the original view. This essentially resets it to the default values. There are probably better ways to do this, but it worked for my purposes.

function phptemplate_views_filters($form) {
  $view = $form['view']['#value'];

  foreach ($view->exposed_filter as $count => $expose) {
    $row[] = form_render($form["op$count"]) . form_render($form["filter$count"]);
    $label[] = $expose['label'];
  }
  $row[] = form_render($form['submit']) .l('reset', $view->url);
  $label[] = ''; // so the column count is the same.

  // make the 'q' come first
  return form_render($form['q']) . theme('table', $label, array($row)) . form_render($form);
}
Keith Adler’s picture

Thanks for much for the response; it is really appreciated. I've taken what you've done and mixed in a little from here http://drupal.org/node/40857 to come up with something I think others may find useful as well ... a reset button that looks just like the submit button; I will link to this on the Views Group.

Note: Because I'm on 5.x I had to replace form_render with drupal_render as well:

function phptemplate_views_filters($form) {
  $view = $form['view']['#value'];

  foreach ($view->exposed_filter as $count => $expose) {
    $row[] = drupal_render($form["op$count"]) . drupal_render($form["filter$count"]);
    $label[] = $expose['label'];
  }
  $row[] = drupal_render($form['submit']) .drupal_render($form['New'] = array(
  '#type' => 'markup',
  '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'class="form-submit" onClick="window.location = \'' .url($view->url) .'\';" />',
  '#weight' => 19,
));

  $label[] = ''; // so the column count is the same.

  // make the 'q' come first
  return drupal_render($form['q']) . theme('table', $label, array($row)) . drupal_render($form);
}

I simple added that to my template.php file as presto! ... a much desired reset button.

Thank you.

mrgoltra’s picture

Good Day,

I tried this but it shows 2 reset button on my exposed filters. Any ideas why?

Thank you,

Mark

Alan_C’s picture

Yes, I can confirm the bug:
I tried it in Drupal 5.2 and I see 2 reset buttons.

Someone can kindly fix it?

Many thanks

mauro72’s picture

I'm having the same problem, a workaround that I find is to add a class to the reset button, and hide it using CSS. If you look in the HTML code, you'll see that the 2 buttons have differents CSS path, one is inside the table and the other is outside of it, so you can choose which one to hide.
Anyway, I think that will be much better to have a solution through php code.

I found a solution through php code in this node http://drupal.org/node/79302#comment-624631, if you need more details let me know.

pgrunzjr’s picture

I'm still looking into it further to find out exactly whats making it happen, but it seems that anything passed to drupal_render is removed from the array or changed so that i cannot be rendered again. For whatever reason though the reset button does not get removed initially, so it is displayed not only when theming the $row array, but again when $form is passed to drupal_render(). To fix the double reset button error remove the final drupal_render($form) from the return statement. The code should be as follows:

This is old code, use the stuff below

//old code, use the new stuff
function phptemplate_views_filters($form) {
  $view = $form['view']['#value'];

  foreach ($view->exposed_filter as $count => $expose) {
    $row[] = drupal_render($form["op$count"]) . drupal_render($form["filter$count"]);
    $label[] = $expose['label'];
  }
  $row[] = drupal_render($form['submit']) .drupal_render($form['New'] = array(
  '#type' => 'markup',
  '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'class="form-submit" onClick="window.location = \'' .url($view->url) .'\';" />',
  '#weight' => 19,
));

  $label[] = ''; // so the column count is the same.

  // make the 'q' come first
  return drupal_render($form['q']) . theme('table', $label, array($row));
}

Maybe someone with a bit more knowledge of drupals API can better explain whats happening.

EDIT:

A better option may be to just move the creation of the reset button array to outside of the drupal_render() call and create it just prior. That way the $form array can still be rendered, in case it may be needed do display any other data. That and it makes the code a little easier to read =)

function phptemplate_views_filters($form) {
  $view = $form['view']['#value'];

  foreach ($view->exposed_filter as $count => $expose) {
    $row[] = drupal_render($form["op$count"]) . drupal_render($form["filter$count"]);
    $label[] = $expose['label'];
  }
  $form['reset'] = array(
  '#type' => 'markup',
  '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'class="form-submit" onClick="window.location = \'' .url($view->url) .'\';" />',
  '#weight' => 19,
  );
  $row[] = drupal_render($form['submit']) .drupal_render($form['reset']);

  $label[] = ''; // so the column count is the same.

  // make the 'q' come first
  return drupal_render($form['q']) . theme('table', $label, array($row)) . drupal_render($form);
}

I suppose you could also just send the reset button directly to drupal_render() without adding it to $form and avoid the issue all together, but either way, for those having trouble this should fix it.

merlinofchaos’s picture

Version: 6.x-2.x-dev » 5.x-1.6
Component: Code » Documentation

This would be a fantastic candidate to be put into the Views documentation section. Anybody willing to create a documentation page about this?

NeuZeitgeist’s picture

I added your code to my template.php and it worked for me.
Thank you.

I think it would be nice to add this function to stundart version of Views

sun’s picture

Title: Ability to add a 'reset' button to exposed filters. » HowTo: Add a 'reset' button to exposed filters
Category: feature » task
tian’s picture

I can't make it work with D6. I added pgrunzjr's code to the beginning of my theme's template.php, but no button :(

webchick’s picture

Here's how I did this in D6 with Views 2. Replace CAPITAL letters accordingly.

/**
 * Implementation of hook_form_alter().
 */
function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'NAME_OF_VIEW') {
      $form['reset'] = array(
        '#value' => l(t('Reset'), 'URL/OF/VIEW'),
      );
    }
  }
}

This gets pasted into a custom module someplace.

ilfelice’s picture

Subscribe

tian’s picture

Here's how I did this in D6 with Views 2.

I did the same thing (found the code in an other thread), but this creates a link, not a button :(
Better than nothing, though...

psynaptic’s picture

Version: 6.x-2.3 » 5.x-1.6

You can also use the page from the view rather than hardcoding:

/**
* Implementation of hook_form_alter().
*/
function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $exposed_views = array('NAME_OF_VIEW', 'NAME_OF_VIEW2', 'NAME_OF_VIEW3');
    if (in_array($form_state['view']->name, $exposed_views)) {
      $form['reset'] = array(
        '#value' => l(t('Reset'), $form_state['view']->display[$form_state['view']->current_display]->display_options['path']),
      );
    }
  }
}
attheshow’s picture

This works to make a "real" reset button, I believe:

<?php
/**
* Implementation of hook_form_alter().
*/
function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'NAME_OF_VIEW') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input class="form-button" type="reset" value="Reset" onClick="javascript:window.location=\'/'. $form_state['view']->display['page_1']->display_options['path'] .'\';" />',
      );
    }
  }
}
?>
alexh’s picture

All this code only puts a link to the view (disguised as a button).
This does not help at all, if exposed filters are set to "Remember the last setting" (D6 Views 2).
Any idea how to make a real, real reset button?

attheshow’s picture

Version: 5.x-1.6 » 6.x-2.3

I realized that my snippet didn't take into account the current display that was being shown. A change to the code is shown below.

<?php
/**
* Implementation of hook_form_alter().
*/
function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $exposed_views = array('My_Workspace', 'Course_Revisions', 'Courses_List');
    if (in_array($form_state['view']->name, $exposed_views)) {
      $current_display = $form_state['view']->current_display;
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input class="form-button" type="reset" value="Reset" onClick="javascript:window.location=\'/'. $form_state['view']->display[$current_display]->display_options['path'] .'\';" />',
      );
    }
  }
}
?>
my-family’s picture

Version: 5.x-1.6 » 6.x-2.3

Is the following version of code correct?
It seems to work right for me (D6), even with "Remember the last setting" option.
The assumption is, that the URL of the view does not change with the filter, which is my case.

function MY_MODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
        $form['reset'] = array(
           '#type' => 'markup',
           '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'class="form-submit" onClick="window.location = \'' .url(arg(0)) .'\';" />',
    );
    }
}
attheshow’s picture

That looks like pretty much the same thing to me, just a bit simplified.

dnewkerk’s picture

Thanks for the great code snippets! Can anyone give a little advice on how to apply this technique to get a Reset button or link in the case of an AJAX-enabled view? I tried out all of the suggestions mentioned thus far, and in the case of the AJAX view, clicking Reset instead forwards to the front page of the site rather than resetting the filters. Thanks! :)

my-family’s picture

To Keyz: I use AJAX as well and my module (#20 above) seems to work right for me (e.g., http://www.my-family.cz/?q=view-atrakce-sport-wellness)...
BUT, when I use $view->url, instead of arg(0), "reset" button goes always to frontpage... I don´t understand, why.

beginner’s picture

Version: 6.x-2.3 » 6.x-2.x-dev
Priority: Minor » Normal

This is affecting project_issue.module, too.
#426086: Add a "Reset search" button/link

dww’s picture

@merlinofchaos: Are you sure you just want to document this, instead of providing a mechanism for this in, for example, the settings for the display handler? There's already the setting about "Put the exposed form in a block". Why not another setting right next to that with "Provide reset link for exposed form"? If you said "go", I'd be happy to write such a patch. I just don't want to spend time on it if you're opposed in principle to giving views the power to provide this functionality out-of-the-box via the admin UI. Your wish is my command. ;) Thanks.

easp’s picture

I had the same problem adding a reset button that actually reset the form so that it would work with the "remember" setting.

What I ended up doing, was to set the reset button to submit "empty" and "any" values for the exposed filters.

Here is a sample of my code:

<?php
function MODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'career_scholarship_fellowship') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/current-undergraduates?title=&field_cc_academic_division_value_many_to_one=All&field_cc_location_of_study_value_many_to_one=All&field_cc_degree_level_value_many_to_one=All\';" />',
      );
    }
  }
}
?>

It may not be perfect, but it works with exposed filters with the "remember" option. It is set to work with a specific view with exposed filter: career_scholarship_fellowship. The window location is the path to the view with arguments that set the exposed filter's values. You can get the arguments by submitting your search and getting the values from the url. Use AJAX must not be set.

webchick’s picture

I agree with dww that this ought to be part of the settings. It's obviously something that people need, given the popularity of this issue, and the sheer number of various solutions floating around make it really confusing.

my-family’s picture

I would like to add one more related question to it. By exposed filters in D5, when the filter was not obligatory, the option "all" appeared in the select box. Unfortenately, there is no such option in D6. I think that it would help a lot. I put this question into this issue, because the functionality is (at least partly) similar to the reset button.

snorkers’s picture

I'd love to see dww's offer taken up. I was on the cusp of delving into code (not my favourite way to end the week), but would rather have something that could (in theory) be maintained as the complexity of Views grows.

gustavosg’s picture

Where do I have to put these codes using D6?? I tried in template.php but nothing happens. Please help me, I'm lost

my-family’s picture

@gustavosg: you should put the code into your own module. You should create only my_module.info file and my_module.module file. You will put the code into the my_module.module file.

gustavosg’s picture

@my-family: I've found this out 5 minutes before you post...heheh

But Thank you so much for you help.

tirdadc’s picture

Subscribing.

dnewkerk’s picture

For anyone who doesn't have (or yet understand) how to have their own personal module with which to use hook_form_alter, here's a version of the code that can work in template.php. Since I wasn't sure how to use the $form_state here the way I do in my module's version, I have used the path arguments to identify the correct pages to affect (if anyone can correct this to use $form_state that would be cool). In my case the paths are media/photos and media/videos. I also have adjusted the submit button's label (which you can remove if you want). As usual, do not include the PHP open/close tags, and change YOURTHEME to the correct name of your theme. Rebuild the theme registry after adding the snippet.

<?php
/**
 * Implementation of hook_theme().
 */
function YOURTHEME_theme(){
  return array(
    'views_exposed_form' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

// Rename Views "Apply" button and add a Reset option.
function YOURTHEME_views_exposed_form($form) {

  if (arg(0) == 'media' && arg(1) == 'photos') {
    $form['reset']['#value'] = l(t('Reset'), 'media/photos');
    $form['submit']['#value'] = t('Search');
  }
  if (arg(0) == 'media' && arg(1) == 'videos') {
    $form['reset']['#value'] = l(t('Reset'), 'media/videos');
    $form['submit']['#value'] = t('Search');
  }
  
  return drupal_render($form);
}
?>
Architeck’s picture

I am having trouble getting any of the snippets on this page to work. some are close but will not reload the correct view.

Here is a temporary javascript solution, based on this post.

To use this solution add this code to your views header.

Then make changes to the formReset function to clear your form values.

The solution below works to clear date field dropdowns and text search.

<script type="text/javascript" language="javascript">       
function formReset(){
// Clear all of the text inputs
$(".views-exposed-widget #edit-keys").each(function(n,element) {
  $(element).val('');
});

// Set any of the exposed filter options back to the first option
$(".views-exposed-widget select option:first-child").attr('selected', 'selected');

// Submit the cleared form to reset the page to all items visible
$("form#views-exposed-form").submit();
}
$(document).ready(function(){	
// Add a reset button to our forms
$(".views-exposed-widget #edit-submit").after('<input type="button" onclick="formReset()" value="Reset form">');
});
</script>
Vahalaman’s picture

If you don't want your page to refresh when you click the 'reset' button - this works..

function views_filter_reset_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
	$form['reset'] = array(
    	'#type' => 'markup',
        '#value' => '<input class="form-button" type="reset" value="Reset" onClick="javascript:void(0);" />',
    );
  }
}

This goes in a custom module - not the template.php file. This post will help you quickly create a custom module for this function.
http://drupal.org/node/416986

jrabeemer’s picture

Title: HowTo: Add a 'reset' button to exposed filters » Add a 'reset' button to exposed filters
Version: 6.x-2.x-dev » 6.x-3.x-dev
Component: Documentation » User interface
Category: task » feature

Agree with webchick #27, this needs to be a feature added.

Rebasing...

jenpasch’s picture

Using Attheshow #19 code, things are good. Grazie!
However, as this is a multilingual site, I need to adjust this code so that the url called when the "reset" button is pressed is language aware...
Should I be doing something in the view arguments to achieve this?
Or is there some way to force the url to be en/... when the english version page is reset and fr/... when the french page is reset?

slotid’s picture

../modules/views/theme/theme.inc

/**
 * Default theme function for all filter forms.
 */
function template_preprocess_views_exposed_form(&$vars) { ...

add

$form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input type="button" value="Reset" onclick="gotourl(\'URL\')" class="CLASS"/>');
  

before

// This includes the submit button.
  $vars['button'] = drupal_render($form);  

and add the function to your scrips.js file

function gotourl (url) {
	
   window.location = url;

} 
julianna’s picture

Subscribe

Bilmar’s picture

subscribing

merlinofchaos’s picture

Category: feature » task
Priority: Normal » Critical

Now that we have exposed plugins with better control of settings, I would like a patch that makes this an option on the base exposed filter plugin. Upping to critical as this is a Desired Feature.

rburgundy’s picture

subscribing - this would be a great feature!

dagmar’s picture

Status: Active » Needs work
StatusFileSize
new1.99 KB

Here is a patch using exposed form plugin.

This patch works only if views has Ajax disabled. Why? Because $form_state['values']['op'] is missing when ajax is enabled.

I'm marking this as "needs work" until the ajax issue be fixed.

To test this patch you need to apply also:
#645150: Call to undefined method views_plugin_display_page::get_exposed_form_plugin()
#639094: Exposed Forms do not work?

Bilmar’s picture

My testing results:

- all patching was successful
- checked 'Include reset button' under settings at Exposed form style
- tested by using exposed filters, then resetting to clear the search filters. Worked Great!
- as you mentioned above, with use AJAX set to Yes it did not work.

Thanks for the great work! I will be ready to test any future patches =)

merlinofchaos’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Status: Needs work » Patch (to be ported)

Ok. I ended up having to clean up a few things unexpectedly, so this patch is a little messy. I went ahead and committed the reset button, because it seems to be working well both with and without AJAX.

As a future patch, we should probably allow the text to be modified in the UI.

The patch is attached for porting to 7.x, and I'm sorry but some of it simply will fail to apply. Hopefully the parts that fail to apply won't matter too much.

merlinofchaos’s picture

sun’s picture

errr. Is it possible that most of this patch are different line endings?

resynthesize’s picture

Subscribing

marcushenningsen’s picture

I tested this with the 6.x-3.x-dev version. The reset filter button works, but the date filter doesn't which makes it useless in my case.

I'm not sure which of the proposed code snippets is the best to use with 6.x-2.8. Both #19 and #20 doesn't reset the filters when set to remember.

For now I'm using #19 without setting the remember option on the filters.

Marcus

merlinofchaos’s picture

Some form of this line should unremember settings during reset:

    unset($_SESSION['views'][$this->view->name][$this->view->current_display]);

($this->view might have to be just $view or something like that)

merlinofchaos’s picture

sun: Yes it is possible, admin.inc incorrectly had windows line endings. I corrected this in 6.x-3.x. It may well still need to be corrected in 2.x and 7.x-3.x

Bilmar’s picture

I noticed an issue with the reset button with 6.x-3.x-dev and thought to mention it here first and open a new issue if required.

With AJAX set to YES, the search result does reset but the exposed filter selected choices do not reset.
The exposed filters do not have 'Remember' checked.

With AJAX set to NO, everything works great!

merlinofchaos’s picture

Odd, I tested that. I shall have to have another look.

Anonymous’s picture

(cut)
see next post

Anonymous’s picture

I needed a real AJAX 'clear form & submit' button that worked on every form, with remember-me capability.

Add this to your custom module:

function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
        $form['reset'] = array(
           '#type' => 'markup',
           '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'onclick="javascript:$(this.form).clearForm();$(this.form).submit();" class="form-submit" />',
    );
    }
}

That's it!

See a live example for my client: http://www.mastersranking.com/pages/masters-finance.html

dawehner’s picture

Status: Patch (to be ported) » Fixed

year and fixed. Works fine in preview

marcushenningsen’s picture

Does this work for 6.x too?

dawehner’s picture

Its commited to the DRUPAL-6--3 branch, which is not stable yet.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nodecode’s picture

I hate to be a pain in the butt, but could this be committed to 6.x-2.x-dev?

I say this because this issue is 3 years old now and i have no idea when Views 6-3 will be stable. Maybe someone can enlighten me.

Nevermind, I dont care anymore. See post #63.

bkyan’s picture

Instead of having to update server code, I just stuck the following javascript code into my view's footer
to insert a reset button right after the Apply button. If you use this snippet, please be sure to change
NAMEOFTHISVIEW to the actual name of your view.

$(document).ready(function(){ $("").insertAfter("#edit-submit-NAMEOFTHISVIEW"); });
nodecode’s picture

I actually resolved this by creating my own module using a combination of post #19 and #36. Neat! thanks to all those experienced Drupal users who contributed to this thread. I gained a larger understanding of how Drupal works with this simple module creation.

daniel wentsch’s picture

Thanks a lot @morningtime. Your code (#56) worked very well for me.
All other suggestions I tried brought me no luck.

aac’s picture

Subscribing!!

roball’s picture

Priority: Critical » Normal

@nodecode: could you please post your module for Views 6.x-2.x mentioned in #63?

nodecode’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new1 KB

Ok, since users keep asking me about this... Here is the module. Please note, YOU MUST EDIT THE MODULE to work with your specific setup (see below).

Instructions:
1. Unzip the folder to your local computer
2. On line 7 of the file "views_filters_reset_button.module" you should see the following code:
$exposed_views = array('view_1', 'view_2', 'view_3');
3. Replace any of the values in the array (eg: 'view_1') with the names of the views where you want to see a "reset button"
4. Save the edited file "views_filters_reset_button.module"
5. Upload the views_filters_reset_button" folder in your contributed modules folder (where you put all your other modules).
6. Now on the Modules page of your website you should see an option titled "CUSTOM Views Filters Reset Button" under the "Views" category. Check the checkbox by this option and click "Save Configuration"

This is just a temporary solution i created for myself until we get this integrated properly into Views. It works with Views 6.x-2.8 and i dont see why it couldn't work with 6.x-2.10.

@dereine: Which solution did you commit to the 6.x-3.x branch? Does it reset the entire page like this module or just the filter values? Has it been thoroughly tested by users? I ask because I don't see one clear solution above that has reportedly worked for more than a few people on this thread. Just checking to make sure a good solution is implemented.

If it helps, the array in this module could be populated from a query that pulls exposed views that are somehow marked to include a "reset button" from within in the Views UI. I don't know how to do this but it seems fairly straightforward.

nodecode’s picture

Status: Needs review » Closed (fixed)

whoops, didn't mean to change the status.

roball’s picture

Thank you nodecode. The version you posted did not work on the site I was going to implement it (where Drupal is installed in a sub directory). Thus I fixed that, enhanced it further and uploaded it to #781480: Module for adding a reset button to forms created by Views 2.x with exposed filters.

sharplesa’s picture

Adding to comment #56, to also reset date fields, I added one more thing to the onclick. My onclick looks like this:

'onclick="javascript:$(\'[name^=date_filter]\').val(\'\');$(this.form).clearForm();$(this.form).submit();" '

Note that this'll reset all the date filter fields on the page, not just the ones in this form. There's probably a narrower selector I could use. In my case, I only have one date filter on the page, so this works just fine for me.

grahamvalue’s picture

Hi,

(To #56)
This code works great but only if I change clearForm() to reset()

Is clearForm() a standard js function?

Sorry, I'm quite new to Drupal.

reset only resets the forms to the remembered values.
I would ideally like a button that clears all the filters altogether

Any idea?

Thanks! :)

grahamvalue’s picture

Ok, didn't have AJAX enabled on the view.
Code works like a charm. Thanks! :)

roball’s picture

lhernon’s picture

Thank you so much. This is such a handy feature.

Laura

ari-meetai’s picture

#56 is the way to go for the time being. Cheers!

druvision’s picture

The 'views_filters_reset' module of the Views Hacks project does it for drupal 6.

roball’s picture

Yes, the "Views Filters Reset" module that is now part of the Views Hacks package does the job well for me.

stuart.crouch’s picture

I created a views-exposed-form.tpl to do this in the theme (all the others seem to cover creating or installing extra modules).

Its basically a copy of the default one from views, with a jquery clearForm function added, and a reset button that calls the clearForm function and then submits the form.

<?php if (!empty($q)): ?>
  <?php
    // This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
    print $q;
  ?>
<?php endif; ?>
<div class="views-exposed-form">
<script type="text/javascript">
$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
    {
     if (type == 'select-one')
	this.selectedIndex = 0;
     else if (type == 'select-multiple')
        for (i = 0; i < this.options.length; i++)
        {
	  this.options[i].selected = false;
        }
    }
  });
};
</script>
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <div class="views-exposed-widget">
        <?php if (!empty($widget->label)): ?>
          <label for="<?php print $widget->id; ?>">
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
    <?php endforeach; ?>
    <div class="views-exposed-widget">
      <?php print $button ?>
      <input type="button" value="<?php print t('Reset') ?>" onclick="javascript:$(this.form).clearForm();$(this.form).submit();" />
      
    </div>
  </div>
</div>
koppie’s picture

I've found the easiest solution is to stick in a custom header on the view, with a simple html link back to the view. That resets the page for me.

tanma’s picture

Just wondering if this can't also be committed to the DRUPAL-6--2 branch?

Not sure how it's meant to work but 6.3 branch seems a long time coming.

marleo’s picture

stuart.crouch: Thanks. Works a treat.

tanma: Agree. 6.2 would be great.

msathesh’s picture

#36 solution works for me.. its was easy though.. Thanks @Vahalaman ..

geek-merlin’s picture

for all thet first cant find it like me:

i can get a reset button on 7.x-3.x-dev by activating it in exposed form style > settings

freestone’s picture

Using Views 6.x-2.12 the only thing for me that worked is #26

Thanks easp

I have 5 views that need the reset button so I simple repeated the if test .....changing the view name and the URL that reset the view to "all" value of all exposed filters. It worked for both text fields and for exposed Taxonomy with depth.

After 4 hours of trying these option this works in V2 for me with HS and lots of other modules

My custom module is below....notice that for each repeated IF the name of the view is tested on and the window.location value changes to the URL of a search done with all the text fields blank and the taxonomy field set to "any".

Hope this helps other..... easp you rock!

// $Id$

function views_reset_filters_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'classified_terms_list') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/classifieds/list?term_node_tid_depth=Any&field_isbn_value=&title=&body=&apply_filter=yes\';" />',
      );
    }
  }
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'classified_roommates_list') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/roommate/list?term_node_tid_depth=Any&body=&field_age_value=&apply_filter=yes\';" />',
      );
    }
  }
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'classified_sublet_list') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/sublet/list?term_node_tid_depth=Any&body=&field_bathrooms_value=&field_bedrooms_value=&field_rent_value=&apply_filter=yes\';" />',
      );
    }
  }
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'classified_tutor_list') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/tutor/list?term_node_tid_depth=Any&body=&field_whatitutor_value=&apply_filter=yes\';" />',
      );
    }
  }
  if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'classified_stuff_list') {
      $form['reset'] = array(
        '#type' => 'markup',
        '#value' => '<input  type="button" value="Reset" class="form-submit" onClick="window.location = \'/stuff/list?term_node_tid_depth=Any&title=&body=&apply_filter=yes\';" />',
      );
    }
  }
}

agileadam’s picture

#56 worked great for me, and seems to be completely compatible with Better Exposed Filters.

clsturgeon’s picture

I tried #56. On two sites (one web hosted, the other local on my machine [using WebMatrix]) and they both work fine. On a third, same web host, different instance (same theme and modules) does not work. With Ajax set to yes on the view the page redirects to homepage. Page code looks like this...

$view = views_get_view('People','default');
$view->override_path = $_GET['q'];
$viewsoutput = $view->preview();
print $viewsoutput;

...and Reset button fails with... Object has no method 'clearForm'.

When Ajax setting is no... Apply button works properly (no redirection) and Reset is no change, still get the error "Object has no method 'clearForm'.

While editing the View, in the Preview, Apply and Reset buttons work as they should no matter what the AJAX setting is.

Any ideas?

Update: The error I see in Chrome dev tools is "Uncaught TypeError: Object #

has no method 'clearForm'". Thanks in advance.
dawehner’s picture

Version: 7.x-3.x-dev » 6.x-2.x-dev
Component: User interface » exposed filters
Assigned: Unassigned » clsturgeon
Category: task » support
Status: Closed (fixed) » Active

I'm sorry but the reset button is not part of views2. You shoudl also paste the full javascript error as context helps always.

ac’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
Assigned: clsturgeon » Unassigned
Status: Active » Closed (fixed)

Please don't open fixed issues, tag them and then assign them to yourself. If you have a support request then open a new issue instead of usurping this one. This feature is part of 7.x and I don't think it will ever be ported to 6.x and released.

maedi’s picture

+1 for #56 as a quick fix.

Mark Vincent Verallo’s picture

Better Exposed Filters is all you need for this. Just install it and select Better Exposed Filters from the Exposed form style section. Then check the "Include reset button" checkbox.

aakanksha’s picture

It works perfectly... :)

hectorelgomez’s picture

Button exposed reset view Drupal dont works:
Perfect, but in plain html format is more easy:

Thanks, it works.

______________

Botón exposed reset view Drupal no funciona:
Muchas gracias, pero es más sencillo de esta otra manera si estás acostumbrado al html plano.
Tuve serios problemas con el botón de reset en las views de Drupal, al final cambiar los atributos por uno en HTML creado con este fin. Te ahorras recargar la página, pues al usuario suele no gustarle para una acción tan sencilla. Además luego le puedes poner los estilos que te parezca.

../modules/views/theme/theme.inc:

/**
* Default theme function for all filter forms.
*/
function template_preprocess_views_exposed_form(&$vars) { ...



  if (isset($form['reset'])) {
--    $vars['reset_button'] = drupal_render($form['reset']);
++    $vars['reset_button'] = '<input type="reset" value="'.t('Clean.').'" id="custom_views_clear_button" class="form-submit" >';
  }

  // This includes the submit button.
  $vars['button'] = drupal_render_children($form);

Saludos y gracias de nuevo

anybody’s picture

Issue summary: View changes

Here you can find a solution that replaces the default reset functionality by an AJAX version: http://julian.pustkuchen.com/node/659 I hope it helps some of you.

summit’s picture

Hi,
better exposed filters module has a reset button.
greetings, Martijn