DataTables.pm

filter: Statementstmt 24.1% 47 / 195 Branchbran 0.0% 0 / 74 Conditioncond 6.2% 2 / 32 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 77.7% 14 / 18 Podpod 100% 1 / 1 totaltotal 18.9% 64 / 337 CC 58 SCAR 65.1
CC
58
Cov
16%
_dt_ssp
1190.0
_decode_params
462.0
_dt_ssp_params
12.0
CRAP 669.6
↑ Summary DataTables/SSP/Params.pm »
1 package Mojolicious::Plugin::DataTables;
2  
3 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::JSON qw(decode_json encode_json true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::DOM::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::Util qw(dumper deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Mojolicious::Plugin::DataTables::SSP::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Mojolicious::Plugin::DataTables::SSP::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Mojolicious::Plugin::DataTables::SSP::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '2.01';
17  
18 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
19  
20 1 my ( $c, $app, $conf ) = @_;
21  
22 1 $app->helper( 'datatable_js' => \&_dt_js );
23 1 $app->helper( 'datatable_css' => \&_dt_css );
24 1 $app->helper( 'datatable.ssp' => \&_dt_ssp );
25 1 $app->helper( 'datatable.ssp_params' => \&_dt_ssp_params );
26 1 $app->helper( 'datatable.ssp_results' => \&_dt_ssp_results );
27  
28 }
29  
30 1 ▶sub _dt_js {
Subroutine _dt_js: called CC=2 SCAR=7.0
31  
32 1 my ( $c, $url ) = @_;
33  
34 1 my $dt_version = '1.10.24';
35 1 ▶ my $dt_js_url = $url || "//cdn.datatables.net/$dt_version/js/jquery.dataTables.min.js";
Condition50%1/2
$url || "//cdn.datatables.net/$dt_version/js/jquery.dataTables.min.js"
l!l
01
MC/DC0%0/1
$url || "//cdn.datatables.net/$dt_version/js/jquery.dataTables.min.js"
$url
Truth table50%1/2
A
A= $url
Aresult
11
00
36  
37 1 return _tag( 'script', 'src' => $dt_js_url );
38  
39 }
40  
41 1 ▶sub _dt_css {
Subroutine _dt_css: called CC=2 SCAR=7.0
42  
43 1 my ( $c, $url ) = @_;
44  
45 1 my $dt_version = '1.10.24';
46 1 ▶ my $dt_js_url = $url || "//cdn.datatables.net/$dt_version/css/jquery.dataTables.min.css";
Condition50%1/2
$url || "//cdn.datatables.net/$dt_version/css/jquery.dataTables.min.css"
l!l
01
MC/DC0%0/1
$url || "//cdn.datatables.net/$dt_version/css/jquery.dataTables.min.css"
$url
Truth table50%1/2
A
A= $url
Aresult
11
00
47  
48 1 return _tag( 'link', 'rel' => 'stylesheet', 'href' => $dt_js_url );
49  
50 }
51  
52 0 ▶sub _dt_ssp {
Subroutine _dt_ssp: not called CC=34 SCAR=70.8
53  
54 0 my ( $c, %args ) = @_;
55  
56 0 ▶ my $table = delete $args{table} || Carp::croak 'Missing table';
Condition0%0/2
delete $args{'table'} || Carp::croak('Missing table')
l!l
00
MC/DC0%0/1
delete $args{'table'} || Carp::croak('Missing table')
delete $args{'table'}
Truth table0%0/2
A
A= delete $args{'table'}
Aresult
11
00
57 0 my $options = delete $args{options};
58 0 my $columns = delete $args{columns};
59 0 my $debug = delete $args{debug};
60 0 my $where = delete $args{where};
61 0 my $sql = delete $args{sql};
62  
63 0 ▶ if ( defined( $args{db} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"db"}) 0 0 0
64  
65 0 deprecated '[Mojolicious::Plugin::DataTables] db is DEPRECATED in favor of sql';
66  
67 0 my $db = $args{db};
68  
69 0 ▶ $sql = $db->sqlite if ( ref $db eq 'Mojo::SQLite::Database' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $db eq "Mojo::SQLite::Database" 0 0 0
70 0 ▶ $sql = $db->pg if ( ref $db eq 'Mojo::Pg::Database' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $db eq "Mojo::Pg::Database" 0 0 0
71 0 ▶ $sql = $db->mysql if ( ref $db eq 'Mojo::mysql::Database' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $db eq "Mojo::mysql::Database" 0 0 0
72  
73 }
74  
75 0 my $log = $c->app->log->context('[DataTables]');
76  
77 0 my $regexp_operator = 'REGEXP'; # REGEXP operator for MySQL and SQLite
78  
79 0 my $sql_class = ref $sql;
80  
81 # "~" operator for PostgreSQL
82 0 ▶ if ( $sql_class =~ /Mojo::Pg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_class =~ /Mojo::Pg/u) 0 0 0
83 0 $regexp_operator = '~';
84 }
85  
86 0 my $ssp = $c->datatable->ssp_params($options);
87  
88 0 ▶ return {} if ( !$ssp->draw );
Branch: 1 branch
BranchTrueFalseTotal
unless $ssp->draw 0 0 0
89  
90 0 my @columns = $ssp->db_columns;
91  
92 0 ▶ if ($columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns) 0 0 0
93 0 ▶ if ( ref $columns eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $columns eq 'ARRAY') { } 0 0 0
94 0 push @columns, @{$columns};
95 } else {
96 0 push @columns, $columns;
97 }
98 }
99  
100 0 my $abstract = {
101 'where' => {
102 '-and' => [],
103 '-or' => [],
104 },
105 'order' => [],
106 'filter' => [],
107 };
108  
109 # Global filter
110 0 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 0 0 0
111 0 ▶ if ( ref $where eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq 'ARRAY') { } 0 0 0
112 0 my ( $where_stmt, @where_bind ) = @{$where};
113 0 foreach (@where_bind) {
114 0 $where_stmt =~ s/\?/'$_'/; # TODO improve
115 }
116 0 push @{ $abstract->{where}->{'-and'} }, { '-bool' => $where_stmt };
117 0 push @{ $abstract->{filter} }, { '-bool' => $where_stmt };
118 } else {
119 0 push @{ $abstract->{where}->{'-and'} }, { '-bool' => $where };
120 0 push @{ $abstract->{filter} }, { '-bool' => $where };
121 }
122 }
123  
124 # Column Search
125 0 foreach ( @{ $ssp->columns } ) {
126 0 ▶ if ( $_->database && $_->searchable != 0 && $_->search->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->database and $_->searchable != 0 and $_->search->{"value"}) 0 0 0
Condition0%0/6
$_->database and $_->searchable != 0 and $_->search->{"value"}
!ll&&!rl&&r
000
$_->database and $_->searchable != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$_->database and $_->searchable != 0 and $_->search->{"value"}
$_->database $_->searchable != 0 $_->search->{"value"}
Truth table0%0/4
A and B and C
A= $_->databaseB= $_->searchable != 0C= $_->search->{"value"}
ABCresult
0XX0
10X0
1100
1111
127  
128 0 ▶ if ( $_->search->{regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->search->{'regex'}) { } 0 0 0
129 0 push @{ $abstract->{where}->{'-and'} }, { $_->database => { $regexp_operator => $_->search->{value} } };
130 } else {
131 0 push @{ $abstract->{where}->{'-and'} },
132 0 { $_->database => { -like => '%' . $_->search->{value} . '%' } };
133 }
134  
135 }
136 }
137  
138 # Global Search
139 0 ▶ if ( $ssp->search->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssp->search->{"value"}) 0 0 0
140 0 foreach ( @{ $ssp->columns } ) {
141 0 ▶ if ( $_->database && $_->searchable != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->database and $_->searchable != 0) 0 0 0
Condition0%0/3
$_->database and $_->searchable != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_->database and $_->searchable != 0
$_->database $_->searchable != 0
Truth table0%0/3
A and B
A= $_->databaseB= $_->searchable != 0
ABresult
0X0
100
111
142  
143 0 ▶ if ( $ssp->search->{regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssp->search->{'regex'}) { } 0 0 0
144 0 push @{ $abstract->{where}->{'-or'} },
145 0 { $_->database => { $regexp_operator => $ssp->search->{value} } };
146 } else {
147 0 push @{ $abstract->{where}->{'-or'} },
148 0 { $_->database => { -like => '%' . $ssp->search->{value} . '%' } };
149 }
150 }
151 }
152 }
153  
154 # Order
155 0 ▶ if ( %{ $ssp->db_order } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$ssp->db_order;}) 0 0 0
156  
157 0 my $order = $ssp->db_order;
158  
159 0 foreach my $column ( keys %{$order} ) {
160 0 my $clausole = $order->{$column};
161 0 push @{ $abstract->{order} }, { "-$clausole" => $column };
162 }
163 }
164  
165 0 ▶ delete $abstract->{where}->{'-and'} if ( scalar @{ $abstract->{where}->{'-and'} } < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$abstract->{"where"}{"-and"};} < 1 0 0 0
166 0 ▶ delete $abstract->{where}->{'-or'} if ( scalar @{ $abstract->{where}->{'-or'} } < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$abstract->{"where"}{"-or"};} < 1 0 0 0
167  
168 0 my ( $stmt, @bind ) = $sql->abstract->select( $table, \@columns, $abstract->{where}, $abstract->{order} );
169  
170 0 $stmt .= sprintf ' LIMIT %s OFFSET %s', $ssp->length, $ssp->start; # TODO
171  
172 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
173 0 $log->debug("Query: $stmt");
174 0 $log->debug( "Bind: " . encode_json \@bind );
175 }
176  
177 0 my $query = $sql->db->query( $stmt, @bind );
178  
179 0 my @results = ();
180  
181 0 ▶ while ( my $row = $query->hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $query->hash) 0 0 0
182  
183 0 my $data = {};
184  
185 0 foreach my $column ( @{ $ssp->columns } ) {
186  
187 0 $column->row($row);
188  
189 0 ▶ my $col_db = $column->database || '';
Condition0%0/2
$column->database || ''
l!l
00
MC/DC0%0/1
$column->database || ''
$column->database
Truth table0%0/2
A
A= $column->database
Aresult
11
00
190 0 my $col_value = $row->{$col_db};
191  
192 0 ▶ if ( ref $column->formatter eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $column->formatter eq "CODE") 0 0 0
193 0 $col_value = $column->formatter->( $col_value, $column );
194 }
195  
196 0 $data->{ $column->data } = $col_value;
197  
198 }
199  
200 0 push @results, $data;
201  
202 }
203  
204 0 my ( $stmt_total, @bind_total ) = $sql->abstract->select( $table, [ \'COUNT(*) AS tot' ], $abstract->{filter} );
205 0 my ( $stmt_filter, @bind_filter ) = $sql->abstract->select( $table, [ \'COUNT(*) AS tot' ], $abstract->{where} );
206  
207 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
208 0 $log->debug("Query Total: $stmt_total");
209 0 $log->debug( "Bind Total: " . encode_json \@bind_total );
210 0 $log->debug("Query Filtered: $stmt_filter");
211 0 $log->debug( "Bind Filtered: " . encode_json \@bind_filter );
212 }
213  
214 0 my $total = $sql->db->query( $stmt_total, @bind_total )->hash->{tot};
215 0 my $filtered = $total;
216  
217 0 ▶ if (@bind_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind_filter) 0 0 0
218 0 $filtered = $sql->db->query( $stmt_filter, @bind_filter )->hash->{tot};
219 }
220  
221 0 my $ssp_results = Mojolicious::Plugin::DataTables::SSP::Results->new(
222 draw => $ssp->draw,
223 data => \@results,
224 records_total => $total,
225 records_filtered => $filtered
226 );
227  
228 0 return $ssp_results;
229  
230 }
231  
232 0 ▶sub _dt_ssp_params {
Subroutine _dt_ssp_params: not called CC=3 SCAR=24.8
233  
234 0 my ( $c, $dt_options ) = @_;
235  
236 0 my $req_params = {};
237 0 ▶ $req_params = $c->req->query_params if ( $c->req->{method} eq 'GET' );
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->{"method"} eq "GET" 0 0 0
238 0 ▶ $req_params = $c->req->body_params if ( $c->req->{method} eq 'POST' );
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->{"method"} eq "POST" 0 0 0
239  
240 0 return Mojolicious::Plugin::DataTables::SSP::Params->new( %{ _decode_params( $req_params, $dt_options ) } );
241  
242 }
243  
244 0 ▶sub _dt_ssp_results {
Subroutine _dt_ssp_results: not called CC=1 SCAR=6.9
245 0 my ( $c, %args ) = @_;
246 0 return Mojolicious::Plugin::DataTables::SSP::Results->new(%args);
247 }
248  
249 2 ▶sub _tag { Mojo::ByteStream->new( Mojo::DOM::HTML::tag_to_html(@_) ) }
Subroutine _tag: called CC=1 SCAR=0.0
250  
251 0 ▶sub _decode_params {
Subroutine _decode_params: not called CC=21 SCAR=61.4
252  
253 0 my ( $req_params, $dt_options ) = @_;
254  
255 0 my $dt_params = {};
256  
257 0 ▶ $dt_params->{draw} = $req_params->param('draw') || false;
Condition0%0/3
$req_params->param('draw') || &false()
l!l&&r!l&&!r
000
MC/DC0%0/2
$req_params->param('draw') || &false()
$req_params->param('draw') &false()
Truth table0%0/3
A || B
A= $req_params->param('draw')B= &false()
ABresult
1X1
011
000
258 0 ▶ $dt_params->{length} = $req_params->param('length') || 0;
Condition0%0/2
$req_params->param('length') || 0
l!l
00
MC/DC0%0/1
$req_params->param('length') || 0
$req_params->param('length')
Truth table0%0/2
A
A= $req_params->param('length')
Aresult
11
00
259 0 ▶ $dt_params->{start} = $req_params->param('start') || 0;
Condition0%0/2
$req_params->param('start') || 0
l!l
00
MC/DC0%0/1
$req_params->param('start') || 0
$req_params->param('start')
Truth table0%0/2
A
A= $req_params->param('start')
Aresult
11
00
260 0 ▶ $dt_params->{timestamp} = $req_params->param('_') || 0;
Condition0%0/2
$req_params->param('_') || 0
l!l
00
MC/DC0%0/1
$req_params->param('_') || 0
$req_params->param('_')
Truth table0%0/2
A
A= $req_params->param('_')
Aresult
11
00
261 0 $dt_params->{columns} = [];
262 0 $dt_params->{order} = [];
263 0 $dt_params->{where} = undef;
264  
265 0 foreach ( @{ $req_params->names } ) {
266  
267 0 my $value = $req_params->param($_);
268 0 ▶ $value = true if ( $value eq 'true' );
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "true" 0 0 0
269 0 ▶ $value = false if ( $value eq 'false' );
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "false" 0 0 0
270  
271 0 ▶ $dt_params->{columns}[$1]->{$2} = $value if ( $_ =~ /columns\[(\d+)\]\[(data|name|searchable|orderable)\]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /columns\[(\d+)\]\[(data|name|searchable|orderable)\]/u 0 0 0
272 0 ▶ $dt_params->{columns}[$1]->{search}->{$2} = $value if ( $_ =~ /columns\[(\d+)\]\[search\]\[(regex|value|)\]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /columns\[(\d+)\]\[search\]\[(regex|value|)\]/u 0 0 0
273 0 ▶ $dt_params->{order}[$1]->{$2} = $value if ( $_ =~ /order\[(\d+)\]\[(column|dir)\]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /order\[(\d+)\]\[(column|dir)\]/u 0 0 0
274 0 ▶ $dt_params->{search}->{$1} = $value if ( $_ =~ /search\[(value|regex)\]/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /search\[(value|regex)\]/u 0 0 0
275  
276 }
277  
278 0 ▶ for ( my $i = 0; $i < @{$dt_options}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$dt_options;}) 0 0 0
279  
280 0 my $dt_option = $dt_options->[$i];
281  
282 0 ▶ if ( !defined $dt_option->{dt} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dt_option->{"dt"}) 0 0 0
283 0 $dt_option->{dt} = $i;
284 }
285  
286 0 ▶ $dt_params->{columns}[ $dt_option->{dt} ]->{database} = $dt_option->{db} || undef;
Condition0%0/2
$dt_option->{'db'} || undef
l!l
00
MC/DC0%0/1
$dt_option->{'db'} || undef
$dt_option->{'db'}
Truth table0%0/2
A
A= $dt_option->{'db'}
Aresult
11
00
287 0 ▶ $dt_params->{columns}[ $dt_option->{dt} ]->{formatter} = $dt_option->{formatter} || undef;
Condition0%0/2
$dt_option->{'formatter'} || undef
l!l
00
MC/DC0%0/1
$dt_option->{'formatter'} || undef
$dt_option->{'formatter'}
Truth table0%0/2
A
A= $dt_option->{'formatter'}
Aresult
11
00
288 0 ▶ $dt_params->{columns}[ $dt_option->{dt} ]->{label} = $dt_option->{label} || undef;
Condition0%0/2
$dt_option->{'label'} || undef
l!l
00
MC/DC0%0/1
$dt_option->{'label'} || undef
$dt_option->{'label'}
Truth table0%0/2
A
A= $dt_option->{'label'}
Aresult
11
00
289  
290 0 ▶ if ( defined $dt_option->{searchable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt_option->{"searchable"}) 0 0 0
291 0 $dt_params->{columns}[ $dt_option->{dt} ]->{searchable} = $dt_option->{searchable};
292 }
293 0 ▶ if ( defined $dt_option->{orderable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt_option->{"orderable"}) 0 0 0
294 0 $dt_params->{columns}[ $dt_option->{dt} ]->{orderable} = $dt_option->{orderable};
295 }
296 }
297  
298 0 ▶ for ( my $i = 0; $i < @{ $dt_params->{columns} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$dt_params->{"columns"};}) 0 0 0
299 0 my $column = $dt_params->{columns}[$i];
300 0 $dt_params->{columns}[$i] = Mojolicious::Plugin::DataTables::SSP::Column->new( %{$column} );
301 }
302  
303 0 ▶ for ( my $i = 0; $i < @{ $dt_params->{order} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$dt_params->{"order"};}) 0 0 0
304 0 $dt_params->{order}[$i]->{column} = $dt_params->{columns}[ $dt_params->{order}[$i]->{column} ];
305 }
306  
307 # $dt_params->{columns} = Mojo::Collection->new ( $dt_params->{columns} );
308 # $dt_params->{order} = Mojo::Collection->new ( $dt_params->{order} );
309  
310 0 return $dt_params;
311  
312 }
313  
314 2 1;
315  
316 =encoding utf8
317  
318 =head1 NAME
319  
320 Mojolicious::Plugin::DataTables - DataTables Plugin for Mojolicious
321  
322 =head1 SYNOPSIS
323  
324 # Mojolicious
325 $self->plugin('DataTables');
326  
327 # Mojolicious::Lite
328 plugin 'DataTables';
329  
330 [...]
331  
332 my $sql = Mojo::Pg->new;
333  
334 my $dt_ssp = $c->datatable->ssp(
335 table => 'users',
336 sql => $sql,
337 columns => qw/role create_date/,
338 debug => 1,
339 where => 'status = "active"'
340 options => [
341 {
342 label => 'UID',
343 db => 'uid',
344 dt => 0,
345 formatter => sub {
346 my ($value, $column) = @_;
347 return '<a href="/user/' . $value . '">' . $value . '</a>';
348 }
349 },
350 {
351 label => 'e-Mail',
352 db => 'mail',
353 dt => 1,
354 },
355 {
356 label => 'Status',
357 db => 'status',
358 dt => 2,
359 },
360 ]
361 );
362  
363 return $c->render(json => $dt_ssp);
364  
365 =head1 DESCRIPTION
366  
367 L<Mojolicious::Plugin::DataTables> is a L<Mojolicious> plugin to add DataTables SSP (Server-Side Protocol) support in your Mojolicious application.
368  
369  
370 =head1 METHODS
371  
372 L<Mojolicious::Plugin::DataTables> implements the following methods.
373  
374 =head2 datatable_js
375  
376 Generate C<script> tag for include DataTables script file in your template.
377  
378 =head2 datatable_css
379  
380 Generate C<link rel="stylesheet"> tag for include DataTable CSS style in your template.
381  
382 =head2 datatable.ssp
383  
384 Params:
385  
386 =over 4
387  
388 =item C<table>: Database table
389  
390 =item C<sql>: An instance of L<Mojo::Pg>, L<Mojo::SQLite>, L<Mojo::mysql> or compatible class
391  
392 =item C<db>: An instance of L<Mojo::Pg::Database> or compatible class (B<DEPRECATED> use C<sql> instead)
393  
394 =item C<columns>: Extra columns to fetch
395  
396 =item C<debug>: Write useful debug information using L<Mojo::Log> class
397  
398 =item C<where>: WHERE condition in L<SQL::Abstract> format
399  
400 =item C<options>: Array of options (see below)
401  
402 =back
403  
404 Options:
405  
406 =over 4
407  
408 =item C<label>: Column label (optional)
409  
410 =item C<db>: Database column name (required)
411  
412 =item C<dt>: DataTable column ID (optional)
413  
414 =item C<formatter>: Formatter sub
415  
416 =back
417  
418 =head2 datatable.ssp_params
419  
420 Return an instance of L<Mojolicious::Plugin::DataTables::SSP::Params> class
421  
422 =head2 datatable.ssl_results
423  
424 Return an instance of L<Mojolicious::Plugin::DataTables::SSP::Results> class
425  
426 =head1 EXAMPLES
427  
428 =head2 Simple table
429  
430 Template:
431  
432 <table id="users_table" class="display" style="width:100%">
433 <thead>
434 <th>UID</th>
435 <th>e-Mail</th>
436 <th>Status</th>
437 </thead>
438 </table>
439  
440 <script>
441 jQuery('#users_table').DataTable({
442 serverSide : true,
443 ajax : '/users_table',
444 });
445 </script>
446  
447 Controller:
448  
449 $c->datatable->ssp(
450 table => 'users',
451 sql => $sql,
452 options => [
453 {
454 label => 'UID',
455 db => 'uid',
456 dt => 0,
457 },
458 {
459 label => 'e-Mail',
460 db => 'mail',
461 dt => 1,
462 },
463 {
464 label => 'Status',
465 db => 'status',
466 dt => 2,
467 },
468 ]
469 );
470  
471 =head2 Formatter
472  
473 The anonymous C<formatter> sub accept this arguments:
474  
475 =over 4
476  
477 =item C<$value>: the column value
478  
479 =item C<$column>: A L<Mojolicious::Plugin::DataTables::SSP::Column> instance
480  
481  
482 options => [
483 {
484 label => 'Name',
485 db => 'username',
486 dt => 0,
487 formatter => sub {
488 my ($value, $column) = @_;
489 my $row = $column->row;
490 return '<a href="/user/' . $row->{id} . '">' .$value . '</a>';
491 }
492 },
493 {
494 ...
495 }
496 ]
497  
498 =head2 Search flag
499  
500 The C<searchable> flag enable=1 or disable=0 a filter for specified column.
501  
502 options => [
503 {
504 label => 'Name',
505 db => 'username',
506 dt => 0,
507 searchable => 0,
508 },
509 {
510 ...
511 }
512 ]
513  
514 =head2 Where condition
515  
516 Use the C<where> option to filter the table using L<SQL::Abstract> syntax:
517  
518 $c->datatable->ssp(
519 table => 'users',
520 sql => $sql,
521 where => { status => 'active' }
522 options => [ ... ]
523 );
524  
525 It's possible to use array (C<[ where, bind_1, bind_2, ... ]>) to bind values:
526  
527 $c->datatable->ssp(
528 table => 'users',
529 sql => $sql,
530 where => [ 'status = ?', 'active' ],
531 options => [ ... ]
532 );
533  
534  
535 =head1 SEE ALSO
536  
537 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>, L<https://datatables.net/>, L<SQL::Abstract>
538 L<Mojolicious::Plugin::DataTables::SSP::Params>, L<Mojolicious::Plugin::DataTables::SSP::Results>, L<Mojolicious::Plugin::DataTables::SSP::Column>.
539  
540  
541 =head1 SUPPORT
542  
543 =head2 Bugs / Feature Requests
544  
545 Please report any bugs or feature requests through the issue tracker
546 at L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables/issues>.
547 You will be notified automatically of any progress on your issue.
548  
549 =head2 Source Code
550  
551 This is open source software. The code repository is available for
552 public review and contribution under the terms of the license.
553  
554 L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables>
555  
556 git clone https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables.git
557  
558  
559 =head1 AUTHOR
560  
561 =over 4
562  
563 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
564  
565 =back
566  
567  
568 =head1 LICENSE AND COPYRIGHT
569  
570 This software is copyright (c) 2020-2021 by Giuseppe Di Terlizzi.
571  
572 This is free software; you can redistribute it and/or modify it under
573 the same terms as the Perl 5 programming language system itself.
574  
575 =cut
576  
↑ Summary DataTables/SSP/Params.pm »