FUEL CMS User Guide : Version 0.9.3


Data Table Class

This class allows you to easily create tables of data by passing in a multi-dimensional array of data. This class provides methods to set the sorting, headers and data of the table. This is the class used by the FUEL admin for the table view of data.

Initializing the Class

The Data_table class is initialized using the $this->load->library function:

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

Alternatively, you can pass initialization parameters as the second parameter:

$this->load->library('data_table', array('id'=>'data_table', 'header_on_class' => 'active'));

Configuring Data Table Information

The Data_table class has the following configuration parameters:

Preference Default Value Options Description
id data_table None ID to be used for the form
css_class data None CSS class to be used with the form
headers array() None The table headers
table_attrs array() Can be string or an array of attributes The table attributes
header_on_class on None Table header on class
header_asc_class asc None Table header asc class
header_desc_class desc None Table header desc class
row_alt_class alt None Row alternating class
sort_js_func NULL None Javascript sorting function to be used
body_attrs array() None tbody attributes
action_delimiter | None The text delimiter between each action
actions_field first first, last Which column the actions are in
auto_sort TRUE/FALSE (boolean) None Should sorting be done automatically?
only_data_fields array() None Data columns that won't be displayed
default_field_action NULL None The default column action
row_id_key id None The <tr> id prefix
row_action TRUE/FALSE (boolean) None Does the row have actions?
data array() None The data applied to the table
rows array() None An array of table rows
inner_td_class empty string '' None The css class to be used for the span inside the td
no_data_str No data to display. None The default string to display when no data exists
lang_prefix table_header_ None The language prefix to associate with table headers
field_styles array() None Styles to apply to the data columns. Index is the column and the value is the style

Function Reference

$this->data_table->assign_data(data, [headers], [attrs])

Assigns the data to be used for the table. Accepts a multi-deminsional array like so:

$data[] = array(
	'name' => 'Darth Vader',
	'weapon' => 'light saber',
	'darkside' => TRUE,
	'active' => 'yes'
);
$data[] = array(
	'name' => 'Luke Skywalker',
	'weapon' => 'light saber',
	'darkside' => FALSE,
	'active' => 'yes'
);

$data[] = array(
	'name' => 'Han Solo',
	'weapon' => 'blaster',
	'darkside' => FALSE,
	'active' => 'yes',
	'__field__' => array('name' => array('class' => 'highlight'))
);

$this->data_table->assign_data($data);

Note the '__field__' field (that's 2 underscores on each side). This is a special field that allows you to add attributes to the parent <td> tag. In the above example, the CSS class of "highlight" is applied to the Han Solo named column.

$this->data_table->render()

Renders the table. Example:

$this->data_table->render();

$this->data_table->render_headers()

Renders just the headers. Example:

$this->data_table->render_headers();

$this->data_table->render_rows()

Renders just the table rows. Example:

$this->data_table->render();

$this->data_table->clear()

Clears the data assigned to the table. Example:

$this->data_table->clear();

$this->data_table->set_sorting('col', 'ordering')

Sets the sorting information of which column and which direction. Example:

$this->data_table->set_sorting('name', 'asc');

$this->data_table->get_sorted_ordering()

Returns the sorting ordering of either asc or desc. Example:

echo $this->data_table->get_sorted_ordering(); // echos asc or desc

$this->data_table->get_sorted_field()

Returns the field that the data is currently being sorted by. Example:

echo $this->data_table->get_sorted_field(); // name of a field

$this->data_table->add_action('field', 'action', ['type'])

Adds actions to each row in the table. The type attribute can be either url or func. Type url is a string value that can have placeholder with {} surrounding column values that need to be substituted in. Type func is a string value of a function that will have the rows field values passed to it as the only parameter. The default is url. Example:

// with just a url
$action = array('EDIT' => 'example/edit/{id}');
$this->data_table->add_action($field, $action);
// with a function
$delete_func = '
$CI =& get_instance();
$link = "";
if ($CI->auth->has_permission("delete"))
{
	$url = site_url("example/delete/".$cols["id"]);
	$link = "DELETE";
}
return $link;';

$delete_func = create_function('$cols', $delete_func);
$this->data_table->add_action($field, $action);

$this->data_table->add_field_formatter('field', 'func')

Adds a formatter to a field. The formatter should be a function that accepts an array of field values. Example:

function is_darkside($fields)
{
	return ($fields['darkside']) ? 'yes' : 'no';
}

$this->data_table->add_field_formatter('darkside', 'is_darkside'); // will echo out either yes or no for the field value

$this->data_table->set_table_attributes(attrs)

Sets the table attributes. Example:

$this->data_table->set_table_attributes($attrs);

$attrs must be a key value array.

$this->data_table->set_body_attributes(attrs)

Sets the tbody attributes. Example:

$this->data_table->set_body_attributes($attrs);

$attrs must be a key value array.

$this->data_table->add_header('key', 'name', ['sorting_param'], [attrs], [index])

Adds a single header to the table. The $sorting_param is the column to sort on if it is different then the $key. Example:

$this->data_table->add_header('name', 'Name', 'name', '', 1);

$this->data_table->add_headers('headers', [sorting_params], [attrs])

Adds multiple headers to the table. Example:

$headers = array(
	'name' => 'Name',
	'weapon' => 'Weapon',
	'active' => 'Active'
);
$sorting_cols = array('name', 'weapon', 'active');

$this->data_table->add_headers($headers, $sorting_cols, '');

$this->data_table->add_row('columns', [attrs], [index], [action])

Adds a single row to the table. Example:

$row = array(
	'id' => 1,
	'name' => 'Luke Skywalker',
	'weapon' => 'light saber',
	'active' => 'yes'
);
$action = array('EDIT' => 'example/edit/{id}');

$this->data_table->add_row($row, '', 1, $action);

$this->data_table->add_rows('data', [attrs], ['action'])

Adds multiple rows to the table. Example:

$rows[] = array(
	'id' => 1,
	'name' => 'Luke Skywalker',
	'weapon' => 'light saber',
	'active' => 'yes'
);

$rows[] = array(
	'id' => 2,
	'name' => 'Han Solo',
	'weapon' => 'blaster',
	'active' => 'yes'
);
$action = array('EDIT' => 'example/edit/{id}');

$this->data_table->add_rows($rows, '', $action);