开发者

how to trigger the auto search option of multiple-remote jquery autocomplete UI

I am using this auto complete library: http://jqueryui.com/demos/autocomplete/#multiple-remote

I was wondering if anyone knows how trigger the auto search without typing anything in the text box. i.e. if we want to display the list on a button click event. the "search" method seems to be doing that. But I cant get it working.

Please note that the data of the auto suggest comes from a web service. If user types something in the text box the suggestion changes accordingly. i.e. the typed data goes to the service and brings back the suggestion. The input is of "Name, Location" form. As a result for different parts of the input different types of suggestions are displa开发者_开发问答yed.

Codes:

UI:

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
</div>

Script:

<script>
    $(function() {
        function split( val ) {
            return val.split( /,\s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $( "#birds" )               
            .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "autocomplete" ).menu.active ) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                source: function( request, response ) {
                    $.getJSON( "search.php", {
                        term: extractLast( request.term )
                    }, response );
                },
                search: function() {
                    var term = extractLast( this.value );
                    if ( term.length < 2 ) {
                        return false;
                    }
                },
                focus: function() {
                    return false;
                },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    terms.pop();
                    terms.push( ui.item.value );
                    terms.push( "" );
                    this.value = terms.join( ", " );
                    return false;
                }
            });
    });
    </script>

If I type anything it sends a request to search.php. I tried changing the logic "term.length < 2" to "term.length <= 0". This works but i have to press the space bar. Then an empty space is placed in the text box and is sends the request to the server. But I don't want to type anything in there. Hope that helps.


If you want to trigger the search when a button is clicked, then you have to call the search method. If you want to show all options, call search with the value set to empty string and set the autocomplete widget to accept minLength: 0.

So in code:

UI

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" size="50" />
    <input type="button" id="search-trigger" value="Trigger" />
</div>

Script

<script type="text/javascript">
$(function() {
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    // Button listener that triggs the search event
    $("#search-trigger").click(function(){
        var searchTerm = $( "#birds" ).val();
        $( "#birds" ).autocomplete( "search" , searchTerm);
    });


    $( "#birds" )               
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: ["lorem", "ipsum", "dolor"], 
                /* Commented this out to use dummy data above
                            function( request, response ) {
                $.getJSON( "search.php", {
                    term: extractLast( request.term )
                }, response );
            },*/
            search: function() {
                var term = extractLast( this.value );

                          /* What is this check for?
                if ( term.length < 2 ) {
                    return false;
                }*/
            },
            focus: function() {
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                terms.pop();
                terms.push( ui.item.value );
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            },
            //Added for "show all" support
            minLength: 0
        });
});
</script>

Is this the behavior you are looking for?

Ref: http://jqueryui.com/demos/autocomplete/#method-search

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜