문제

I got the following piece of code

$("#UserSearch").autocomplete({
    source: "search.php",
    minLength: 2
});

Here a user enters an email and I seek it through the database, but I need to find the moment when the autocomplete stops getting results. For example I got user@example.com and user2@example.com. If the current user seeks "user3" and the page no longer returns results how can I catch this moment so that I may display another element on the page.

도움이 되었습니까?

해결책

You can consider to check the number of displayed elements in the open event, if the len is 0 you can display another element accordingly.

Other solutions are to use a custom _renderItem function or a custom extended widget, but in this case this can be a simpler solution.

Code:

$("#project").autocomplete({
    minLength: 0,
    source: projects,
    open: function (event, ui) {
        var len = $('.ui-autocomplete > li').length;
        $('#count').html('Founded ' + len + ' results');
    }
});

Demo: http://jsfiddle.net/IrvinDominin/DZ9zU/

UPDATE

Better using response event:

Triggered after a search completes, before the menu is shown. Useful for local manipulation of suggestion data, where a custom source option callback is not required. This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled.

Code:

$("#project").autocomplete({
    minLength: 0,
    source: projects,
    response: function (event, ui) {
        var len = ui.content.length;
        $('#count').html('Founded ' + len + ' results');
    }
});

Demo: http://jsfiddle.net/IrvinDominin/DZ9zU/1/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top