6

I am trying to use the Views module for an auto-submit field, to get the names from my taxonomies. How can I get the field to show the results after the users type something into the input box, instead of showing all the results when nothing has been entered as value for the filter?

I have tried a number of contextual filters but no luck.

This what the current search looks like, if I type in one of the terms or the field name it matches up to what I type which is how i wanted it to work, the problem is the the results are showing by default before I type anything. enter image description here

  • do you mean an auto-complete field? – Chaulky Aug 6 '12 at 4:21
  • no i mean no results until the person starts typing in the letters to match the taxonomies, pretty much exactly like how this search is google.com/finance?tab=we when you type in goog it starts matching the company name – Jeremy Love Aug 6 '12 at 8:26
  • Are you looking for how to solve it using an existing module, or are you happy also with understanding how to solve this with your own module? – kiamlaluno Aug 6 '12 at 18:24
  • Either way is preferred I just want to solve it lol – Jeremy Love Aug 7 '12 at 0:03
15

This is built into views 3:

  1. Edit your view
  2. Set Advanced | Exposed Form | Exposed Form Style to input required
  • Whoo hoo! this is the right answer. The other answer is fraught with difficulties and work-arounds, this one is the correct, clean way to do things IMO. – artfulrobot Sep 16 '14 at 9:25
  • I agree, this is the correct answer. – user1015214 Mar 30 '15 at 15:55
  • This is THE ANSWER!!! – Sorin Sep 9 '15 at 10:51
13
+100

If you set the exposed filter to "Required" with nothing in the default 'Value' box, then the view shows nothing until a user fills in and uses it.

Tip: If you find that your exposed filter doesn't show up when it is empty, then (after checking 'Use AJAX' is selected if it's a block) add something to the view's "No results behavior" field on the right, such as a Global: Text area containing a space or  .


Another option is setting a default value that doesn't match anything - for example, if the default value is a friendly message like "Enter team name here" or "Search teams", or something neutral like "0" or "–––", there will be no matches until they enter their input, so the view will be blank until they enter input. This does mean there will be one unneccessary extra query on the page - but this should usually be cached.

If you use the default, for improved usability, you can do what the "Search finance" text in the Google page you link to does, and save your users the inconvenience of deleting that default entry themselves with some jquery code that clears the field when it gains focus, and for even better usability, you can style the default text to be lightly greyed out, then remove this style when the field gains focus.

There are lots of guides out there on how to do this, I won't duplicate them here. Here's one example. You could drop code like that into your theme's scripts.js file (using Drupal.behaviors instead of $(document).ready so it still works when elements get updated by AJAX). There might be a way to do this in Drupal php on the server side or in configuration too, but I don't know of one off the top of my head.

You can also get the best of both options, avoiding that extra query and only showing the default "Search..." text to people who have Javascript enabled (who it won't be a nuisance to), by ticking the "Required" checkbox, leaving Value blank, and using something like $('#whatever-the-input-field-is').attr('value', 'Search teams'); in addition to code like the above to write in the friendly disappearing message on page load, only for people with javascript is enabled, without causing a query that will find nothing.


If I misunderstood your question, and it's the autocomplete feature you're asking about, then check out autocomplete_deluxe

  • 2
    “If you set the exposed filter to "Required" with nothing in the default 'Value' box, then the view shows nothing until a user fills in and uses it.” — this what @JeremyLove needs I guess :) – kalabro Aug 9 '12 at 8:52
  • I tried what you both suggested about the required field and the input field disappeared... – Jeremy Love Aug 14 '12 at 16:56
  • @JeremyLove See the last but one paragraph of my answer: "Tip: If you find that your exposed filter doesn't show up when it is empty, then (after checking 'Use AJAX' is selected if it's a block) add something to the view's "No results behavior" field on the right, such as a Global: Text area containing a space or   " – user568458 Aug 14 '12 at 17:30
  • @JeremyLove did you try adding the "No results behaviour" as suggested? – user568458 Aug 14 '12 at 20:48
  • Yeah I did try that the whole view still disappeared, with your suggestions I will look into this further to see what I can figure out. Also im using view populate filters module, im wondering if that has something to do with it – Jeremy Love Aug 15 '12 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.