From 2b6a88303ee81419e9583c4fc34e71f4e692a2e0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 20 Aug 2009 01:26:27 +0100 Subject: [PATCH] Sortable commodity price table --- yarrg/web/query_commod | 80 +++++++++++++++++++++++++++++++++--------- yarrg/web/tabsort | 15 ++++++-- 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod index c9b17ae..052d019 100644 --- a/yarrg/web/query_commod +++ b/yarrg/web/query_commod @@ -89,6 +89,8 @@ $someresults->(); #---------- actually compute the results and print them ---------- +my $onloads= ""; + foreach my $bs (split /_/, $ARGS{BuySell}) { $bs =~ m/^(buy|sell)$/ or die; $bs= $1; @@ -118,23 +120,24 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {

Offers to <% uc $bs |h %> <% $commodname |h %>

- +
+ % $islands->execute($commodid); % my $island; +% my %ts_sortkeys; % while ($island= $islands->fetchrow_hashref) { % my $islandid= $island->{'islandid'}; % $offers->execute($commodid, $islandid); @@ -161,21 +164,64 @@ foreach my $bs (split /_/, $ARGS{BuySell}) { % $approxqty += $qty; % } % } -% my $nstalls= @beststalls; +% my $stallname; +% +% my $rowid= "id_${bs}_$islandid"; +% my $s= [ ]; +% +% $s->[2]= sprintf "%06d", scalar @beststalls; +% if (!@beststalls) { +% $stallname= '-'; +% } elsif (@beststalls==1) { +% $stallname= $beststalls[0]; +% $s->[2] .= " $stallname"; +% } else { +% $stallname= sprintf "%d offers", scalar @beststalls; +% } +% % $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid "; - > + +% for my $cix (0..$#$s) { +% $ts_sortkeys{$cix}{$rowid}= $s->[$cix]; +% } % }
Prices -Quantities available -
Quantity at price +
Archipelago Island -Unique best stall +Stall(s) Best Median -At best -Within 10% -Total +Best ++/-10% +Any
<% $island->{'archipelago'} |h %> - <% $island->{'islandname'} |h %> - <% $nstalls==0 ? '-' : - $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %> - <% length $bestqty ? $bestprice : '-' %> - <% $median %> - <% $bestqty %> - <% $approxqty %> - <% $cqty %> +
<% $s->[0]= $island->{'archipelago'} |h %> + <% $s->[1]= $island->{'islandname'} |h %> + <% $stallname |h %> + <% $s->[3]= (length $bestqty ? $bestprice : '-') %> + <% $s->[4]= $median %> + <% $s->[5]= $bestqty %> + <% $s->[6]= $approxqty %> + <% $s->[7]= $cqty %>
+<& tabsort, table => "${bs}_table", sortkeys => "${bs}_sortkeys", + throw => "${bs}_table_thr", cols => [ + {}, {}, + { DoReverse => 1 }, + { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" }, + { DoReverse => 1, Numeric => 1, MapFn => "ts_Pricemap_${bs}" }, + { DoReverse => 1, Numeric => 1 }, + { DoReverse => 1, Numeric => 1 }, + { DoReverse => 1, Numeric => 1 }, + ] &> +<&| script &> + <% $bs %>_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>; + function ts_Pricemap_<% $bs %>(price) { + if (price=='-') { return <% $bs eq 'buy' ? '-1' : '99999999' %>; } + return price; + } + +% $onloads .= " ts_onload__${bs}_table();\n"; + <%perl> } + +<&| script &> + function all_onload() { +<% $onloads %> + } + window.onload= all_onload; + diff --git a/yarrg/web/tabsort b/yarrg/web/tabsort index e690ac8..ad25f86 100644 --- a/yarrg/web/tabsort +++ b/yarrg/web/tabsort @@ -38,6 +38,7 @@ <%args> $table => 'ts_table' $sortkeys => 'ts_sortkeys' +$throw => undef $cols @@ -60,6 +61,9 @@ function <% $sortfn %>(compar) { var row= rows.item(rowix); debug('process row '+rowix+' [[ '+row+' ]] id='+row.id) if (!row.id) continue; +% if (defined $throw) { + if (row.id == '<% $throw %>') continue; +% } if (row.tagName != 'TR') continue; newrows.push(row); } @@ -83,7 +87,12 @@ function <% $mapfn %>(rowelement) { % if ($col->{SortKey}) { return <% $col->{SortKey} %>; % } else { - return <% $sortkeys %>[<% $cix %>][rowid]; +% my $sk= "$sortkeys"."[$cix][rowid]"; +% if ($col->{MapFn}) { + return <% $col->{MapFn} %>(<% $sk %>); +% } else { + return <% $sk %>; +% } % } } @@ -116,8 +125,8 @@ function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); } function ts_onload__<% $table %>() { var ts_add_heads= <% to_json_protecttags(\%add_heads) %>; - var table= document.getElementById('<% $table %>'); - var firstth= table.getElementsByTagName('th').item(0); + var ctr= document.getElementById('<% defined($throw) ? $throw : $table %>'); + var firstth= ctr.getElementsByTagName('th').item(0); var thlist= firstth.parentNode.getElementsByTagName('th'); debug('thlist='+thlist); debug('thlist.item(2)=' + thlist.item(2)); -- 2.30.2