views:

51

answers:

2

Hi. I'm a newbie programmer / designer trying to configure jquery ui autocomplete. I have it working, using a javascript object (array). The array contains retailer stores that we are directing our customers to. We have certain stores that are preferred and so we would like for them to come up earlier than other stores but I don't really know how to filter them into the result or if it is even possible.

I want "Widget Store 4" to appear first if anyone types in "Widget". Here's the jquery code:

var widgetstores = [ 
{label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 4", value: "1004" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]

 $(function() {     
                $('#tags').autocomplete({
                    minLength: 3,
                    source: widgetstores,
                    focus: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        return false;
                    },
                    select: function(event, ui) {
                        $('#tags').val(ui.item.label);
                        $('#customer_num').val(ui.item.value);                      
                        return false;
                    }
                })
                .data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };
            });
+1  A: 

I'm not sure what AutoComplete library you're using, but it looks like:

http://docs.jquery.com/UI/Autocomplete

That library does not sort the items for you automatically. It uses the order of the items passed to the script, and filters from that. So if your items are in the order you want them before you pass to the autocomplete it should use that order.

Update:

var widgetstores = [ 
{label: "Widget Store 4", value: "1004" }, {label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]
Ryan Ternier
+1 : that's what I thought, but was not quite sure about it
tsimbalar
A: 

Thanks Ryan. Very simple solution and it worked! I thought it would be something convoluted.

glaven