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/paginate.js"></script>
11 <script type="text/javascript" src="/plekit/table/table.js"></script>
12 <link href="/plekit/table/table.css" rel="stylesheet" type="text/css" />
15 ////////////////////////////////////////
16 // table_id: <table>'s id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code
17 // headers: an associative array "label"=>"type"
18 // sort_column: the column to sort on at load-time - set to negative number for no onload- sorting
19 // options : an associative array to override options
20 // - bullets1 : set to true if you want decorative bullets in column 1 (need white background)
21 // - stripes : use diferent colors for odd and even rows
22 // - caption : a caption for the table -- never used I'm afraid
23 // - search_area : boolean (default true)
24 // - pagesize_area : boolean (default true)
25 // - notes_area : boolean (default true)
26 // - search_width : size in chars of the search text dialog
27 // - pagesize: the initial pagination size
28 // - pagesize_def: the page size when one clicks the pagesize reset button
29 // - max_pages: the max number of pages to display in the paginator
30 // - notes : an array of additional notes
31 // - debug: enables debug callbacks (prints out on console.log)
39 var $bullets1; // boolean - default false - display decorative bullets in column 1
40 var $stripes; // boolean - default true - use different colors for odd and even rows
41 var $caption; // string - never used so far
42 var $search_area; // boolean (default true)
43 var $pagesize_area; // boolean (default true)
44 var $notes_area; // boolean (default true)
45 var $search_width; // size in chars of the search text dialog
46 var $pagesize; // the initial pagination size
47 var $pagesize_def; // the page size when one clicks the pagesize reset button
48 var $max_pages; // the max number of pages to display in the paginator
49 var $notes; // an array of additional notes
50 var $debug; // set to true for enabling various log messages on console.log
55 function PlekitTable ($table_id,$headers,$sort_column,$options=NULL) {
56 $this->table_id = $table_id;
57 $this->headers = $headers;
58 $this->sort_column = $sort_column;
60 $this->bullets1 = true;
63 $this->search_area = true;
64 $this->pagesize_area = true;
65 $this->notes_area = true;
66 $this->search_width = 40;
68 $this->pagesize_def = 999;
69 $this->max_pages = 10;
70 $this->notes = array();
73 $this->set_options ($options);
76 $this->has_tfoot=false;
79 function set_options ($options) {
82 if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
83 if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
84 if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
85 if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
86 if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
87 if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
88 if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
89 if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
90 if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
91 if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
92 if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
93 if (array_key_exists('debug',$options)) $this->debug=$options['debug'];
96 public function columns () {
97 return count ($this->headers);
101 public function start () {
102 $paginator=$this->table_id."_paginator";
103 $classname="paginationcallback-".$paginator;
104 $classname.=" max-pages-" . $this->max_pages;
105 $classname.=" paginate-" . $this->pagesize;
106 if ($this->bullets1) { $classname .= " bullets1"; }
107 if ($this->stripes) { $classname .= " rowstyle-alt"; }
108 if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; }
110 // instantiate paginator callback
111 print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n";
113 // instantiate debug hooks if needed
115 $cb_init = $this->table_id."_init";
116 print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n";
117 $classname .= " sortinitiatedcallback-$cb_init";
118 $cb_comp = $this->table_id."_comp";
119 print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n";
120 $classname .= " sortcompletecallback-$cb_comp";
122 // start actual table
123 print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n";
125 if ($this->pagesize_area)
126 print $this->pagesize_area_html ();
127 if ($this->search_area)
128 print $this->search_area_html ();
131 print "<caption> $this->caption </caption>";
133 foreach ($this->headers as $label => $type) {
137 case "string": case "int": case "float":
138 $class="sortable"; break;
139 case ( strpos($type,"date-") == 0):
140 $class="sortable-" . $type; break;
142 $class="sortable-sort" . $type; break;
144 printf ('<th class="%s plekit_table">%s</th>',$class,$label);
147 print "</tr></thead><tbody>";
151 // for convenience, the options that apply to the bottom area can be passed here
152 // typically notes will add up to the ones provided so far, and to the default ones
153 // xxx default should be used only if applicable
154 function end ($options=NULL) {
155 $this->set_options($options);
156 print $this->bottom_html();
157 if ($this->notes_area)
158 print $this->notes_area_html();
162 function pagesize_area_html () {
163 $width=count($this->headers);
164 $pagesize_text_id = $this->table_id . "_pagesize";
166 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
167 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
168 <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
169 onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);'
170 size='3' maxlength='3' />
171 <label class='pagesize_label'> items/page </label>
172 <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
173 onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
174 </fieldset></form></td></tr>
180 function search_area_html () {
181 $width=count($this->headers);
182 $search_text_id = $this->table_id . "_search";
183 $search_reset_id = $this->table_id . "_search_reset";
184 $search_and_id = $this->table_id . "_search_and";
186 <tr class='search_area'><td class='search_area' colspan='$width'>
187 <div class='search'><fieldset>
188 <label class='search_label'> Search </label>
189 <input class='search_input' type='text' id='$search_text_id'
190 onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
191 size='$this->search_width' maxlength='256' />
193 <input id='$search_and_id' class='search_and'
194 type='checkbox' checked='checked'
195 onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
196 <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
197 onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
198 </fieldset></div></td></tr>
203 //////////////////// start a <tfoot> section
204 function tfoot_start () { print $this->tfoot_start_html(); }
205 function tfoot_start_html () {
206 $this->has_tfoot=true;
207 return "</tbody><tfoot>";
210 ////////////////////////////////////////
211 function bottom_html () {
213 if ($this->has_tfoot)
214 $result .= "</tfoot>";
216 $result .= "</tbody>";
217 $result .= "</table>\n";
221 ////////////////////////////////////////
222 function notes_area_html () {
224 array("Enter & or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes");
226 array ("Hold down the shift key to select multiple columns to sort");
232 $notes=array_merge($notes,$sort_notes);
233 if ($this->search_area)
234 $notes=array_merge($notes,$search_notes);
238 $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
239 foreach ($notes as $note)
240 $result .= "<br/>$note\n";
245 ////////////////////////////////////////
246 function row_start ($id=NULL,$class=NULL) {
248 if ( $id) print (" id=\"$id\"");
249 if ( $class) print (" class=\"$class\"");
253 function row_end () {
258 // supported options:
259 // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
264 public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
265 public function cell_html ($text,$options=NULL) {
266 if (isset ($options['only-if']) && ! $options['only-if'] )
270 $option=$options['class']; if ($option) $html .= " class='$option'";
271 $option=$options['columns'];if ($option) $html .= " colspan='$option'";
272 $option=$options['hfill']; if ($option) $html .= " colspan='" . $this->columns() . "'";
273 $option=$options['align']; if ($option) $html .= " style='text-align:$option'";
274 $html .= ">$text</td>";