Quiq/Html/Table/Simple.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 54 / 54 CC 7 SCAR 15.0
CC
7
Cov
100%
html
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
« Quiq/Html/Widget/TextField.pm ↑ Summary Quiq/Database/Row/Object/Table.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Table::Simple - HTML-Tabelle
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse dient der Generierung von HTML-Tabellen mit einer
16 freien Struktur, wie Formulartabellen. Die Definition erfolgt
17 zeilenweise wie in HTML, wobei die C<tr>- und C<td>-Information
18 durch Listen angegeben werden.
19  
20 Der Wert des Objekt-Attributs C<rows>, das den Inhalt der Tabelle
21 definiert, hat den Aufbau:
22  
23 [[@keyValTr,[@keyValTd,$content],...],...]
24 ^^ ^ ^ ^
25 || | | weitere Zeilen
26 || | weitere Kolumnen
27 || erste Kolumne
28 |erste Zeile mit Attributen @keyValTr
29 Array der Zeilen
30  
31 Die Listen C<@keyValTr> und C<@KeyValTd> definieren die Attribute
32 der C<tr>- bzw. C<td>-Tags. Besteht C<@keyValTr> aus einer
33 ungeraden Anzahl an Elementen, wird das erste Element C<$val> als
34 Klassenname interpretiert und zu C<< class=>$val >> expandiert.
35  
36 =head1 ATTRIBUTES
37  
38 =over 4
39  
40 =item border => $n (Default: undef)
41  
42 C<border>-Attribut der Tabelle.
43  
44 =item cellpadding => $n (Default: undef)
45  
46 C<cellpadding>-Attribut der Tabelle.
47  
48 =item cellspacing => $n (Default: 0)
49  
50 C<cellspacing>-Attribut der Tabelle.
51  
52 =item class => $class (Default: undef)
53  
54 C<class>-Attribut der Tabelle.
55  
56 =item data => \@keyVal (Default: [])
57  
58 C<data-*> Attribute der Tabelle.
59  
60 =item id => $id (Default: undef)
61  
62 Id der Tabelle.
63  
64 =item rows => \@rows (Default: [])
65  
66 Liste der Zeilen (und Kolumnen).
67  
68 =item style => $cssCode (Default: undef)
69  
70 C<style>-Attribut der Tabelle.
71  
72 =item width => $width (Default: undef)
73  
74 C<width>-Attribut der Tabelle.
75  
76 =back
77  
78 =head1 EXAMPLE
79  
80 Klasse:
81  
82 $html = Quiq::Html::Table::Simple->html($h,
83 class => 'my-table',
84 border => 1,
85 rows => [
86 ['my-title',['A'],[colspan=>2,'B']],
87 [[rowspan=>2,'a1'],['de'],['Text1_de']],
88 [['en'],['Text1_en']],
89 [[rowspan=>2,'a2'],['de'],['Text2_de']],
90 [['en'],['Text2_en']],
91 ],
92 );
93  
94 =over 2
95  
96 =item *
97  
98 tr-Angabe C<'my-title'> ist äquivalent zu C<< class=>'my-title' >>
99  
100 =item *
101  
102 mit -tag=>'th' wird aus einer beliebigen Zelle eine Head-Zelle
103 gemacht.
104  
105 =back
106  
107 Aussehen:
108  
109 +--+-----------+
110 |A |B |
111 +--+--+--------+
112 | |de|Text1_de|
113 |a1+--+--------+
114 | |en|Text1_en|
115 +--+--+--------+
116 | |de|Text2_de|
117 |a2+--+--------+
118 | |en|Text2_wn|
119 +--+--+--------+
120  
121 HTML:
122  
123 <table class="my-table" border="1" cellspacing="0">
124 <tr class="my-title">
125 <td>A</td>
126 <td colspan="2">B</td>
127 </tr>
128 <tr>
129 <td rowspan="2">a1</td>
130 <td>de</td>
131 <td>Text1_de</td>
132 </tr>
133 <tr>
134 <td>en</td>
135 <td>Text1_en</td>
136 </tr>
137 <tr>
138 <td rowspan="2">a2</td>
139 <td>de</td>
140 <td>Text2_de</td>
141 </tr>
142 <tr>
143 <td>en</td>
144 <td>Text2_en</td>
145 </tr>
146 </table>
147  
148 =cut
149  
150 # -----------------------------------------------------------------------------
151  
152 package Quiq::Html::Table::Simple;
153 5 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 5 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 5 our $VERSION = '1.240';
160  
161 # -----------------------------------------------------------------------------
162  
163 =head1 METHODS
164  
165 =head2 Konstruktor
166  
167 =head3 new() - Instantiiere Objekt
168  
169 =head4 Synopsis
170  
171 $e = $class->new(@keyVal);
172  
173 =head4 Description
174  
175 Instantiiere ein Tabellen-Objekt und liefere eine Referenz auf
176 dieses Objekt zurück.
177  
178 =cut
179  
180 # -----------------------------------------------------------------------------
181  
182 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
183 4 my $class = shift;
184 # @_: @keyVal
185  
186 4 my $self = $class->SUPER::new(
187 border => undef,
188 cellpadding => undef,
189 cellspacing => 0,
190 class => undef,
191 data => [],
192 id => undef,
193 rows => [],
194 style => undef,
195 width => undef,
196 );
197 4 $self->set(@_);
198  
199 4 return $self;
200 }
201  
202 # -----------------------------------------------------------------------------
203  
204 =head2 Objektmethoden
205  
206 =head3 html() - Generiere HTML
207  
208 =head4 Synopsis
209  
210 $html = $e->html($h);
211 $html = $class->html($h,@keyVal);
212  
213 =head4 Description
214  
215 Generiere den HTML-Code des Tabellen-Objekts und liefere diesen
216 zurück. Als Klassenmethode gerufen, wird das Objekt intern erzeugt
217 und mit den Attributen @keyVal instantiiert.
218  
219 =cut
220  
221 # -----------------------------------------------------------------------------
222  
223 4 ▶sub html {
Subroutine html: called CC=7 SCAR=19.5
Pod documented
224 4 my $this = shift;
225 4 my $h = shift;
226  
227 4 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 2 2 4
228  
229 4 my ($border,$cellpadding,$cellspacing,$class,$dataA,$id,$rowA,$style,
230 $width) = $self->get(qw/border cellpadding cellspacing class data
231 id rows style width/);
232  
233 return $h->tag('table',
234 class => $class,
235 id => $id,
236 style => $style,
237 data => $dataA,
238 border => $border,
239 width => $width,
240 cellpadding => $cellpadding,
241 cellspacing => $cellspacing,
242 $h->tag('tbody',
243 -ignoreTagIf => 1, # kein tbody per Default
244 4 do {
245 4 my $tbody;
246 4 for my $trA (@$rowA) {
247 # Elemente des Zeilen-Array auswerten
248  
249 10 my (@keyVal,@td);
250 10 ▶ for (my $i = 0; $i < @$trA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$trA) 20 10 30
251 20 my $e = $trA->[$i];
252 20 ▶ if (ref $e) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) { } 19 1 20
253 19 push @td,$e;
254 }
255 else {
256 1 push @keyVal,$e;
257 }
258 }
259 10 ▶ if (@keyVal == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keyVal == 1) 1 9 10
260 1 unshift @keyVal,'class';
261 }
262  
263 # HTML-Code der Zeile generieren und
264 # zum tbody hinzufügen
265  
266 $tbody .= $h->tag('tr',
267 @keyVal,
268 10 do {
269 10 my $tr;
270 10 for my $tdA (@td) {
271 19 $tr .= $h->tag('td',
272 @$tdA
273 );
274 }
275 10 $tr;
276 },
277 );
278 }
279 4 $tbody;
280 },
281 )
282 );
283 }
284  
285 # -----------------------------------------------------------------------------
286  
287 =head1 VERSION
288  
289 1.240
290  
291 =head1 AUTHOR
292  
293 Frank Seitz, L<http://fseitz.de/>
294  
295 =head1 COPYRIGHT
296  
297 Copyright (C) 2026 Frank Seitz
298  
299 =head1 LICENSE
300  
301 This code is free software; you can redistribute it and/or modify
302 it under the same terms as Perl itself.
303  
304 =cut
305  
306 # -----------------------------------------------------------------------------
307  
308 5 1;
309  
310 # eof
« Quiq/Html/Widget/TextField.pm ↑ Summary Quiq/Database/Row/Object/Table.pm »