| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # EditableTable Example 4 | ||
| 4 | # demonstrates the Horizontal table with a fairy complex dataset and rowspanning | ||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use CGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use CGI::Carp qw(fatalsToBrowser); | ||
| 9 | use Getopt::Std; | ||
| 10 | use HTML::EditableTable; | ||
| 11 | use HTML::EditableTable::Horizontal; | ||
| 12 | |||
| 13 | my @tableData = | ||
| 14 | ( | ||
| 15 | { | ||
| 16 | 'part_id' => 7765, | ||
| 17 | 'catalog_id' => 'UX35AT', | ||
| 18 | 'addition_date' => '2008-10-10', | ||
| 19 | 'part_name' => 'control module', | ||
| 20 | 'vendor' => 'Praxis', | ||
| 21 | 'description' => 'ABS package with revA firmware. Used in low-cost applications and as replacement for model UX34AT. Includes adaptor wiring harness for UX34AT', | ||
| 22 | 'part_options' => | ||
| 23 | [ | ||
| 24 | { | ||
| 25 | 'option_id' => 'O17G', | ||
| 26 | 'color' => 'green', | ||
| 27 | 'power_rating' => '17w', | ||
| 28 | }, | ||
| 29 | { | ||
| 30 | 'option_id' => 'O24R', | ||
| 31 | 'color' => 'red', | ||
| 32 | 'power_rating' => '24w', | ||
| 33 | } | ||
| 34 | ], | ||
| 35 | 'rohs_category' => 2, | ||
| 36 | 'reorder_class' => 'C', | ||
| 37 | 'last_order_date' => '2010-06-10', | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | 'part_id' => 7961, | ||
| 41 | 'catalog_id' => 'ZX42AT', | ||
| 42 | 'addition_date' => '2009-03-01', | ||
| 43 | 'part_name' => 'power regulator', | ||
| 44 | 'vendor' => 'Armscor', | ||
| 45 | 'description' => 'Minature power supply with redundant relays', | ||
| 46 | 'part_options' => | ||
| 47 | [ | ||
| 48 | { | ||
| 49 | 'option_id' => 'O3W', | ||
| 50 | 'color' => 'yellow', | ||
| 51 | 'power_rating' => '3w', | ||
| 52 | }, | ||
| 53 | { | ||
| 54 | 'option_id' => 'O4P', | ||
| 55 | 'color' => 'pink', | ||
| 56 | 'power_rating' => '4w', | ||
| 57 | } | ||
| 58 | ], | ||
| 59 | 'rohs_category' => 2, | ||
| 60 | 'reorder_class' => 'A', | ||
| 61 | 'last_order_date' => '2009-12-17', | ||
| 62 | }, | ||
| 63 | { | ||
| 64 | 'part_id' => 8055, | ||
| 65 | 'catalog_id' => 'UX24AT', | ||
| 66 | 'addition_date' => '2007-04-08', | ||
| 67 | 'part_name' => 'control module', | ||
| 68 | 'vendor' => 'Subarashii', | ||
| 69 | 'description' => 'Obsolete control module for A45 overthruster. Requires UX27AZ conditioner and 3F buffering caps if the overthruster runs >18psi', | ||
| 70 | 'part_options' => | ||
| 71 | [ | ||
| 72 | { | ||
| 73 | 'option_id' => 'O76B', | ||
| 74 | 'color' => 'blue', | ||
| 75 | 'power_rating' => '76w', | ||
| 76 | }, | ||
| 77 | { | ||
| 78 | 'option_id' => 'O98O', | ||
| 79 | 'color' => 'orange', | ||
| 80 | 'power_rating' => '98w', | ||
| 81 | }, | ||
| 82 | { | ||
| 83 | 'option_id' => 'Z103', | ||
| 84 | 'color' => 'red', | ||
| 85 | 'power_rating' => '103w', | ||
| 86 | }, | ||
| 87 | { | ||
| 88 | 'option_id' => 'Z120', | ||
| 89 | 'color' => 'pink', | ||
| 90 | 'power_rating' => '120w', | ||
| 91 | } | ||
| 92 | ], | ||
| 93 | 'rohs_category' => 4, | ||
| 94 | 'reorder_class' => 'A', | ||
| 95 | 'last_order_date' => '2005-08-19', | ||
| 96 | }, | ||
| 97 | ); | ||
| 98 | |||
| 99 | my @tableFields = | ||
| 100 | ( | ||
| 101 | { | ||
| 102 | 'editOnly' => 1, | ||
| 103 | 'formElement' => 'deleteRowButton', | ||
| 104 | }, | ||
| 105 | { | ||
| 106 | 'dbfield' => 'part_id', | ||
| 107 | 'label' => 'Part Id', | ||
| 108 | 'viewOnly' => 1, | ||
| 109 | 'callback'=> \&cbPartId, | ||
| 110 | }, | ||
| 111 | { | ||
| 112 | 'dbfield' => 'catalog_id', | ||
| 113 | 'label' => 'Catalog Id', | ||
| 114 | 'formElement' => 'textfield', | ||
| 115 | 'size' => 15, | ||
| 116 | 'uniquifierField' => 'part_id', | ||
| 117 | 'rowspanArrayKeyForUniquification' => 'part_options', | ||
| 118 | 'rowspanArrayUniquifier' => 'option_id', | ||
| 119 | }, | ||
| 120 | { | ||
| 121 | 'dbfield' => 'addition_date', | ||
| 122 | 'label' => 'Available From', | ||
| 123 | 'formElement' => 'calendar', | ||
| 124 | 'uniquifierField' => 'part_id', | ||
| 125 | }, | ||
| 126 | { | ||
| 127 | 'dbfield' => 'part_name', | ||
| 128 | 'label' => 'Part Name', | ||
| 129 | 'formElement' => 'textfield', | ||
| 130 | 'size' => 20, | ||
| 131 | 'uniquifierField' => 'part_id', | ||
| 132 | }, | ||
| 133 | { | ||
| 134 | 'dbfield' => 'vendor', | ||
| 135 | 'label' => 'Vendor', | ||
| 136 | 'formElement' => 'popup', | ||
| 137 | 'selectionList' => ['', 'Amexx', 'Armscor', 'Consolidated', 'Gentine', 'Oroco', 'Praxis', 'Shellalco', 'Subarashii',], | ||
| 138 | 'uniquifierField' => 'part_id', | ||
| 139 | }, | ||
| 140 | { | ||
| 141 | 'dbfield' => 'description', | ||
| 142 | 'label' => 'Part Description', | ||
| 143 | 'formElement' => 'textarea', | ||
| 144 | 'subBr' => 1, | ||
| 145 | 'drillDownTruncate' => 60, | ||
| 146 | 'uniquifierField' => 'part_id', | ||
| 147 | 'style' => "font-family:'Times New Roman';font-size:12px;", | ||
| 148 | }, | ||
| 149 | { | ||
| 150 | 'dbfield' => 'part_options', | ||
| 151 | 'rowspanArrayKey' => 'option_id', | ||
| 152 | 'label' => 'Option #', | ||
| 153 | }, | ||
| 154 | { | ||
| 155 | 'dbfield' => 'part_options', | ||
| 156 | 'rowspanArrayKey' => 'color', | ||
| 157 | 'uniquifierField' => 'part_id', | ||
| 158 | 'rowspanArrayUniquifier' => 'option_id', | ||
| 159 | 'label' => 'Color', | ||
| 160 | 'formElement' => 'popup', | ||
| 161 | 'selectionList' => ['blue', 'green', 'orange', 'pink', 'red', 'yellow'], | ||
| 162 | }, | ||
| 163 | { | ||
| 164 | 'dbfield' => 'part_options', | ||
| 165 | 'rowspanArrayKey' => 'power_rating', | ||
| 166 | 'uniquifierField' => 'part_id', | ||
| 167 | 'rowspanArrayUniquifier' => 'option_id', | ||
| 168 | 'label' => 'Power Rating', | ||
| 169 | 'formElement' => 'textfield', | ||
| 170 | 'minimalEditSize' => 1, | ||
| 171 | }, | ||
| 172 | { | ||
| 173 | 'dbfield' => 'rohs_category', | ||
| 174 | 'label' => 'RoHS', | ||
| 175 | 'formElement' => 'popup', | ||
| 176 | 'selectionList' => ['',1..10], | ||
| 177 | 'selectionLabels' => { | ||
| 178 | 1 => 'Large and small household appliances', | ||
| 179 | 2 => 'IT equipment', | ||
| 180 | 3 => 'Telecommunications equipment', | ||
| 181 | 4 => 'Consumer equipment', | ||
| 182 | 5 => 'Lighting equipment', | ||
| 183 | 6 => 'Electronic and electrical tools', | ||
| 184 | 7 => 'Toys, leisure, and sports equipment', | ||
| 185 | 8 => 'Medical devices', | ||
| 186 | 9 => 'Monitoring and control instruments', | ||
| 187 | 10 => 'Automatic dispensers', | ||
| 188 | }, | ||
| 189 | 'uniquifierField' => 'part_id', | ||
| 190 | }, | ||
| 191 | { | ||
| 192 | 'dbfield' => 'reorder_class', | ||
| 193 | 'label' => 'Reorder Class', | ||
| 194 | 'formElement' => 'popup', | ||
| 195 | 'selectionList' => ['', 'A', 'B', 'C'], | ||
| 196 | 'uniquifierField' => 'part_id', | ||
| 197 | }, | ||
| 198 | { | ||
| 199 | 'dbfield' => 'last_order_date', | ||
| 200 | 'label' => 'Last Ordered', | ||
| 201 | 'formElement' => 'calendar', | ||
| 202 | 'uniquifierField' => 'part_id', | ||
| 203 | }, | ||
| 204 | ); | ||
| 205 | |||
| 206 | ######## CGI Controller ########## | ||
| 207 | |||
| 208 | our $t = CGI->new(); | ||
| 209 | print $t->header(); | ||
| 210 | |||
| 211 | print "<h3>" . "Example using EditableTable::Horizontal" . "</h3>"; | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | my $context = $t->param('context') || 'view'; | ||
| 216 | |||
| 217 | # might be getting a context from command-line if the script is being run in the test suite | ||
| 218 | my %opts = (); | ||
| 219 | getopts('c:', \%opts); | ||
| 220 | |||
| 221 | if ($opts{c}) { $context = $opts{c}; } | ||
| 222 | |||
| 223 | my $tabIndex = 100; | ||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | my $table = HTML::EditableTable::Horizontal->new | ||
| 228 | ( | ||
| 229 | { | ||
| 230 | 'tableFields' => \@tableFields, | ||
| 231 | 'width' => '100%', | ||
| 232 | 'jsAddData' => 1, | ||
| 233 | 'editMode' => $context, | ||
| 234 | 'data' => \@tableData, | ||
| 235 | 'tabindex' => \$tabIndex, | ||
| 236 | 'title' => "title test", | ||
| 237 | 'sortHeader' => "example_4.cgi?", # server side sort url | ||
| 238 | 'sortData' => 1, # let EditableTable do the sort rather than assume it is done by the caller (in SQL, typically) | ||
| 239 | } | ||
| 240 | ); | ||
| 241 | |||
| 242 | print "<form method=post>"; | ||
| 243 | |||
| 244 | $table->htmlDisplay(); | ||
| 245 | |||
| 246 | my $nextContext = $context eq 'view' ? 'edit' : 'view'; | ||
| 247 | |||
| 248 | print "<input type=submit name=context value=$nextContext>"; | ||
| 249 | print "</form>"; | ||
| 250 | |||
| 251 | ######## TABLE CALLBACKS ########## | ||
| 252 | |||
| 253 | sub cbPartId { | ||
| 254 | my ($row, $colSpec, $editMode, $rowspanSubcounter) = shift @_; | ||
| 255 | return "<a href=example_4.cgi?context=view&uid=$row->{part_id}>" . $row->{part_id} . "</a>"; | ||
| 256 | |||
| 257 | } | ||
| 258 | |||
| 259 | |||