Mojolicious::Plugin::DataTables - DataTables Plugin for Mojolicious
# Mojolicious $self->plugin('DataTables'); # Mojolicious::Lite plugin 'DataTables'; [...] my $sql = Mojo::Pg->new; my $dt_ssp = $c->datatable->ssp( table => 'users', sql => $sql, columns => qw/role create_date/, debug => 1, where => 'status = "active"' options => [ { label => 'UID', db => 'uid', dt => 0, formatter => sub { my ($value, $column) = @_; return '<a href="/user/' . $value . '">' . $value . '</a>'; } }, { label => 'e-Mail', db => 'mail', dt => 1, }, { label => 'Status', db => 'status', dt => 2, }, ] ); return $c->render(json => $dt_ssp);
Mojolicious::Plugin::DataTables is a Mojolicious plugin to add DataTables SSP (Server-Side Protocol) support in your Mojolicious application.
Mojolicious::Plugin::DataTables implements the following methods.
Generate script tag for include DataTables script file in your template.
script
Generate link rel="stylesheet" tag for include DataTable CSS style in your template.
link rel="stylesheet"
Params:
table
sql
db
columns
debug
where
options
Options:
label
dt
formatter
Return an instance of Mojolicious::Plugin::DataTables::SSP::Params class
Return an instance of Mojolicious::Plugin::DataTables::SSP::Results class
Template:
<table id="users_table" class="display" style="width:100%"> <thead> <th>UID</th> <th>e-Mail</th> <th>Status</th> </thead> </table> <script> jQuery('#users_table').DataTable({ serverSide : true, ajax : '/users_table', }); </script>
Controller:
$c->datatable->ssp( table => 'users', sql => $sql, options => [ { label => 'UID', db => 'uid', dt => 0, }, { label => 'e-Mail', db => 'mail', dt => 1, }, { label => 'Status', db => 'status', dt => 2, }, ] );
The anonymous formatter sub accept this arguments:
$value
$column
options => [ { label => 'Name', db => 'username', dt => 0, formatter => sub { my ($value, $column) = @_; my $row = $column->row; return '<a href="/user/' . $row->{id} . '">' .$value . '</a>'; } }, { ... } ]
The searchable flag enable=1 or disable=0 a filter for specified column.
searchable
options => [ { label => 'Name', db => 'username', dt => 0, searchable => 0, }, { ... } ]
Use the where option to filter the table using SQL::Abstract syntax:
$c->datatable->ssp( table => 'users', sql => $sql, where => { status => 'active' } options => [ ... ] );
It's possible to use array ([ where, bind_1, bind_2, ... ]) to bind values:
[ where, bind_1, bind_2, ... ]
$c->datatable->ssp( table => 'users', sql => $sql, where => [ 'status = ?', 'active' ], options => [ ... ] );
Mojolicious, Mojolicious::Guides, https://mojolicious.org, https://datatables.net/, SQL::Abstract Mojolicious::Plugin::DataTables::SSP::Params, Mojolicious::Plugin::DataTables::SSP::Results, Mojolicious::Plugin::DataTables::SSP::Column.
Please report any bugs or feature requests through the issue tracker at https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables
git clone https://github.com/giterlizzi/perl-Mojolicious-Plugin-DataTables.git
Giuseppe Di Terlizzi <gdt@cpan.org>
This software is copyright (c) 2020-2021 by Giuseppe Di Terlizzi.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head2'
To install Mojolicious::Plugin::DataTables, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::DataTables
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::DataTables
For more information on module installation, please visit the detailed CPAN module installation guide.