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 "label"=>"type"
19 // sort_column: the column to sort on at load-time - set to negative number for no onload- sorting
20 // options : an associative array to override options
21 // - bullets1 : set to true if you want decorative bullets in column 1 (need white background)
22 // - stripes : use diferent colors for odd and even rows
23 // - caption : a caption for the table -- never used I'm afraid
24 // - search_area : boolean (default true)
25 // - pagesize_area : boolean (default true)
26 // - notes_area : boolean (default true)
27 // - search_width : size in chars of the search text dialog
28 // - pagesize: the initial pagination size
29 // - pagesize_def: the page size when one clicks the pagesize reset button
30 // - max_pages: the max number of pages to display in the paginator
31 // - notes : an array of additional notes
32 // - debug: enables debug callbacks (prints out on console.log)
40 var $bullets1; // boolean - default false - display decorative bullets in column 1
41 var $stripes; // boolean - default true - use different colors for odd and even rows
42 var $caption; // string - never used so far
43 var $search_area; // boolean (default true)
44 var $pagesize_area; // boolean (default true)
45 var $notes_area; // boolean (default true)
46 var $search_width; // size in chars of the search text dialog
47 var $pagesize; // the initial pagination size
48 var $pagesize_def; // the page size when one clicks the pagesize reset button
49 var $max_pages; // the max number of pages to display in the paginator
50 var $notes; // an array of additional notes
51 var $debug; // set to true for enabling various log messages on console.log
56 function PlekitTable ($table_id,$headers,$sort_column,$options=NULL) {
57 $this->table_id = $table_id;
58 $this->headers = $headers;
59 $this->sort_column = $sort_column;
61 $this->bullets1 = true;
64 $this->search_area = true;
65 $this->pagesize_area = true;
66 $this->notes_area = true;
67 $this->search_width = 40;
69 $this->pagesize_def = 9999;
70 $this->max_pages = 10;
71 $this->notes = array();
74 $this->set_options ($options);
77 $this->has_tfoot=false;
80 function set_options ($options) {
83 if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
84 if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
85 if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
86 if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
87 if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
88 if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
89 if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
90 if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
91 if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
92 if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
93 if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
94 if (array_key_exists('debug',$options)) $this->debug=$options['debug'];
97 public function columns () {
98 return count ($this->headers);
102 public function start () {
103 $paginator=$this->table_id."_paginator";
104 $classname="paginationcallback-".$paginator;
105 $classname.=" max-pages-" . $this->max_pages;
106 $classname.=" paginate-" . $this->pagesize;
107 if ($this->bullets1) { $classname .= " bullets1"; }
108 if ($this->stripes) { $classname .= " rowstyle-alt"; }
109 if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; }
111 // instantiate paginator callback
112 print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n";
114 // instantiate debug hooks if needed
116 $cb_init = $this->table_id."_init";
117 print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n";
118 $classname .= " sortinitiatedcallback-$cb_init";
119 $cb_comp = $this->table_id."_comp";
120 print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n";
121 $classname .= " sortcompletecallback-$cb_comp";
123 // start actual table
124 print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n";
126 if ($this->pagesize_area)
127 print $this->pagesize_area_html ();
128 if ($this->search_area)
129 print $this->search_area_html ();
132 print "<caption> $this->caption </caption>";
134 foreach ($this->headers as $label => $type) {
138 case "string": case "int": case "float":
139 $class="sortable"; break;
140 case ( strpos($type,"date-") == 0):
141 $class="sortable-" . $type; break;
143 $class="sortable-sort" . $type; break;
145 printf ('<th class="%s plekit_table">%s</th>',$class,$label);
148 print "</tr></thead><tbody>";
152 // for convenience, the options that apply to the bottom area can be passed here
153 // typically notes will add up to the ones provided so far, and to the default ones
154 // xxx default should be used only if applicable
155 function end ($options=NULL) {
156 $this->set_options($options);
157 print $this->bottom_html();
158 if ($this->notes_area)
159 print $this->notes_area_html();
163 function pagesize_area_html () {
164 $width=count($this->headers);
165 $pagesize_text_id = $this->table_id . "_pagesize";
167 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
168 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
169 <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
170 onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);'
171 size='3' maxlength='4' />
172 <label class='pagesize_label'> items/page </label>
173 <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
174 onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
175 </fieldset></form></td></tr>
181 function search_area_html () {
182 $width=count($this->headers);
183 $search_text_id = $this->table_id . "_search";
184 $search_reset_id = $this->table_id . "_search_reset";
185 $search_and_id = $this->table_id . "_search_and";
187 <tr class='search_area'><td class='search_area' colspan='$width'>
188 <div class='search'><fieldset>
189 <label class='search_label'> Search </label>
190 <input class='search_input' type='text' id='$search_text_id'
191 onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
192 size='$this->search_width' maxlength='256' />
194 <input id='$search_and_id' class='search_and'
195 type='checkbox' checked='checked'
196 onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
197 <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
198 onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
199 </fieldset></div></td></tr>
204 //////////////////// start a <tfoot> section
205 function tfoot_start () { print $this->tfoot_start_html(); }
206 function tfoot_start_html () {
207 $this->has_tfoot=true;
208 return "</tbody><tfoot>";
211 ////////////////////////////////////////
212 function bottom_html () {
214 if ($this->has_tfoot)
215 $result .= "</tfoot>";
217 $result .= "</tbody>";
218 $result .= "</table>\n";
222 ////////////////////////////////////////
223 function notes_area_html () {
225 array("Enter & or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes");
227 array ("Hold down the shift key to select multiple columns to sort");
233 $notes=array_merge($notes,$sort_notes);
234 if ($this->search_area)
235 $notes=array_merge($notes,$search_notes);
239 $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
240 foreach ($notes as $note)
241 $result .= "<br/>$note\n";
246 ////////////////////////////////////////
247 function row_start ($id=NULL,$class=NULL) {
249 if ( $id) print (" id=\"$id\"");
250 if ( $class) print (" class=\"$class\"");
254 function row_end () {
259 // supported options:
260 // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
265 public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
266 public function cell_html ($text,$options=NULL) {
267 if (isset ($options['only-if']) && ! $options['only-if'] )
271 $option=$options['class']; if ($option) $html .= " class='$option'";
272 $option=$options['columns'];if ($option) $html .= " colspan='$option'";
273 $option=$options['hfill']; if ($option) $html .= " colspan='" . $this->columns() . "'";
274 $option=$options['align']; if ($option) $html .= " style='text-align:$option'";
275 $html .= ">$text</td>";