Skip to content
This repository
Browse code

added simple summarizing to datatables

only shown data is summarized, so when a table is filtered only the
filtered data is summarized
  • Loading branch information...
commit e9a41540ebf2554890255e16414055ad3b08b917 1 parent 0d86e67
Andreas Gohr authored December 13, 2011
2  plugin.info.txt
... ...
@@ -1,7 +1,7 @@
1 1
 base    data
2 2
 author  Andreas Gohr
3 3
 email   andi@splitbrain.org
4  
-date    2010-06-08
  4
+date    2011-12-13
5 5
 name    Structured Data Plugin
6 6
 desc    Add and query structured data in your wiki
7 7
 url     http://www.dokuwiki.org/plugin:data
35  syntax/table.php
@@ -14,6 +14,8 @@ class syntax_plugin_data_table extends DokuWiki_Syntax_Plugin {
14 14
      */
15 15
     var $dthlp = null;
16 16
 
  17
+    var $sums = array();
  18
+
17 19
     /**
18 20
      * Constructor. Load helper plugin
19 21
      */
@@ -67,6 +69,7 @@ function handle($match, $state, $pos, &$handler){
67 69
         $data = array('classes' => $class,
68 70
                       'limit'   => 0,
69 71
                       'dynfilters' => false,
  72
+                      'summarize'  => false,
70 73
                       'headers' => array());
71 74
 
72 75
         // parse info
@@ -144,6 +147,9 @@ function handle($match, $state, $pos, &$handler){
144 147
                 case 'dynfilters':
145 148
                         $data['dynfilters'] = (bool) $line[1];
146 149
                     break;
  150
+                case 'summarize':
  151
+                        $data['summarize'] = (bool) $line[1];
  152
+                    break;
147 153
                 default:
148 154
                     msg("data plugin: unknown option '".hsc($line[0])."'",-1);
149 155
             }
@@ -213,6 +219,19 @@ function render($format, &$R, $data) {
213 219
                                 $data['cols'][$clist[$num]],
214 220
                                 $cval,$R);
215 221
                 $R->doc .= $this->after_val;
  222
+
  223
+                // clean currency symbols
  224
+                $nval = str_replace('$€₤','',$cval);
  225
+                $nval = str_replace('/ [A-Z]{0,3}$/','',$nval);
  226
+                $nval = str_replace(',','.',$nval);
  227
+                $nval = trim($nval);
  228
+
  229
+                // summarize
  230
+                if($data['summarize'] && is_numeric($nval)){
  231
+                    if(!isset($this->sums[$num])) $this->sums[$num] = 0;
  232
+                    $this->sums[$num] += $nval;
  233
+                }
  234
+
216 235
             }
217 236
             $R->doc .= $this->after_item;
218 237
         }
@@ -303,6 +322,22 @@ function nullList($data, $clist, &$R) {
303 322
     function postList($data, $rowcnt) {
304 323
         global $ID;
305 324
         $text = '';
  325
+        // if summarize was set, add sums
  326
+        if($data['summarize']){
  327
+            $text .= '<tr>';
  328
+            $len = count($data['cols']);
  329
+            for($i=0; $i<$len; $i++){
  330
+                $text .= '<td>';
  331
+                if(!empty($this->sums[$i])){
  332
+                    $text .= '∑ '.$this->sums[$i];
  333
+                }else{
  334
+                    $text .= '&nbsp;';
  335
+                }
  336
+                $text .= '</td>';
  337
+            }
  338
+            $text .= '<tr>';
  339
+        }
  340
+
306 341
         // if limit was set, add control
307 342
         if($data['limit']){
308 343
             $text .= '<tr><th colspan="'.count($data['cols']).'">';

0 notes on commit e9a4154

Please sign in to comment.
Something went wrong with that request. Please try again.