11

我正在使用 phonegap 开发一个 android 应用程序。在我使用 jquery 组合框的应用程序中有一个自动完成选择框。当我为组合框使用静态源时,组合框工作正常。但我必须使用动态源,因为有大量数据。数据存储在 sqlite 数据库中。数据中有大约 12000 个项目,我想过滤掉它以仅显示大约 10 个项目,在组合框中键入了起始字母。但是组合框只显示前 10 个项目,而不是在数据库中搜索每个键入的单词。仅在按下退格键时才搜索数据库。

这是组合框代码:

function loadCustomer(){

    //console.log('No rows affected!');
    //navigator.notification.alert(results.rows.item(0).name);

            (function( $ ) {
        $.widget( "ui.customer", {
            _create: function() {
                var input,
                    self = this,
                    select = this.element.hide(),
                    selected = select.children( ":selected" ),
                    value = selected.val() ? selected.text() : "",
                    wrapper = this.wrapper = $( "<span>" )
                        .addClass( "ui-customer" )
                        .insertAfter( select );

                input = $( "<input id='customer_input' autofocus>" )
                    .appendTo( wrapper )
                    .val( value )
                    .addClass( "ui-state-default ui-customer-input" )
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        autofocus:true,
                        source: function( request, response ) {
                            var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex(request.term), "i" );
                            response( select.children( "option" ).map(function() {
                                var text = $( this ).text();
                                if ( this.value && ( !request.term || matcher.test(text) ) )
                                    return {
                                        label: text.replace(
                                            new RegExp(
                                                "(?![^&;]+;)(?!<[^<>]*)(" +
                                                $.ui.autocomplete.escapeRegex(request.term) +
                                                ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                            ), "<strong>$1</strong>" ),
                                        value: text,
                                        option: this
                                    };
                            }) );
                        },
                        select: function( event, ui ) {
                            ui.item.option.selected = true;
                            self._trigger( "selected", event, {
                                item: ui.item.option
                            });
                            show_balance();
                            updateGrid();
                            $("#next_link").focus();
                        },
                        search: function(event, ui) {var db = window.openDatabase("Database", "1.0", "Cordova Demo", 2097152);
                            db.transaction(selectCustomer, errorCB);},
                        change: function( event, ui ) {
                            if ( !ui.item ) {
                                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                    valid = false;
                                select.children( "option" ).each(function() {
                                    if ( $( this ).text().match( matcher ) ) {
                                        this.selected = valid = true;
                                        return false;
                                    }
                                });
                                if ( !valid ) {
                                    // remove invalid value, as it didn't match anything
                                    $( this ).val( "" );
                                    select.val( "" );
                                    input.data( "autocomplete" ).term = "";
                                    return false;
                                }
                            }
                        }
                    })
                    .focus(function() {
       $(this).autocomplete("search", "");})
                    .addClass( "ui-widget ui-widget-content ui-corner-left" );

                input.data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };

                $( "<a>" )
                    .attr( "tabIndex", -1 )
                    .attr( "title", "Show All Items" )
                    .appendTo( wrapper )
                    .button({
                        icons: {
                            primary: "ui-icon-triangle-1-s"
                        },
                        text: false
                    })
                    .removeClass( "ui-corner-all" )
                    .addClass( "ui-corner-right ui-customer-toggle" )
                    .click(function() {
                        // close if already visible
                        if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                            input.autocomplete( "close" );
                            return;
                        }

                        // work around a bug (likely same cause as #5265)
                        $( this ).blur();

                        // pass empty string as value to search for, displaying all results
                        input.autocomplete( "search", "" );
                        input.focus();
                    });
            },

            destroy: function() {
                this.wrapper.remove();
                this.element.show();
                $.Widget.prototype.destroy.call( this );
            }
        });
    })( jQuery );

    $(function() {
        $( "#customer" ).customer();

    });
    return false;

    }

这是从数据库中选择数据的代码:

function selectCustomer(tx) {
        requestterm=$("#customer_input").val();
        tx.executeSql('SELECT * FROM CUSTOMER WHERE name LIKE "'+requestterm+'%" LIMIT 10', [], selectCustomerSuccess, errorCB);

    }



function selectCustomerSuccess(tx, results) {

  if (!results.rowsAffected) {
    //console.log('No rows affected!');
    //navigator.notification.alert(results.rows.item(0).name);
     var len = results.rows.length;

    var options = '<option value=""></option>';
            for (var i=0; i<len; i++) {
                options += '<option value="' + results.rows.item(i).custid + '">' + results.rows.item(i).name + '</option>';
            }
            $("#customer").html(options);
            $('#customer option:first').attr('selected', 'selected');

return false;

  }

    }

请帮我。提前致谢。

4

1 回答 1

1

你有两个问题:

1) 查询只返回十个结果

您的查询仅返回 10,因为您LIMIT 10在其中有一个。所以(我认为)你想要selectCustomer()看起来像这样:

function selectCustomer(tx) {
    requestterm=$("#customer_input").val();
    tx.executeSql('SELECT * FROM CUSTOMER WHERE name LIKE "'+requestterm+'%"', [], selectCustomerSuccess, errorCB);
}

2) 小部件仅在按下退格键时搜索

我不确定这个。文档说只执行“search在请求(源选项)开始之前,在满足 minLength 和延迟之后。”。可能是您的查询由于某种原因需要一段时间才能执行,而当它们赶上时您正在按退格键只是巧合?另外,您是否尝试过向parse自动完成小部件添加功能?parse允许您通过在 之后立即调用来过滤搜索结果search,这样可以帮助您调试它。

于 2012-10-28T13:59:10.703 回答