Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  193] [ 2]  / answers: 1 / hits: 53956  / 11 Years ago, mon, november 18, 2013, 12:00:00

I'm trying to have a No Results message appear in the dropdown menu if there are no results. So for instance, if I type in ABCD into the text field, and there is no entity that matches, the message No Results. will be displayed.



After looking through stackoverflow for the various different ways of accomplishing this, and trying a few of them, I still can't get it to work.



How can I add a No Results message to the dropdown menu when no results are found?



jQuery:



    $element.autocomplete({
source: function (request, response) {
$.ajax({
url: thUrl + thQS,
type: get,
dataType: json,
cache: false,
data: {
featureClass: P,
style: full,
maxRows: 12
},
success: function (data) {
response($.map(data, function (item) {
if (data.indexOf(item) === -1) {
return { label: No Results. }
} else {
return {
label: item.Company + ( + item.Symbol + ),
value: item.Company
}
}
}));
}
});
},
minLength: that.options.minLength,
select: function (event, ui) {
reRenderGrid();
}
});


I have tried adding an if() statement with the following but that didn't work.



if (data.length === 0) {
// Do logic for empty result.
}


I am able to overwrite the first entry with the text No Result if I do the following...



if (data.indexOf(item) === 0) {
return {
label: No Results.
}


...but if I set data.indexOf(item) === -1 nothing shows up.



I just recently tried the following, and when there is no data, it goes into the loop, however, No Results is not being displayed in the menu:



   success: function (data) {
response($.map(data, function (item) {
return {
label: item.Company + ( + item.Symbol + ),
value: item.Company
}
}));
if (data.length === 0) {
label: No Results.
}
}


I have also attempted to use the below example from Andrew Whitaker with no luck:



ANDREW WHITACKER'S FIDDLE: http://jsfiddle.net/J5rVP/128/



SOURCE: http://blog.andrewawhitaker.com/2012/10/08/jqueryui-autocomplete-1-9/


More From » jquery

 Answers
4

Modify the function like this to check for length of data.



success: function (data) {
if(!data.length){
var result = [
{
label: 'No matches found',
value: response.term
}
];
response(result);
}
else{
// normal response
response($.map(data, function (item) {
return {
label: item.CompanyName + ( + item.SymbolName + ),
value: item.CompanyName
}
}));
}
}

[#74221] Saturday, November 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;