Skip to content

yogitrion/ci4-datatables

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GitHub GitHub repo size Hits

ci4-datatables

Server Side Datatables Library for CodeIgniter 4 Framework NOTE: This lib is under early development.

Description

Library to make server side Datatables on CodeIgniter 4 to be more easy

Requirements

  • Codeigniter 4.*
  • JQuery 3.*
  • JQuery Datatables

Installation

Installation is best done via Composer, you may use the following command:

composer require irsyadulibad/codeigniter4-datatables

This will add the latest release of codeigniter4-datatables as a module to your project. Note that you may need to adjust your project's minimum stability in order to use this lib while it is in beta.

Manual Installation

Should you choose not to use Composer to install, you can download this repo, extract and rename this folder to codeigniter4-datatables. Then enable it by editing app/Config/Autoload.php and adding the Irsyadulibad\DataTables namespace to the $psr4 array. For example, if you copied it into app/Libraries:

    $psr4 = [
        'Config'      => APPPATH . 'Config',
        APP_NAMESPACE => APPPATH,
        'App'         => APPPATH,
        'Irsyadulibad\DataTables'   => APPPATH .'Libraries/codeigniter4-datatables/src',
    ];

Example:

This is an example code for using this library:

  • PHP:
<?php namespace App\Controllers;

use Irsyadulibad\DataTables\DataTables;

class Home extends BaseController
{
	public function json()
	{
		return DataTables::use('users')
			->where(['role' => 'admin'])
			->hideColumns(['password'])
			->rawColumns(['bio'])
			->make(true);
	}
}
  • Javascript
$('#table').DataTable({
  processing: true,
  serverSide: true,
  ajax:{
    url: 'http://localhost:8080/json'
  },
  columns: [
	  {data: 'username', name: 'username'},
	  {data: 'email', name: 'email'},
	  {data: 'fullname', name: 'fullname'}
	  {data: 'bio', name: 'bio'}
  ]
});

Documentation:

Now you can use this without instantiate class

DataTables::use('table');
  • Select Table
    Select the table that you want to use
DataTables::use('table')
  • Set Output
    The default parameter is true, which is automatically return the JSON data. You can return the data's dump by passing the false param
DataTables::use('table')
	->make(false);
  • Select Fields
    Select the sepicifics column in the table
->select('username, password')
  • Where Clause
->where(['role' => 'user', 'active' => 1])
  • Join Clause
// <table>, <condition>, <type>
->join('address', 'users.id = address.uid', 'INNER JOIN')
  • Add Column
    Add custom column which is not in the table
// <name>, <callback>
->addColumn('action', function($data) {
	return '<a href="/edit/'.$data->id.'">edit</a>';
})
  • Edit Column\
// <name>, <callback>
->editColumn('created_at', function($data) {
	return format($data);
})
  • Raw Columns
    By default, all of the data is escaped to prevent XSS. But if you want to unescape them, you can use this method
->rawColumns(['bio'])
  • Hide Columns
    Hide columns from JSON output
->hideColumns(['password'])

Notes:

  • For now, we don't use the POST method due to a problem with the CSRF

Author's Profile:

Github: [https://github.com/irsyadulibad]
Website: [http://irsyadulibad.my.id]
Facebook: [https://facebook.com/irsyadulibad.dev]

About

Server Side Datatables Library for CodeIgniter 4 Framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%