Mojolicious :: Lite jQuery自动完成问题

时间:2011-09-09 08:15:25

标签: jquery perl autocomplete mojolicious

自动完成功能不起作用:整个方法是错误的还是只做了一些错误?

#!/usr/local/bin/perl
use warnings; use 5.014; use utf8;
use Mojolicious::Lite;
use DBI;
my $dbh = DBI->connect( ... ) or die $DBI::errstr;
my $table = 'my_table';

get '/input' => sub {
    my $self = shift;
    $self->render( 'input' );
};

get '/search_db' => sub {
    my $self = shift;
    my $col = $self->param( 'col' );
    my $sth = $dbh->prepare( "SELECT $col FROM $table" );
    $sth->execute();
    my $ref;
    while ( my $row = $sth->fetchrow_arrayref() ) {
        push @$ref, @$row;
    }
    $self->render( json => $ref );
};

app->start;

__DATA__
@@ input.html.ep
<!DOCTYPE HTML>
<html>
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/js_local/development-bundle/jquery-1.6.2.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.core.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.widget.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.position.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.autocomplete.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#vorname").autocomplete({
                source: '/search_db?col=vorname',
                minLength: 2
            });
        });
    </script>
</head>
<body>
<form>
    <table>
        <tr><td>Vorname:</td><td><input type="text" id="vorname" 
        name="vorname" autocomplete="off"/></td></tr>
        <tr><td>Nachname:</td><td><input type="text" id="nachname" 
        name="nachname" autocomplete="on" /></td></tr>
    </table><br />
    <input type="submit" value="OK"/>
</form>
</body>
</html>

我想我更进了一步:现在在第二个角色之后我得到了所有的名字作为选择。

3 个答案:

答案 0 :(得分:2)

minLength之后你有一个额外的逗号:

$(function() { 
    $("#vorname").autocomplete({ 
        source: '/search_db?col=vorname', 
        minLength: 2, 
    }); 
}); 

首先尝试一下!

答案 1 :(得分:1)

我看不到你声明$table的位置?从命令行运行您的两个脚本我得到了:

Global symbol "$table" requires explicit package name at mojo_test2.pl line 19.
mojo_test2.pl had compilation errors.

答案 2 :(得分:0)

我找到了解决方案:

#!/usr/local/bin/perl
use warnings;
use 5.014;
use utf8;
use Mojolicious::Lite;
use DBI;

my $table = 'my_table';
my $dbh = DBI->connect( ... ) or die $DBI::errstr;

get '/eingabe' => sub {
    my $self = shift;
    $self->render( 'eingabe' );
};

get '/search_db/:col' => sub {
    my $self = shift;
    my $col = $self->param( 'col' );
    my $term = $self->param( 'term' );
    my $sth = $dbh->prepare( 
                  "SELECT DISTINCT $col FROM $table WHERE $col LIKE ?" 
              );
    $sth->execute( $term . '%');
    my $ref;
    while ( my $row = $sth->fetchrow_arrayref() ) {
            push @$ref, @$row;
    }
    $self->render( json => $ref );
};

app->start;

__DATA__
@@ eingabe.html.ep
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/development-bundle/jquery-1.6.2.js"></script>
    <script src="/development-bundle/ui/jquery.ui.core.js"></script>
    <script src="/development-bundle/ui/jquery.ui.widget.js"></script>
    <script src="/development-bundle/ui/jquery.ui.position.js"></script>
    <script src="/development-bundle/ui/jquery.ui.autocomplete.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() {
            var data = [];
            var form = document.forms[0];
            var formEls = form.elements;
            var elLen = formEls.length;
            for ( var i = 0; i < elLen; ++i ) {
                if ( formEls[i].type != 'submit' ) {
                    data.push( formEls[i].id );
                }
            }
            var dLen = data.length;
            for ( i = 0; i < dLen; i++ ){
                $( "#" + data[i] ).autocomplete({
                    delay: 100,
                    minLength: 2,                                 
                    source: '/search_db/' + data[i]
                });
            }
        });
    </script>
</head>
<body>
    <form>
        <table>
            <tr><td>Vorname:</td><td><input type="text" id="vorname" 
            name="vorname" autocomplete="off" autofocus="on" /></td></tr>
            <tr><td>Nachname:</td><td><input type="text" id="nachname" 
            name="nachname" autocomplete="on" /></td></tr>
        </table><br />
        <input type="submit" value="OK"/>
    </form>
</body>
</html>