PHP code example of syamsoul / laravel-datatable-ssp

1. Go to this page and download the library: Download syamsoul/laravel-datatable-ssp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

syamsoul / laravel-datatable-ssp example snippets


use SoulDoit\DataTable\SSP;

use SoulDoit\DataTable\SSP;

class MyController extends Controller
{
    public function get(SSP $ssp)
    {
        // or using `new` keyword:
        // $ssp = new SSP();

        $ssp->setColumns($dt_cols_opt);

        $ssp->setQuery($dt_query);

        return $ssp->response()->json();
    }
}

    $ssp->setQuery(function ($selected_columns) {
        return \App\Models\User::select($selected_columns);
    });
    

    $ssp->setColumns([
        ['label'=>'ID',         'db'=>'id',            'formatter' => function ($value, $model) {
            return str_pad($value, 5, '0', STR_PAD_LEFT); 
        }],
        ['label'=>'Username',   'db'=>'uname'],
        ['label'=>'Email',      'db'=>'email'],
    ]);
    

    [   
        'label'         => $dt_col_header,
        'db'            => $db_col_name,
        'class'         => $dt_class,
        'formatter'     => $dt_formatter,
    ],
    

    $dt_col_header = 'Username';
    

    $db_col_name = 'uname';
    

    $dt_class = 'text-center';

    // or use array for multiple classes

    $dt_class = ['text-center', 'text-bold'];
    

    $dt_formatter = function ($value, $model) {
        return ucwords($value);
        // which is 'value' is the value of the column

        // or
        return $model->name;
        // which is 'model' is the model of the current row

        // or
        return $value . '(#' .$model->id. ')';
    };
    

namespace App\Http\Controllers\AdminPanel;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use SoulDoit\DataTable\SSP;

class UsersController extends Controller
{
    private $ssp;
    
    public function __construct()
    {
        $ssp = new SSP();

        $ssp->enableSearch();
        $ssp->allowExportAllItemsInCsv();
        $ssp->setAllowedItemsPerPage([5, 10, 20, -1]);
        $ssp->frontend()->setFramework('datatablejs');

        $ssp->setColumns([
            ['label'=>'ID',         'db'=>'id',            'formatter' => function ($value, $model) {
                return str_pad($value, 5, '0', STR_PAD_LEFT); 
            }],
            ['label'=>'Email',      'db'=>'email',         ],
            ['label'=>'Username',   'db'=>'uname',         ],
            ['label'=>'Created At', 'db'=>'created_at',    ],
            ['label'=>'Action',     'db'=>'id',            'formatter' => function ($value, $model) {
                $btns = [
                    '<button onclick="edit(\''.$value.'\');">Edit</button>',
                    '<button onclick="delete(\''.$value.'\');">Delete</button>',
                ];
                return implode($btns, " ");
            }],
            ['db'=>'email_verified_at'],
        ]);

        $ssp->setQuery(function ($selected_columns) {
            return \App\Models\User::select($selected_columns)
            ->where('status', 'active')
            ->where(function ($query) {
                $query->where('id', '!=', 1);
                $query->orWhere('uname', '!=', 'superadmin');
            });
        });
        
        $this->ssp = $ssp;
    }
    
    public function page()
    {   
        return view('admin-panel.users-list', [
            'fe_settings' => $this->ssp->frontend()
                ->setInitialSorting('created_at', true) // this means `order created_at desc`
                ->setInitialItemsPerPage(10)
                ->setResponseDataUrl(route('users.get'))
                ->getSettings(true),
        ]);
    }

    public function get()
    {
        return $this->ssp->response()->json();
    }
}