CodeIgniter library for Datatables server-side processing / AJAX, easy to use :3
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
libraries
LICENSE
README.md

README.md

CIgniter-Datatables

CodeIgniter library for Datatables server-side processing / AJAX, easy to use :3

Features

  1. Easy to use.
  2. Generates Datatable and JSON for server side processing in just one controller.
  3. Multiple Datatables in one page.
  4. Use CodeIgniter Query Builder Class to produce query (support all functions). Read Documentation
  5. Support columns rendering/formatting.
  6. Able to define searchable table columns.
  7. Configurable datatables options. Read Documentation

Wiki

  1. Basic Usage

Installing

  • jQuery

     <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
  • DataTables

     <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
    
  • CIgniter Datatables Library

    Download and place to your codeigniter libraries folder

Basic Example

Controllers

$this->load->library('Datatables');

$dt_authors = $this->datatables->new();

$dt_authors->select('*')->from('authors');

$dt_authors
    ->style(array(
	'class' => 'table table-striped table-bordered',
    ))
    ->column('First Name', 'first_name')
    ->column('Last Name', 'last_name')
    ->column('Email', 'email');

$this->datatables->init('dt_authors', $dt_authors); 

Views

$this->datatables->generate('dt_authors');

// Add this line after you load jquery from code.jquery.com
$this->datatables->jquery('dt_authors');

Usage

Use CodeIgniter Query Builder Class/Active Record to build SQL query. Read Query Builder Documentation

Create new variable to create new Datatables.

$dt_authors = $this->datatables->new();

Select columns and table. NOTE : Don't use ->get() or other method for executing the query, let the library do for you.

$dt_authors->select('first_name, last_name, email')->from('authors');

Use column() to add column to datatables.

$dt_authors
    ->column('First Name', 'first_name')
    ->column('Last Name', 'last_name')
    ->column('Email', 'email');

Initialize the configurations

$this->datatables->init('dt_authors', $dt_authors);

Generate table in views

$this->datatables->generate('dt_authors);
$this->datatables->jquery('dt_authors);

Column Rendering/Formatting

// $dt_authors is an example 
$dt_authors
    ->column('Name', 'name', function($data, $row){
    	return $row['first_name'] .' '. $row['last_name'];
    })
    ->column('Age', 'age', function($data, $row){
		return $data . ' years old';
    })
    ->column('Email', 'salary');

$t->init();

Custom searchable column

// $dt_authors is an example 
$dt_authors
    ->searchable('first_name, age'); 	// table columns
    // -> ... other chain methods

Datatable Options

DataTables and its extensions are extremely configurable libraries and almost every aspect of the enhancements they make to HTML tables can be customised.

You can use set_options add the options.

Note : Second parameter will not produce single quote, wrap option value with double quotes to produce single quotes or use escaping.

// $dt_authors is an example 
$dt_authors
    ->set_options('searching', 'false')			// searching : false
    ->set_options('pagingType', '\'simple\'')		// pagingType : 'simple'
  //->set_options('pagingType', "'simple'")
    ->set_options('lengthMenu', '[ 10, 25, 50, 75, 100 ]')	 // lengthMenu : [ 10, 25, 50, 75, 100 ]

You can use array too ...

->set_options(array(
    array('searching', 'false')
    array('pagingType', "'simple'")
    array('lengthMenu', '[ 10, 25, 50, 75, 100 ]')
));

Use set_options('ajax.data', '...') to override ajax data options

Styling Tables

You can use style to add table tag attributes to styling your table.

// $dt_authors is an example 
$dt_authors
    ->style(array(
        'class' => 'table table-bordered table-striped',
    ))

Changelog

Version 1.5

  • Add new API to override ajax data
  • Support multiple datatables in one page
  • Fix unable to search on field contains null

Version 1.1

  • Fix searching when use alias for columns
  • Remove query_builder, use direct select() to build query

Version 1.0

  • Initial Release (Development)