The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::DataTables - DataTables Plugin for Mojolicious

SYNOPSIS

    # 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);

DESCRIPTION

Mojolicious::Plugin::DataTables is a Mojolicious plugin to add DataTables SSP (Server-Side Protocol) support in your Mojolicious application.

METHODS

Mojolicious::Plugin::DataTables implements the following methods.

datatable_js

Generate script tag for include DataTables script file in your template.

datatable_css

Generate link rel="stylesheet" tag for include DataTable CSS style in your template.

datatable.ssp

Params:

table: Database table
db: An instance of Mojo::Pg or compatible class
columns: Extra columns to fetch
debug: Write debug information using Mojo::Log class
options: Array of options (see below)

Options:

label: Column label
db: Database column name
dt: DataTable column ID
formatter: Formatter sub

datatable.ssp_params

Return an instance of Mojolicious::Plugin::DataTables::SSP::Params class

datatable.ssl_results

Return an instance of Mojolicious::Plugin::DataTables::SSP::Results class

EXAMPLES

Simple table

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,
            },
        ]
    ));

Formatter

The anonymous formatter sub accept this arguments:

$value: the column value
$column: A Mojolicious::Plugin::DataTables::SSP::Column instance
    options => [
        {
            label     => 'Name',
            db        => 'username',
            dt        => 0,
            formatter => sub {
                my ($value, $column) = @_;
                my $row = $column->row;
                return '<a href="/user/' . $row->{id} . '">' .$value . '</a>';
            }
        },
        {
            ...
        }
    ]

Search flag

The searchable flag enable or disable a filter for specified column.

    options => [
        {
            label      => 'Name',
            db         => 'username',
            dt         => 0,
            searchable => 0,
        },
        {
            ...
        }
    ]

SEE ALSO

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:

Around line 433:

You forgot a '=back' before '=head2'