[Catalyst] Complete example for autocomplete with HTML::FormFu, please

Alex Povolotsky tarkhil at over.ru
Fri Jul 1 17:02:21 GMT 2011


On 06/19/11 13:44, Charlie Garrison wrote:
> Good evening,
>
> On 19/06/11 at 12:18 AM +0400, Alex Povolotsky <tarkhil at over.ru> wrote:
>
>> I'm trying to make a form with autocomplete text edit field; I do 
>> understand general workflow but I still miss some simple-but-complete 
>> example.
>>
>> Can anyone please provide me with one? JQuery would be the best.
>
> Ripped and simplified from code I'm using. So can't say it's a 
> complete example, but should have all the elements you need to make it 
> work.

Well... Nearly works, BUT.

# In template
$(document).ready(function(){
         $("#addblack").autocomplete('[% 
c.uri_for('/userinfo','complete_user', 'blacklist') %]');
   });

# In controller
sub complete_user  : Local : Args(1) {
     my ($self, $c, $mode) = @_;
     my $name = $c->req->param('term');
     if ($name gt '') {
         my $names;
         if ($mode eq 'blacklist') {
             $c->log->info('Autocompleting blacklist '.$name);
             $names = $c->model('Db::Account')->search({ login => { 
ILIKE => $name.'%' } },
                                                          { columns => 
[qw/login/], limit => 10, order_by => [qw/login/]});
             $c->log->info($names->count().' found');
             $c->stash->{entity} = [ map { { label => $_->login } } 
$names->all() ];
         } else {
             die "Wrong mode $mode";
         }
         $c->stash->{term} = $name;
     }
     $c->forward('View::JSON');
}

It requests data; it receives data. Autocompletion does not work.


In http://jqueryui.com/demos/autocomplete/, there is NOTHING at all (I 
really do hate them) about suggestions format expected by jQueryUI 
autocomplete.

I've tried every combitation I've managed to invent, but no success.

>
> <http://jqueryui.com/demos/autocomplete/>
>
>
> package MyApp::Controller::REST;
> use Moose;
> use namespace::autoclean;
>
> extends qw'Catalyst::Controller::REST';
>
> sub usernames :Chained('') :PathPart('usernames') :ActionClass('REST') 
> { }
> sub usernames_GET : Local {
>     my ( $self, $c ) = @_;
>     my $query  ||= $c->req->params->{query}  || '';
>     my $limit  ||= $c->req->params->{limit}  || 250;
>
>     my $usernames = $c->model('DBIC::User')->search(
>       { username => {'-like', "\%$query\%"}  },
>       { order_by => 'username',rows=>$limit  }
>     );
>     my @usernames_list = map { {value => $_->id, label => 
> $_->username} } $usernames->all;
>
>     $self->status_ok(
>         $c,
>         entity => \@usernames_list,
>     );
> }
>
> __PACKAGE__->meta->make_immutable;
> 1;
>
>
> ## edit.yml
> ........
>   - type: Hidden
>     name: user_id
>     constraints:
>       - SingleValue
>       - Integer
>   - type: Block
>     nested_name: user
>     elements:
>     - type: Text
>       name: username
>       label: User
>       model_config:
>         read_only: 1
>
> ## edit.tt2
> ........
> $(document).ready(function(){
>     $("#form_user\\.username").autocomplete({
>         minLength: 2,
>         source: function( request, response ) {
>             $.getJSON( "[% c.uri_for_action('/rest/usernames') %]", {
>                 query: request.term
>             }, response );
>         },
>         select: function( event, ui ) {
>             $( "#form_user\\.username" ).val( ui.item.label );
>             $( "#form_user_id"         ).val( ui.item.value );
>             return false;
>         },
>     });
> });
>
>
>
> Charlie
>




More information about the Catalyst mailing list