Mojolicious::Plugin::DataTables - DataTables Plugin for Mojolicious
# Mojolicious $self->plugin('DataTables'); # Mojolicious::Lite plugin 'DataTables'; [...] my $dt_ssp = $c->datatable->ssp( table => 'users', db => $db, columns => qw/role create_date/, debug => 1, 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
db
columns
debug
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', db => $db, 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 or disable a filter for specified column.
searchable
options => [ { label => 'Name', db => 'username', dt => 0, searchable => 0, }, { ... } ]
Mojolicious, Mojolicious::Guides, https://mojolicious.org, https://datatables.net/, Mojolicious::Plugin::DataTables::SSP::Params, Mojolicious::Plugin::DataTables::SSP::Results, Mojolicious::Plugin::DataTables::SSP::Column.
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.