loading

Codeigniter Server Side Grid with Pagination Ready To Use Script

A CodeIgniter library for building a Datatables server side processing SQL query.

Links

CodeIgniter

DataTables

Requirements

  • PHP Version 7.3.10 or greater
  • CodeIgniter 3.1.11+
  • Datatables 1.10.20+

Installation

Download Datatables_server_side.php and add it to your application/libraries directory.

Usage

Initialize library

From within any of your Controller methods, initialize library using the CodeIgniter's standard way. You MUST pass data as an array via the second parameter and it will be passed to the library's constructor:

$this->load->library('datatables_server_side', array(
	'table' => 'customer', //name of the table to fetch data from
	'primary_key' => 'customer_id', //primary key field name
	'columns' => array('first_name', 'last_name', 'email'), //zero-based array of field names. 
	'where' => array() //associative array or custom where string
));

Access the library

Once the library is loaded, you need to call the following method:

$this->datatables_server_side->process();

The method accepts two parameters which are both optional. These parameters are used to add / modify tr tag.

row_id (String)

Possible values: 'id', 'data', 'none'

Examples

$this->datatables_server_side->process('id');
//
$this->datatables_server_side->process('data'); //Default
//
$this->datatables_server_side->process('none');
//

row_class (String)

Possible values: '', 'class_name'

Examples

$this->datatables_server_side->process('none', ''); //Default
//
$this->datatables_server_side->process('none', 'class_name');
//

Sample

The following sample uses sakila dump downloaded from (https://dev.mysql.com/doc/index-other.html). You can also clone this repository and run it on your local machine to see an end to end working example.

Controller

load->helper('url');
		$this->load->view('home');
	}

	public function load_data()
	{
		$this->load->library('datatables_server_side', array(
			'table' => 'customer',
			'primary_key' => 'customer_id',
			'columns' => array('first_name', 'last_name', 'email'),
			'where' => array()
		));

		$this->datatables_server_side->process();
	}
}

View


First name Last name Email

Output

Sample

Known issues

  • Only zero-based column labelling is currently supported: 0 -> first column, 1 -> second column, etc.
  • No data processors / formatters. All data processing / formatting must be done at the front end.