chiark / gitweb /
Sortable commodity price table
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Thu, 20 Aug 2009 00:26:27 +0000 (01:26 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Thu, 20 Aug 2009 00:26:27 +0000 (01:26 +0100)
yarrg/web/query_commod
yarrg/web/tabsort

index c9b17ae6700b72a0e5ba135a14d95ff381ae3a98..052d019f998b137cd103f806c46bbbb0f3e0fd4a 100644 (file)
@@ -89,6 +89,8 @@ $someresults->();
 
 #---------- actually compute the results and print them ----------
 
 
 #---------- actually compute the results and print them ----------
 
+my $onloads= "";
+
 foreach my $bs (split /_/, $ARGS{BuySell}) {
        $bs =~ m/^(buy|sell)$/ or die;
        $bs= $1;
 foreach my $bs (split /_/, $ARGS{BuySell}) {
        $bs =~ m/^(buy|sell)$/ or die;
        $bs= $1;
@@ -118,23 +120,24 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
 
 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
 
 
 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
 
-<table>
+<table id="<% $bs %>_table">
 <tr>
 <th colspan=3>
 <th colspan=2>Prices
 <tr>
 <th colspan=3>
 <th colspan=2>Prices
-<th colspan=3>Quantities available
-<tr>
+<th colspan=3>Quantity at price
+<tr id="<% $bs %>_table_thr">
 <th>Archipelago
 <th>Island
 <th>Archipelago
 <th>Island
-<th>Unique best stall
+<th>Stall(s)
 <th>Best
 <th>Median
 <th>Best
 <th>Median
-<th>At best
-<th>Within 10%
-<th>Total
+<th>Best
+<th>+/-10%
+<th>Any
 </tr>
 %      $islands->execute($commodid);
 %      my $island;
 </tr>
 %      $islands->execute($commodid);
 %      my $island;
+%      my %ts_sortkeys;
 %      while ($island= $islands->fetchrow_hashref) {
 %              my $islandid= $island->{'islandid'};
 %              $offers->execute($commodid, $islandid);
 %      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;
 %                      }
 %              }
 %                              $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 ";
 %              $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
-<tr> <td><% $island->{'archipelago'} |h %>
-     <td><% $island->{'islandname'} |h %>
-     <td><% $nstalls==0 ? '-' :
-            $nstalls==1 ? $beststalls[0] : "$nstalls offers" |h %>
-     <td><% length $bestqty ? $bestprice : '-' %>
-     <td><% $median %>
-     <td><% $bestqty %>
-     <td><% $approxqty %>
-     <td><% $cqty %>
+<tr id=<% $rowid %> >
+     <td><% $s->[0]= $island->{'archipelago'} |h %>
+     <td><% $s->[1]= $island->{'islandname'} |h %>
+     <td><%          $stallname |h %>
+     <td><% $s->[3]= (length $bestqty ? $bestprice : '-') %>
+     <td><% $s->[4]= $median %>
+     <td><% $s->[5]= $bestqty %>
+     <td><% $s->[6]= $approxqty %>
+     <td><% $s->[7]= $cqty %>
 </tr>
 </tr>
+%              for my $cix (0..$#$s) {
+%                      $ts_sortkeys{$cix}{$rowid}= $s->[$cix];
+%              }
 %      }
 </table>
 
 %      }
 </table>
 
+<& 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>
 }
 </%perl>
 <%perl>
 }
 </%perl>
+
+<&| script &>
+  function all_onload() {
+<% $onloads %>
+  }
+  window.onload= all_onload;
+</&>
index e690ac8df740d2535ecc56425452e79ab4463305..ad25f86b9ab981d58ed2e740cd91cc9d7d6ba23e 100644 (file)
@@ -38,6 +38,7 @@
 <%args>
 $table => 'ts_table'
 $sortkeys => 'ts_sortkeys'
 <%args>
 $table => 'ts_table'
 $sortkeys => 'ts_sortkeys'
+$throw => undef
 $cols
 </%args>
 
 $cols
 </%args>
 
@@ -60,6 +61,9 @@ function <% $sortfn %>(compar) {
     var row= rows.item(rowix);
     debug('process row '+rowix+' [[ '+row+' ]] id='+row.id)
     if (!row.id) continue;
     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);
   }
     if (row.tagName != 'TR') continue;
     newrows.push(row);
   }
@@ -83,7 +87,12 @@ function <% $mapfn %>(rowelement) {
 %      if ($col->{SortKey}) {
   return <% $col->{SortKey} %>;
 %      } else {
 %      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) %>;
 
 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));
   var thlist= firstth.parentNode.getElementsByTagName('th');
   debug('thlist='+thlist);
   debug('thlist.item(2)=' + thlist.item(2));