shemgp/ssp

Datatables SSP class for PHP server side scripting for PostgreSQL

v0.1.3 2018-03-25 07:04 UTC

This package is not auto-updated.

Last update: 2024-09-29 23:35:55 UTC


README

Datatables SSP class for server side scripting for PostgreSQL (uses ILIKE)

Extra features

  • May have columns for actions which are not in the database (eg. @actions) that can be used to display actions. The name should start with @.

Sample code using Slim with slim_plates

<?php
$crud = [
    'table_name' => 'students',
    'fields' => [
        'first' => 'text',
        'middle' => 'text',
        'last' => 'text',
        'bday' => 'date',
        'gender' => 'text'
    ]
];

$app->get('/'.$crud['table_name'].'/list', function ($request, $response, $args) use ($crud) {
    $columns = [];
    $count = 0;
    foreach($crud['fields'] as $field => $data_type)
    {
        $column = ['db' => $field, 'dt' => $count++];
        if ($data_type == 'date')
            $column['formatter'] = function( $d, $row ) {
                return date( 'F d, Y', strtotime($d));
            };
        else if ($data_type == 'number')
            $column['formatter'] = function( $d, $row ) {
                return '$'.number_format($d);
            };
        $columns[] = $column;
    }
    $columns[] = ['db' => 'id', 'dt' => $count++];
    $columns[] = [
            'db' => '@actions',
            'dt' => $count++,
            'formatter' => function ($d, $row ) use ($crud) {
                return '<div class="btn-group btn-group-sm">
                <a href="'.$this->router->pathFor($crud['table_name'].'.update', ['id'=>$row['id']]).'" class="btn btn-info btn-sm"><i class="fa fa-edit"></i></a>
                <form action="'.$this->router->pathFor($crud['table_name'].'.delete', ['id'=>':id']).'" method="POST">
                    <button class="btn btn-danger btn-sm delete-item" onclick="return confirm(\'Are you sure?\')">
                        <i class="fa fa-trash"></i>
                    </button>
                </form>
                </div>';
            }
        ];
    $ssp = DataTable\SSP::simple($request->getParams(), $this->db, $crud['table_name'], 'id', $columns);
    return json_encode($ssp);
})->setName($crud['table_name'].'.list.json');