[Catalyst] Alphabetical Paging based on First Letter
Cory Watson
jheephat at gmail.com
Thu Feb 28 03:40:49 GMT 2008
On Wed, Feb 27, 2008 at 6:01 PM, Ashley <apv at sedition.com> wrote:
> On Feb 27, 2008, at 3:50 PM, Matt Knesi wrote:
> > I would like to implement a page navigation based on the first
> > letter of the items, e.g. first letter of last names, so all last
> > names with 'A' will display when you click on 'A' (instead of page
> > 1) and all last names starting with 'M' appear when you click on
> > 'M' and so on...
> >
> > Only the initials that are actually present in the database should
> > be displayed for paging navigation, so if there is NO last name
> > starting with 'X', 'X' shouldn't show up in the navigation line.
>
> I've done this with a dictionary application. You could do a
> relationship to a letter. So say name has_one letter, letter has_many
> names. Or what I've done a couple of times is make "letter" a field
> in the target table (word for me, name for you) and just keep the
> letter or "#" for a leading number/special and make it an index so
> it's fast to lookup.
>
I'm not a fan of munging data unless there's a need for high performance.
Unless this is a gigantic table you could probably just do:
# I didn't syntax test this, ymmv
my $rs =3D $schema->resultset('Person')->search(undef, {
select =3D> \'SUBSTR(name, 1, 1)',
as =3D> 'letter',
group_by=3D> \'SUBSTR(name, 1, 1)
});
my @letters =3D $rs->get_column('letter')->all();
$c->stash->{'letters'} =3D \@letters;
and in TT:
[% FOREACH letter =3D letters %]<a href...>[% letter %]</a>[% END %]
Then you'd have your list and not have to do any column adding or name
munging.
I'd also add a custom resultset and a method that did something like:
sub starts_with {
my ($self, $str) =3D @_;
return $self->search({ name =3D> { '-like' =3D> "$str\%" } });
}
So that you can do $rs->starts_with($c->req->param('letter')).
-- =
Cory 'G' Watson
http://www.onemogin.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20080227/fc41a=
d9c/attachment.htm
More information about the Catalyst
mailing list