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

Charlie Garrison garrison at zeta.org.au
Sun Jun 19 09:44:40 GMT 2011


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.

<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

-- 
    Ꮚ Charlie Garrison ♊ <garrison at zeta.org.au>

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
〠  http://www.ietf.org/rfc/rfc1855.txt




More information about the Catalyst mailing list