5 require_once 'prototype.php';
8 <script type="text/javascript" src="/plekit/tablesort/tablesort.js"></script>
9 <script type="text/javascript" src="/plekit/tablesort/customsort.js"></script>
10 <script type="text/javascript" src="/plekit/tablesort/plc_customsort.js"></script>
11 <script type="text/javascript" src="/plekit/tablesort/paginate.js"></script>
12 <script type="text/javascript" src="/plekit/table/table.js"></script>
13 <link href="/plekit/table/table.css" rel="stylesheet" type="text/css" />
16 ////////////////////////////////////////
17 // table_id: <table>'s id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code
18 // headers: an associative array; the values can take several forms
19 // simple/legacy form is "label"=>"type"
20 // more advanced form is "label"=>options, it self a dict with the following known keys
21 // (*) 'type': the type for sorting; this is passed to the javascript layer for custom sorting
22 // default is to use 'text', custom sort functions can be specified with e.g. 'type'=>'sortAlphaNumericBottom'
23 // a special case is for type to be 'date-format' like e.g. 'type'=>'date-dmy'
24 // setting type to 'none' gives an non-sortable column
25 // (*) 'title': if set, this is used in the "Sort on ``<title>''" bubble
26 // sort_column: the column to sort on at load-time - set to negative number for no onload- sorting
27 // options : an associative array to override options
28 // - bullets1 : set to true if you want decorative bullets in column 1 (need white background)
29 // - stripes : use diferent colors for odd and even rows
30 // - caption : a caption for the table -- never used I'm afraid
31 // - search_area : boolean (default true)
32 // - pagesize_area : boolean (default true)
33 // - notes_area : boolean (default true)
34 // - search_width : size in chars of the search text dialog
35 // - pagesize: the initial pagination size
36 // - pagesize_def: the page size when one clicks the pagesize reset button
37 // - max_pages: the max number of pages to display in the paginator
38 // - notes : an array of additional notes
39 // - debug: enables debug callbacks (prints out on console.log)
47 var $bullets1; // boolean - default false - display decorative bullets in column 1
48 var $stripes; // boolean - default true - use different colors for odd and even rows
49 var $caption; // string - never used so far
50 var $search_area; // boolean (default true)
51 var $pagesize_area; // boolean (default true)
52 var $notes_area; // boolean (default true)
53 var $search_width; // size in chars of the search text dialog
54 var $pagesize; // the initial pagination size
55 var $pagesize_def; // the page size when one clicks the pagesize reset button
56 var $max_pages; // the max number of pages to display in the paginator
57 var $notes; // an array of additional notes
58 var $debug; // set to true for enabling various log messages on console.log
63 function PlekitTable ($table_id,$headers,$sort_column,$options=NULL) {
64 $this->table_id = $table_id;
65 $this->headers = $headers;
66 $this->sort_column = $sort_column;
68 $this->bullets1 = true;
71 $this->search_area = true;
72 $this->pagesize_area = true;
73 $this->notes_area = true;
74 $this->search_width = 40;
76 $this->pagesize_def = 9999;
77 $this->max_pages = 10;
78 $this->notes = array();
81 $this->set_options ($options);
84 $this->has_tfoot=false;
87 function set_options ($options) {
90 if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
91 if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
92 if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
93 if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
94 if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
95 if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
96 if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
97 if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
98 if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
99 if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
100 if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
101 if (array_key_exists('debug',$options)) $this->debug=$options['debug'];
104 public function columns () {
105 return count ($this->headers);
109 public function start () {
110 $paginator=$this->table_id."_paginator";
111 $classname="paginationcallback-".$paginator;
112 $classname.=" max-pages-" . $this->max_pages;
113 $classname.=" paginate-" . $this->pagesize;
114 if ($this->bullets1) { $classname .= " bullets1"; }
115 if ($this->stripes) { $classname .= " rowstyle-alt"; }
116 if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; }
118 // instantiate paginator callback
119 print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n";
121 // instantiate debug hooks if needed
123 $cb_init = $this->table_id."_init";
124 print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n";
125 $classname .= " sortinitiatedcallback-$cb_init";
126 $cb_comp = $this->table_id."_comp";
127 print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n";
128 $classname .= " sortcompletecallback-$cb_comp";
130 // start actual table
131 print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n";
133 if ($this->pagesize_area)
134 print $this->pagesize_area_html ();
135 if ($this->search_area)
136 print $this->search_area_html ();
139 print "<caption> $this->caption </caption>";
141 foreach ($this->headers as $label => $colspec) {
142 // which form is being used
143 if (is_array($colspec)) {
144 $type=$colspec['type'];
145 $title=ucfirst($colspec['title']);
147 // simple/legacy form
154 case "string": case "int": case "float":
155 $class="sortable"; break;
156 case ( strpos($type,"date-") == 0):
157 $class="sortable-" . $type; break;
159 $class="sortable-sort" . $type; break;
161 $title_part=$title ? "title=\"$title\"" : "";
162 print ("<th class=\"$class plekit_table\" $title_part>$label</th>\n");
165 print "</tr></thead><tbody>";
169 // for convenience, the options that apply to the bottom area can be passed here
170 // typically notes will add up to the ones provided so far, and to the default ones
171 // xxx default should be used only if applicable
172 function end ($options=NULL) {
173 $this->set_options($options);
174 print $this->bottom_html();
175 if ($this->notes_area)
176 print $this->notes_area_html();
180 function pagesize_area_html () {
181 $width=count($this->headers);
182 $pagesize_text_id = $this->table_id . "_pagesize";
184 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
185 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
186 <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
187 onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);'
188 size='3' maxlength='4' />
189 <label class='pagesize_label'> items/page </label>
190 <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
191 onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
192 </fieldset></form></td></tr>
198 function search_area_html () {
199 $width=count($this->headers);
200 $search_text_id = $this->table_id . "_search";
201 $search_reset_id = $this->table_id . "_search_reset";
202 $search_and_id = $this->table_id . "_search_and";
204 <tr class='search_area'><td class='search_area' colspan='$width'>
205 <div class='search'><fieldset>
206 <label class='search_label'> Search </label>
207 <input class='search_input' type='text' id='$search_text_id'
208 onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
209 size='$this->search_width' maxlength='256' />
211 <input id='$search_and_id' class='search_and'
212 type='checkbox' checked='checked'
213 onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
214 <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
215 onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
216 </fieldset></div></td></tr>
221 //////////////////// start a <tfoot> section
222 function tfoot_start () { print $this->tfoot_start_html(); }
223 function tfoot_start_html () {
224 $this->has_tfoot=true;
225 return "</tbody><tfoot>";
228 ////////////////////////////////////////
229 function bottom_html () {
231 if ($this->has_tfoot)
232 $result .= "</tfoot>";
234 $result .= "</tbody>";
235 $result .= "</table>\n";
239 ////////////////////////////////////////
240 function notes_area_html () {
242 array("Enter & or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes");
244 array ("Hold down the shift key to select multiple columns to sort");
250 $notes=array_merge($notes,$sort_notes);
251 if ($this->search_area)
252 $notes=array_merge($notes,$search_notes);
256 $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
257 foreach ($notes as $note)
258 $result .= "<br/>$note\n";
263 ////////////////////////////////////////
264 function row_start ($id=NULL,$class=NULL) {
266 if ( $id) print (" id=\"$id\"");
267 if ( $class) print (" class=\"$class\"");
271 function row_end () {
276 // supported options:
277 // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
282 public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
283 public function cell_html ($text,$options=NULL) {
284 if (isset ($options['only-if']) && ! $options['only-if'] )
288 $option=$options['class']; if ($option) $html .= " class='$option'";
289 $option=$options['columns'];if ($option) $html .= " colspan='$option'";
290 $option=$options['hfill']; if ($option) $html .= " colspan='" . $this->columns() . "'";
291 $option=$options['align']; if ($option) $html .= " style='text-align:$option'";
292 $html .= ">$text</td>";