NAME

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

SYNOPSIS

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

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
sql: An instance of Mojo::Pg, Mojo::SQLite, Mojo::mysql or compatible class
db: An instance of Mojo::Pg::Database or compatible class (DEPRECATED use sql instead)
columns: Extra columns to fetch
debug: Write useful debug information using Mojo::Log class
where: WHERE condition in SQL::Abstract format
options: Array of options (see below)

Options:

label: Column label (optional)
db: Database column name (required)
dt: DataTable column ID (optional)
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',
    sql     => $sql,
    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=1 or disable=0 a filter for specified column.

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

Where condition

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:

$c->datatable->ssp(
    table   => 'users',
    sql     => $sql,
    where   => [ 'status = ?', 'active' ],
    options => [ ... ]
);

SEE ALSO

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.

SUPPORT

Bugs / Feature Requests

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.

Source Code

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

AUTHOR

  • Giuseppe Di Terlizzi <gdt@cpan.org>

LICENSE AND COPYRIGHT

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:

Around line 498:

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