PHP code example of nongbit / codeigniter-datatable

1. Go to this page and download the library: Download nongbit/codeigniter-datatable 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/ */

    

nongbit / codeigniter-datatable example snippets




namespace App\Controllers;

use App\Models\UserModel;
use Nongbit\DataTable\Traits\DataTableTrait;

class UserController extends BaseController
{
    use DataTableTrait;

    public function datatable()
    {
        $model = new UserModel();
        $builder = $model->builder();
        $dt = $this->getDatatable($builder);
        return $this->response->setJSON($dt->toArray());
    }
}

public function datatable()
{
    $builder = $this->db->table('orders o')
        ->select('o.id, o.customer_id, c.name as customer_name, o.total, o.status')
        ->join('customers c', 'c.id = o.customer_id', 'left')
        ->where('o.status !=', 'cancelled')
        ->where('o.total >', 1000);

    $dt = $this->getDatatable($builder);
    return $this->response->setJSON($dt->toArray());
}

public function datatable()
{
    $builder = $this->db->table('sales')
        ->select('product_id, SUM(amount) as total_sales')
        ->groupBy('product_id')
        ->having('SUM(amount) >', 5000);

    $dt = $this->getDatatable($builder);
    return $this->response->setJSON($dt->toArray());
}

public function datatable()
{
    $builder = $this->db->table('products')
        ->where('price >=', 100)
        ->where('stock >', 0)
        ->groupStart()
            ->where('category', 'electronics')
            ->orWhere('category', 'computers')
        ->groupEnd()
        ->orderBy('created_at', 'DESC');

    $dt = $this->getDatatable($builder);
    return $this->response->setJSON($dt->toArray());
}

public function datatable()
{
    $builder = $this->db->table('employees')
        ->select('*, YEAR(hire_date) as hire_year')
        ->where('YEAR(hire_date) >', 2020)
        ->orderBy('hire_year', 'DESC');

    $dt = $this->getDatatable($builder);
    return $this->response->setJSON($dt->toArray());
}

use Nongbit\DataTable\DataTable;

$builder = $model->builder()->where('active', 1);
$dt = new DataTable($builder);
return $this->response->setJSON($dt->toArray());

$dt->setCallback('created_at', function($value, $row) {
    return date('d-m-Y', strtotime($value));
});