And here it is, if JQuery::TableSorter were added I'd look for (jquery/css) ways to disable the uparrow/downarrow links if user has javascript on, so the browser does the work, not perl
I run this as perl mojo.template.array.autoindex.sortable.pl daemon --listen http://localhost:80/
The some variable names are dumb :) The subs then mojo stuff before __DATA__ order is intentional, see Mojolicious::Lite
#!/usr/bin/perl --
# Automatically enables "strict", "warnings", "utf8" and Perl 5.10 fea
+tures
use Mojolicious::Lite;
sub my_sorted_fudge { ## has no pageination :P
my( $sortorder, $sortindex ) = @_;
$sortorder ||= '';
#~ $sortindex ||= 0;
$sortindex = int $sortindex ;
my %order = (
'ascend' => sub { $$a[ $sortindex ] cmp $$b[ $sortindex ] },
'descend' => sub { $$b[ $sortindex ] cmp $$a[ $sortindex ] },
'' => undef, # default sort order, none
);
my $byorder = $order{ $sortorder };
my @th = ( "Ro", "Sham", "Bo" );
my @array = (
["i", "f", "i"],
["q", "s", "n"],
["u", "g", "m"],
["j", "r", "n"],
["w", "r", "j"],
["q", "q", "n"],
["x", "p", "p"],
["o", "h", "b"],
["g", "x", "a"],
);
$byorder and @array = sort $byorder @array; ## don't sort by defau
+lt
return \@array, \@th ;
}
sub mojo_index {
my $self = shift;
my $sortorder = $self->param('sortorder') || '';
my $sortindex = $self->param('sortindex') || 0;
my( $rah, $rahth ) = my_sorted_fudge( $sortorder, $sortindex );
return $self->render(
ray => $rah,
rayth => $rahth,
sortindex => $sortindex,
sortorder => $sortorder,
title => 'the sortening',
);
}
## .ep templates are auto_escape <%= by default / XML escaped result
get '/' => \&mojo_index => 'index';;;
app->start;
__DATA__
@@ index.html.ep
% layout 'green';
%= content content => begin
<table border="1">
<thead>
<tr>
% for my $thix ( 0 .. $#$rayth ){
<th>
<%# uparrow =%>
<a href="/?sortorder=ascend;sortindex=<%= $thix %>
+">↑</a>
<%= $$rayth[$thix] =%>
<%# downarrow =%>
<a href="/?sortorder=descend;sortindex=<%= $thix %
+>">↓</a>
</th>
% }
</tr>
</thead>
<tbody>
% for my $rai ( @$ray ) {
<tr>
% for my $cel ( @$rai ){
<td> <%= $cel =%> </td>
% }
</tr>
% }
</tbody>
</table>
% if( $sortorder and defined $sortindex ){
<div id="sortorderdesc"> * sortorder( <%= $sortorder %>)
by sortindex( <%= $sortindex %>
aka <%= $$rayth[ $sortindex ] %> )
</div>
% }
% end
@@ layouts/green.html.ep
<html>
<head>
<title><%== title %></title>
</head>
<body bgcolor="lightgreen">
<%= content %>
</body>
</html>