[Catalyst] ResultSet renderer?
Bruce J Keeler
bruce at drangle.com
Fri Jan 4 22:20:39 GMT 2008
Greetings, Catalystos!
I'm looking to find or create a canned solution for rendering
DBIx::Class::ResultSets as paged, sortable HTML tables. All of the
examples I've seen do this manually, foreaching through the resultset,
spitting out <tr>s and <td>s and so on. Seems like the sort of wheel
that shouldn't need to be reinvented.
I've poked around on CPAN and the wiki, but don't see anything like what
I'm looking for. What I'm envisioning in my minds eye is something that
would work a bit like this:
# ... in a controller:
$rs = $c->model("Employee")->search(undef, { join => [qw/ dept /],
prefetch => [qw/dept/] });
# Helper wrapper object defines which columns to show and hints at how
to render them
$rrh = ResultSetRendererHelper->new( $rs, [
{
column => "name", title => "Employee Name", align => "left"
link => sub { $c->uri_for("/employee/") . $_[0]->id }, # $_[0]
being a DBIx::Class::Row
}, {
column => sub { $_[0]->dept->name }, title => "Department",
align => "left",
link => sub { $c->uri_for("/dept/" . $_[0]->dept->id },
}, {
column => "salary", title => "Salary", format_as => "currency",
},
] );
$c->stash->{employees} = $rrh;
$c->stash->{template} = "/employee/list.tt2";
}
...and in /employee/list.tt2:
[% META title = "Employee List" -%]
<h2>Look at all these great employees! </h2>
[% INCLUDE resultsetrenderer
resultsetrendererhelper = employees
%]
... and the, due to magic implemented between the resultsetrenderer TT
template set and the Perl helper, it would spit out an HTML table with
clickable headers, paging links at the bottom, and possibly other
goodies like links to download the result set into a spreadsheet or as
CSV. All kinds of possibilities.
I'm interested in hearing about anything that already does this, or
failing that, critiques and suggestions.
Thanks,
Bruce
More information about the Catalyst
mailing list