Wednesday, February 19, 2014

How To Use Callback Function In Ignited Datatables ?

I did some research on jQuery datatables wrapper [IgnitedDatatables] and made it working on a project to load huge data. One problem where i stuck was how to use callback function on edit columns. After several hours of try, i fixed it. Below is the solution on how i fixed it.

This post is not about how to use Ignited Datatables. I assume that you have working Ignited Datatables .

This example is taken from
$this->datatables->select('customer_id, first_name, last_name, email')
->add_column('edit', 'Edit', 'customer_id')
->add_column('delete', 'Delete', 'customer_id')
->edit_column('email', '$1', strtolower('email')) // php functions
->edit_column('email', '$1', $this->custom_email('email'))  // custom functions
->edit_column('first_name', '$1', $this->fix_first_name('first_name'));

echo $this->datatables->generate();
// Callback Functions
function custom_email($val)
  return substr($val, 0, 3) . '..' . strstr($val, "@");

function fix_first_name($val)
  return ucwords(strtolower($val));

How to use callback function properly in an ignited datatables is shown below.

The above solution (custom edit_column callback function ) will not work for example, if you want to check the status of the user and want to display active or inactive status instead of 1 or 0.

I just want to tell you that you have to put callback functions in the Codeigniter helper file and not in the same controller. A CodeIgniter helper file is a PHP file with multiple functions. It is not a class.
To make it work. you have to load the helper file ($this->load->helper('My_datatable_helper')) before $this->datatables->generate() method.

Do remember that callbacks are made inside a library file which of course has no direct access to which controller uses it. Therefore, it is much easier or actually, just possible (from a library point of view) to use helpers instead for their callbacks.
/* Controller */

$this->load->helper('My_datatable_helper');  //load before generate function
->edit_column('status', '$1', 'check_status($status)')  // custom callback functions
echo $this->datatables->generate();
The controller has check_status($status) callback function to display ACTIVE OR INACTIVE depending on status (0 or 1 in the mysql database). In order to achieve that, you need to create a Codeigniter helper file and save this to 'application/helpers/' directory. I created 'MY_datatable_helper.php' and place a function inside. The check_status function checks the status variable and return Active, it is 1 otherwise Inactive. Below is the helper file:
/* MY_datatable_helper.php*/

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

*  edit_column callback function in Codeigniter (Ignited Datatables)
* Grabs a value from the edit_column field for the specified field so you can
* return the desired value.  
* @access   public
* @return   mixed

if ( ! function_exists('check_status'))
    function check_status($status = '')
        return ($status == 1) ? 'Active' : 'Inactive';

/* End of file MY_datatable_helper.php */
/* Location: ./application/helpers/MY_datatable_helper.php */  

All credit goes to codeigniter Ignited-Datatables library. You can download the library from


  1. It should be ->edit_column('status', '$1', 'check_status(status)')

    Not ->edit_column('status', '$1', 'check_status(***$*****status)')

    No Dollar sign :) thanks for this helper example!! it works great when you remove the $ sign :)

  2. This comment has been removed by the author.