jQuery UI Autocomplete Combobox Very Slow With Large Select Lists

前端 未结 5 1309
清歌不尽
清歌不尽 2020-12-04 05:45

I\'m using a modified version of the jQuery UI Autocomplete Combobox, as seen here: http://jqueryui.com/demos/autocomplete/#combobox

For the sake of this questio

5条回答
  •  [愿得一人]
    2020-12-04 06:12

    I like the answer from Berro. But because it was still a bit slow (I had about 3000 options in select), i modified it slightly so that only first N matching results are displayed. I also added an item at the end notifying the user that more results are available and canceled focus and select events for that item.

    Here is modified code for source and select functions and added one for focus:

    source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        var select_el = select.get(0); // get dom element
        var rep = new Array(); // response array
        var maxRepSize = 10; // maximum response size  
        // simple loop for the options
        for (var i = 0; i < select_el.length; i++) {
            var text = select_el.options[i].text;
            if ( select_el.options[i].value && ( !request.term || matcher.test(text) ) )
                // add element to result array
                rep.push({
                    label: text, // no more bold
                    value: text,
                    option: select_el.options[i]
                });
            if ( rep.length > maxRepSize ) {
                rep.push({
                    label: "... more available",
                    value: "maxRepSizeReached",
                    option: ""
                });
                break;
            }
         }
         // send response
         response( rep );
    },          
    select: function( event, ui ) {
        if ( ui.item.value == "maxRepSizeReached") {
            return false;
        } else {
            ui.item.option.selected = true;
            self._trigger( "selected", event, {
                item: ui.item.option
            });
        }
    },
    focus: function( event, ui ) {
        if ( ui.item.value == "maxRepSizeReached") {
            return false;
        }
    },
    

提交回复
热议问题