views:

50

answers:

2

I'd like to give a "No matches" message on a jquery autocomplete when the result is empty.

A partial answer was given here, but it doesn't explain exactly how to show the "No matches" result in the autocomplete dropdown.

UPDATE: Here is my code based on the current answers..

HTML:

<input type="text" name="myAutocomplete" id="myAutocomplete">

jQuery:

$("#myAutocomplete").autocomplete("ajax.php", {
    selectFirst: false,
    resultsClass: 'my_results_class',
    parse: function(data) {
        if (!data || data.length == 0) {
            $('#myAutocomplete option').val('No Matches');
        }
    },
    focus: function(event, ui) {
        if($(ui.item).val() == 'No Matches')
            $(ui.item).disable();
    },
    select: function(event, ui){
        if($(ui.item).val() == 'No Matches')
            return false;
    }

});
+1  A: 

First of all, return a result with the value 'No matches' or whatever you want,then on the jquery autocompleter add these handlers

focus: function(event, ui){ 
    if($(ui.item).val() == 'No Matches')
        $(ui.item).disable();
},
select: function(event, ui){
    if($(ui.item).val() == 'No Matches')
        return false;
},

This should prevent the user from selecting the entry 'No Matches'.

Aldo
Aldo, is this correct?http://pastebin.com/VUcW8MRgIt's not showing the "No matches" text
babonk
Check if your autocomplete has an id of 'dropdown_id', if it doesn't either give it that id, or change it in the code
Aldo
Why call it "dropdown_id"?
babonk
Oh, i see.. you're referencing the other answer.. The id is indeed matching
babonk
The snippet you posted kind of mixed the two answers, that's why I said so
Aldo
I updated the question to include my current HTML + jQuery.. What change should I make?
babonk
I don't know why you're messing with it so much. Remove the `parse:` from the autocomplete options, and on ajax.php check if there are no results add one, with the label 'No Matches' And it'll work like a charm.
Aldo
Aldo, good advice.. I'm going to do that.. at this point I'm just curious as to how I would do it with parse though
babonk
Aldo, the focus and select aren't being set off. I put alerts inside them to verify this. Basically, neither is preventing 'No Matches' from going into the text input. Writing these events out in the form $("#myAutoComplete").focus... makes focus set off, when you click in the text, but aside from that neither event is set off and it's still not working at stopping 'No Matches' from going in
babonk
Here's my implementation (http://pastebin.com/sEwxweCb).In that code I disallow selecting a hr tag which is on the suggestion list. just replace <hr> with 'No Matches' and it should work
Aldo
Aldo, it's not even calling the focus or select events when they use it.. I put alerts there and they never go off
babonk
Now I feel terribly ashamed! I didnt even check the link you provided I just assumed it as jquery UI Autocomplete. The above example works only for that library. Sorry.
Aldo
+1  A: 

From the previous answer, you need to add this code between:

 if (!data || data.length == 0) {
         // handle no data case specially
 }

Making it:

 if (!data || data.length == 0) {
   $('#dropdown_id option').val('No Matches');
 }

Update:

Instead of:

$('#myAutocomplete option').val('No Matches');

Use:

$('#myAutocomplete').val('No Matches');

Update:

Use:

$('#school-term').val('No Matches');
Sarfraz
Hey, I'm not getting the "No matches" text with this code: http://pastebin.com/VUcW8MRg
babonk
Make sure that you specify the id to dropdown and specify that correctly in the code, also try this variation: `$('#dropdown_id').val('No Matches');`
Sarfraz
the id is correctly specified. When I do $('#dropdown_id').val('No Matches'); it puts it directly in the input rather than in the dropdown. Are you sure "option" is the right thing? Maybe im wrong but I thought the autocomplete works through a list rather than an option select..?
babonk
@babonk: Make sure that you have specified the same id to more than one elment for example text field and dropdown. `dropdown_id` should be the id of dropdown eg `<select id="dropdown_id">`
Sarfraz
I updated the question to include my current HTML + jQuery.. What change should I make?
babonk
@babonk: see my update please. Your dropdown keyword you said created the confusion
Sarfraz
$('#myAutocomplete').val('No Matches'); makes it show "No Matches" in the text input
babonk
@babonk: That is what you updated in your question, should it show elsewhere?
Sarfraz
It should show in the autocomplete dropdown, same as results do
babonk
@babonk: Please post the html for your dropdown too in your question.
Sarfraz
There is no html for the dropdown.. it's all handled by the autocomplete jQuery
babonk
@babonk: Show me the link of your page i will check out myself
Sarfraz
@babonk: see my update 2 please
Sarfraz
@sarfaz: by first dropdown, i mean first autocomplete (Your School). And like I said, doing $('#school-name').val('No Matches'); makes it show "No Matches" in the text input, not in the dropdown of it
babonk
o man i said `$('#school-term').val('No Matches');` not `$('#school-name').val('No Matches');` Can't you see the difference of ids in both lines?
Sarfraz
Yes, I see the difference, thats why I clarified above.. Term is not related to this.. I want the drop down on school-name to change
babonk