Quiq/JQuery/DataTable.pm

filter: Statementstmt 73.6% 81 / 110 Branchbran 44.4% 24 / 54 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 12 / 14 Podpod 100% 4 / 4 totaltotal 65.9% 122 / 185 CC 34 SCAR 34.9
CC
34
Cov
64%
instantiate
59.2
html
19.8
__ANON__
12.0
CRAP 32.7
« Quiq/Dumper.pm ↑ Summary Quiq/ExportFile.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::JQuery::DataTable - Erzeuge eine HTML/JavaScript DataTables-Tabelle
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Die Klasse liefert den HTML- und JavaScript-Code für ein
16 DataTable-Widget. Hompage des DataTables-Plugin:
17 L<https://datatables.net/>
18  
19 =head1 ATTRIBUTES
20  
21 Attribute, die bei der Instantiierung des JavaScript DataTable-Objekts
22 gesetzt werden, sind B<fett> hervorgehoben:
23  
24 =over 4
25  
26 =item B<< dom => $dom >> (Default: 't')
27  
28 Definiert die Bedienelemente der Tabelle. Siehe:
29 L<https://datatables.net/reference/option/dom>
30  
31 =item B<< emptyTableMsg => $str >> (Default: 'No data')
32  
33 Text, der angezeigt wird, wenn die Tabelle keine Daten enthält.
34 Siehe (auch für weitere sprachabhängige Texte):
35 L<https://datatables.net/reference/option/language>
36  
37 =item B<< info => $bool >> (Default: 0)
38  
39 Zeige Information über den Tabelleninhalt an. Siehe:
40 L<https://datatables.net/reference/option/info>
41  
42 =item $jsCode => $javaScript
43  
44 JavaScript-Code der nach der Instantiierung des DataTable-Objektes
45 hinzugefügt wird.
46  
47 =item B<< order => \@arrOfArr >> (Default: [])
48  
49 Initiale Sortierung der Tabelle. Siehe:
50 L<https://datatables.net/reference/option/order>
51  
52 =item B<< orderClasses => $bool >> (Default: 1)
53  
54 Hebe die Sortierkolumnen hervor. Siehe:
55 L<https://datatables.net/reference/option/orderClasses>
56  
57 =item B<< paging => $bool >> (Default: 0)
58  
59 Schalte Paginierung ein. Siehe:
60 L<https://datatables.net/reference/option/paging>
61  
62 =item B<< searchLabel => $str >> (Default: 'Search:')
63  
64 Text, der vor dem Suchfeld angezeigt wird (falls vorhanden)
65 Siehe (auch für weitere sprachabhängige Texte):
66 L<https://datatables.net/reference/option/language>
67  
68 =item B<< zeroRecordsMsg => $str >> (Default: 'No matching records found')
69  
70 Text, der angezeigt wird, wenn die Suche keine Daten geliefert hat.
71 Siehe (auch für weitere sprachabhängige Texte):
72 L<https://datatables.net/reference/option/language>
73  
74 =item B<< columns => \@columns >> (Default: [])
75  
76 Referenz auf eine Liste mit Kolumnen-Spezifikationen. Eine einzelne
77 Kolumnen-Spezifikation ist ein Hash mit den Komponenten:
78  
79 {
80 name => $name, # interner Name, insbes. f. Wert-Lookup
81 title => $title, # Kolumnenüberschrift
82 type => $type, # DataTables-Kolumnentyp (s. Link unten)
83 align => $align, # 'left'|'center'|'right' (Default: 'left')
84 orderable => $bool, # 0|1 (Default: 1)
85 searchable => $bool, # 0|1 (Default: 1)
86 visible => $bool, # 0|1 (Default: 1)
87 width => $width, # s. DtaTables Doku
88 }
89  
90 Nicht benötigte Komponenten können weggelassen werden.
91  
92 Mögliche Werte für $type: 'date', 'num', 'num-fmt', 'html-num',
93 'html-num-fmt', 'html', 'string'. Siehe
94 L<https://datatables.net/reference/option/columns.type>
95  
96 =item allowHtml => $bool|\@titles (Default: 0)
97  
98 Erlaube HTML insgesamt oder auf den Kolumnen in @titles,
99 d.h. ersetze die Werte der Kolumnen &, <, > I<nicht> automatisch
100 durch HTML-Entities.
101  
102 =item class => $class (Default: "compact stripe hover cell-border nowrap \
103  
104 order-column")
105 CSS-Klasse der DataTable (des Table-Elements).
106  
107 =item fixedHeader => $bool (Default: 0)
108  
109 Aktiviere FixedHeader. In dem Fall müssen zusätzlich die FixedHrader
110 CSS- und JS-Resourcen geladen werden.
111  
112 =item footer => $bool (Default: 0)
113  
114 Setze die Titel auch als Footer.
115  
116 =item id => $id (Default: undef)
117  
118 DOM-Id der DataTable (des Table-Elements).
119  
120 =item rowsAreArrays => $bool (Default: 0)
121  
122 Die Row-Objekte sind einfache Arrays. Als rowCallback wird
123 (per Default) verwendet:
124  
125 rowCallback => sub {
126 my ($row,$i,$columnA) = @_;
127 return (undef,@$row);
128 }
129  
130 Wenn nicht gesetzt, wird (per Default) als rowCallback verwendet:
131  
132 rowCallback => sub {
133 my ($row,$i,$columnA) = @_;
134
135 my @arr;
136 for my $col (@$columnA) {
137 my $name = $col->name;
138 push @arr,$name? $row->get($name): undef;
139 }
140
141 return (undef,@arr);
142 };
143  
144 =item rowCallback => $sub (Default: s. Attribut rowsAreArrays)
145  
146 Referenz auf eine Subroutine, die für jedes Row-Objekt die
147 darzustellende Zeileninformation (für tr- und td-Tag) liefert
148 (siehe Quiq::Html::Table::List). Der Default hängt vom
149 Wert des Attributs rowsAreArrays ab. Siehe dort.
150  
151 =item rows => \@rows (Default: [])
152  
153 Liste der Row-Objekte. Für jedes Element wird die Callback-Methode
154 (Attribut rowCallback) aufgerufen.
155  
156 =back
157  
158 =head1 EXAMPLE
159  
160 Default-Aussehen einer DataTable:
161  
162 =begin html
163  
164 <p class="sdoc-fig-p">
165 <img class="sdoc-fig-img" src="https://raw.github.com/s31tz/Quiq/master/img/quiq-jquery-datatable-example01.png" alt="" />
166 </p>
167  
168 =end html
169  
170 Das Programm
171  
172 my $h = Quiq::Html::Producer->new;
173
174 my $tab = Quiq::Database::Row::Object->makeTable(
175 [qw/per_id per_vorname per_nachname per_geburtsdatum/],
176 qw/1 Rudi Ratlos 1971-04-23/,
177 qw/2 Harry Hirsch 1948-07-22/,
178 qw/3 Susi Sorglos 1992-10-23/,
179 qw/4 Axel Nässe 1985-04-05/,
180 );
181
182 my $html = Quiq::Html::Page->html($h,
183 load => [
184 'https://code.jquery.com/jquery-latest.min.js',
185 'https://cdn.datatables.net/v/dt/dt-1.11.3/datatables.min.css',
186 'https://cdn.datatables.net/v/dt/dt-1.11.3/datatables.min.js',
187 ],
188 styleSheet => q~
189 body {
190 font-family: sans-serif;
191 font-size: 12pt;
192 max-width: 500px;
193 }
194 ~,
195 body => Quiq::JQuery::DataTable->html($h,
196 id => 'personTable',
197 order => [[2,'asc']],
198 columns => [
199 {
200 name => 'per_id',
201 title => 'Id',
202 align => 'right',
203 },{
204 name => 'per_vorname',
205 title => 'Vorname',
206 },{
207 name => 'per_nachname',
208 title => 'Nachname',
209 },{
210 name => 'per_geburtsdatum',
211 title => 'Geburtstag',
212 align => 'center',
213 },
214 ],
215 rows => scalar $tab->rows,
216 ),
217 );
218  
219 erzeugt den HTML-Code (lange Zeilen umbrochen)
220  
221 <!DOCTYPE html>
222
223 <html>
224 <head>
225 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
226 <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.11.3/datatables.min.css" />
227 <script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
228 <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.11.3/datatables.min.js"> </script>
229 <style type="text/css">
230 body {
231 font-family: sans-serif;
232 font-size: 12pt;
233 max-width: 500px;
234 }
235 </style>
236 </head>
237 <body>
238 <table class="compact stripe hover cell-border nowrap order-column" id="personTable" cellspacing="0">
239 <thead>
240 <tr>
241 <th>Id</th>
242 <th>Vorname</th>
243 <th>Nachname</th>
244 <th>Geburtstag</th>
245 </tr>
246 </thead>
247 <tbody>
248 <tr>
249 <td>1</td>
250 <td>Rudi</td>
251 <td>Ratlos</td>
252 <td>1971-04-23</td>
253 </tr>
254 <tr>
255 <td>2</td>
256 <td>Harry</td>
257 <td>Hirsch</td>
258 <td>1948-07-22</td>
259 </tr>
260 <tr>
261 <td>3</td>
262 <td>Susi</td>
263 <td>Sorglos</td>
264 <td>1992-10-23</td>
265 </tr>
266 <tr>
267 <td>4</td>
268 <td>Axel</td>
269 <td>Nässe</td>
270 <td>1985-04-05</td>
271 </tr>
272 </tbody>
273 </table>
274 <script type="text/javascript">
275 var dt = $('#personTable').DataTable({
276 dom: 't',
277 info: false,
278 order: [[2,'asc']],
279 orderClasses: true,
280 paging: false,
281 columns: [{className:'dt-right'},{className:'dt-left'},{className:'dt-left'},{className:'dt-center'}],
282 });
283 $('#personTable').show();
284 </script>
285 </body>
286 </html>
287  
288 =cut
289  
290 # -----------------------------------------------------------------------------
291  
292 package Quiq::JQuery::DataTable;
293 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
294  
295 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
296 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
297 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
298  
299 1 our $VERSION = '1.240';
300  
301 1 ▶use Quiq::Html::Table::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
302 1 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
303 1 ▶use Quiq::Json::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
304 1 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
305  
306 # -----------------------------------------------------------------------------
307  
308 =head1 METHODS
309  
310 =head2 Konstruktor
311  
312 =head3 new() - Instantiiere Objekt
313  
314 =head4 Synopsis
315  
316 $obj = $class->new(@keyVal);
317  
318 =head4 Description
319  
320 Instantiiere eine DataTable in Perl und liefere eine Referenz auf
321 dieses Objekt zurück.
322  
323 =cut
324  
325 # -----------------------------------------------------------------------------
326  
327 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
328 2 my $class = shift;
329 # @_: @keyVal
330  
331 2 my $self = $class->SUPER::new(
332 # DataTable-Attribute
333 dom => 't',
334 emptyTableMsg => undef,
335 info => 0,
336 jsCode => undef,
337 order => [],
338 orderClasses => 1,
339 paging => 0,
340 searchLabel => undef,
341 zeroRecordsMsg => undef,
342 # HTML-Attribute
343 allowHtml => 0,
344 class => 'compact stripe hover cell-border nowrap order-column',
345 columns => [],
346 fixedHeader => 0,
347 footer => 0,
348 id => undef,
349 rowsAreArrays => 0,
350 rowCallback => undef,
351 rows => [],
352 );
353 2 $self->set(@_);
354  
355 2 return $self;
356 }
357  
358 # -----------------------------------------------------------------------------
359  
360 =head2 Code-Generierung
361  
362 =head3 html() - Generiere HTML
363  
364 =head4 Synopsis
365  
366 $html = $obj->html($h);
367 $html = $class->html($h,@keyVal);
368  
369 =head4 Description
370  
371 Generiere den HTML-Code des DataTable-Objekts und liefere
372 diesen zurück. Als Klassenmethode gerufen, wird das Objekt intern
373 mit den Attributen @keyVal instantiiert.
374  
375 =cut
376  
377 # -----------------------------------------------------------------------------
378  
379 2 ▶sub html {
Subroutine html: called CC=9 SCAR=29.8
Pod documented
380 2 my $this = shift;
381 2 my $h = shift;
382  
383 2 ▶ my $self = ref $this? $this: $this->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 2 0 2
384  
385 2 my ($allowHtml,$class,$footer,$id,$rowsAreArrays,
386 $rowCallback,$rowA) = $self->get(qw/allowHtml class footer id
387 rowsAreArrays rowCallback rows/);
388  
389 # Liste der Kolumnendefinitionen als Hash-Objekte
390 2 my @columns = $self->getColumns;
391  
392 2 ▶ if (!$rowCallback) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rowCallback) 1 1 2
393 1 ▶ if ($rowsAreArrays) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowsAreArrays) { } 0 1 1
394 0 ▶ $rowCallback = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 0 my ($row,$i,$columnA) = @_;
396 0 return (undef,@$row);
397 0 };
398 }
399 else {
400 0 ▶ $rowCallback = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
401 0 my ($row,$i,$columnA) = @_;
402  
403 0 my @arr;
404 0 for my $col (@$columnA) {
405 0 my $name = $col->name;
406 0 ▶ push @arr,$name? $row->get($name): undef;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
407 }
408  
409 0 return (undef,@arr);
410 1 };
411 }
412 };
413  
414 # HTML generieren
415  
416 2 ▶ if (!@columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@columns) 1 1 2
417 1 return '';
418 }
419  
420 1 my @titles;
421 1 for my $col (@columns) {
422 2 push @titles,$col->title;
423 }
424  
425 1 ▶ if (ref $allowHtml) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $allowHtml) 0 1 1
426 0 my %column = map {$_->name => $_} @columns;
427 0 my @arr;
428 0 for my $name (@$allowHtml) {
429 0 my $col = $column{$name};
430 0 ▶ if (!$col) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col) 0 0 0
431 0 $self->throw(
432 'DATATABLE-00001: Column does not exist',
433 Column => $name,
434 );
435 }
436 0 push @arr,$col->title;
437 }
438 0 $allowHtml = \@arr;
439 }
440  
441 1 my $html = Quiq::Html::Table::List->html($h,
442 allowHtml => $allowHtml,
443 border => 0,
444 class => $class,
445 empty => undef,
446 id => $id,
447 rowCallback => $rowCallback,
448 rowCallbackArguments => [\@columns],
449 rows => $rowA,
450 titles => \@titles,
451 footer => $footer,
452 );
453  
454 # Instantiiere DataTable-Objekt
455  
456 1 $html .= $h->tag('script',
457 $self->instantiate,
458 );
459  
460 1 return $html;
461 }
462  
463 # -----------------------------------------------------------------------------
464  
465 =head2 Hilfsmethoden
466  
467 =head3 getColumns() - Liste der Kolumnendefinitionen
468  
469 =head4 Synopsis
470  
471 @columns | $columns = $e->getColumns;
472  
473 =head4 Description
474  
475 Liefere die Liste der Kolumnendefinitionen. Die Kolumnen werden
476 beim Setzen des Objektattributs columns als einfache Hashes
477 angegeben. Diese Methode liefert die Kolumnen-Definitionen als
478 Hash-Objekte (vom Typ Quiq::Hash).
479  
480 =cut
481  
482 # -----------------------------------------------------------------------------
483  
484 3 ▶sub getColumns {
Subroutine getColumns: called CC=3 SCAR=11.0
Pod documented
485 3 my $self = shift;
486  
487 3 my ($columnA) = $self->get(qw/columns/);
488  
489 # Column-Hashes in Objekte wandeln. Die Attributnamen werden
490 # dabei auf Korrektheit geprüft.
491  
492 3 my @columns;
493 3 for my $h (@$columnA) {
494 4 push @columns,Quiq::Hash->new([qw/
495 name
496 title
497 type
498 align
499 orderable
500 render
501 searchable
502 visible
503 width
504 /])->join($h);
505 }
506  
507 3 ▶ return wantarray? @columns: \@columns;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
508 }
509  
510 # -----------------------------------------------------------------------------
511  
512 =head3 instantiate() - Instantiiere Widget in JavaScript
513  
514 =head4 Synopsis
515  
516 $javaScript = $e->instantiate;
517  
518 =head4 Description
519  
520 Liefere den JavaScript-Code, der das DataTables-Objekt in JavaScript
521 instantiiert. Aufbau:
522  
523 jQuery('#ID').DataTable({
524 <JSON-Code>,
525 columns: [
526 <Kolumnen-Definitionen>
527 ]
528 });
529  
530 =cut
531  
532 # -----------------------------------------------------------------------------
533  
534 1 ▶sub instantiate {
Subroutine instantiate: called CC=22 SCAR=40.8
Pod documented
535 1 my ($self,$json) = @_;
536  
537 1 my ($id,$dom,$emptyTableMsg,$fixedHeader,$footer,$info,$jsCode,$orderA,
538 $orderClasses,$paging,$searchLabel,$zeroRecordsMsg) =
539 $self->get(qw/id dom emptyTableMsg fixedHeader footer info jsCode order
540 orderClasses paging searchLabel zeroRecordsMsg/);
541  
542 1 my $j = Quiq::Json::Code->new;
543  
544 1 my @language;
545 1 ▶ if ($emptyTableMsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($emptyTableMsg) 0 1 1
546 0 push @language,emptyTable=>$emptyTableMsg;
547 }
548 1 ▶ if ($searchLabel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($searchLabel) 0 1 1
549 0 push @language,search=>$searchLabel;
550 }
551 1 ▶ if ($zeroRecordsMsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zeroRecordsMsg) 0 1 1
552 0 push @language,zeroRecords=>$zeroRecordsMsg;
553 }
554  
555 1 ▶ my @prop = (
Branch: 4 branches
BranchTrueFalseTotal
$info ? : 0 1 1
$orderClasses ? : 1 0 1
$paging ? : 0 1 1
!@language ? : 1 0 1
556 dom => $dom,
557 info => $info? \'true': \'false',
558 order => $orderA,
559 orderClasses => $orderClasses? \'true': \'false',
560 paging => $paging? \'true': \'false',
561 !@language? (): (language=>{@language}),
562 );
563  
564 1 my @columns;
565 1 for my $col ($self->getColumns) {
566 2 my %col;
567 2 ▶ if (my $type = $col->type) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $col->type) 0 2 2
568 0 $col{'type'} = $type;
569 }
570 2 ▶ if (my $align = $col->align || 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if (my $align = $col->align || "left") 2 0 2
Condition50%1/2
$col->align || 'left'
l!l
02
MC/DC0%0/1
$col->align || 'left'
$col->align
Truth table50%1/2
A
A= $col->align
Aresult
11
00
571 2 $col{'className'} = "dt-$align";
572 }
573 2 my $searchable = $col->searchable;
574 2 ▶ if (defined $searchable) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $searchable) 0 2 2
575 0 $col{'searchable'} = $searchable;
576 }
577 2 my $orderable = $col->orderable;
578 2 ▶ if (defined $orderable) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $orderable) 0 2 2
579 0 ▶ $col{'orderable'} = $orderable? \'true': \'false';
Branch: 1 branch
BranchTrueFalseTotal
$orderable ? : 0 0 0
580 }
581 2 my $visible = $col->visible;
582 2 ▶ if (defined $visible) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $visible) 0 2 2
583 0 ▶ $col{'visible'} = $visible? \'true': \'false';
Branch: 1 branch
BranchTrueFalseTotal
$visible ? : 0 0 0
584 }
585 2 ▶ if (my $width = $col->width) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $width = $col->width) 0 2 2
586 0 $col{'width'} = $width;
587 }
588 2 ▶ if (my $render = $col->render) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $render = $col->render) 0 2 2
589 0 $col{'render'} = \$render;
590 }
591 2 push @columns,\%col;
592 }
593 1 push @prop,columns=>\@columns;
594  
595 1 my $js = sprintf q|var dt = $('#%s').DataTable(%s);|,
596 $id,scalar $j->object(@prop);
597  
598 1 ▶ if ($jsCode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jsCode) 0 1 1
599 0 $js .= "\n".Quiq::Unindent->string($jsCode);
600 }
601  
602 1 $js .= "\n".sprintf q|$('#%s').show();|,$id;
603  
604 1 ▶ if ($fixedHeader) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixedHeader) 0 1 1
605 0 ▶ $js .= "\n".sprintf q|new $.fn.dataTable.FixedHeader(dt,%s);|,
Branch: 1 branch
BranchTrueFalseTotal
!$footer ? : 0 0 0
606 scalar $j->object(
607 header => \'true',
608 !$footer? (): (footer => \'true'),
609 )
610 ;
611 }
612 1 return $js;
613 }
614  
615 # -----------------------------------------------------------------------------
616  
617 =head1 VERSION
618  
619 1.240
620  
621 =head1 AUTHOR
622  
623 Frank Seitz, L<http://fseitz.de/>
624  
625 =head1 COPYRIGHT
626  
627 Copyright (C) 2026 Frank Seitz
628  
629 =head1 LICENSE
630  
631 This code is free software; you can redistribute it and/or modify
632 it under the same terms as Perl itself.
633  
634 =cut
635  
636 # -----------------------------------------------------------------------------
637  
638 1 1;
639  
640 # eof
« Quiq/Dumper.pm ↑ Summary Quiq/ExportFile.pm »