FUEL CMS User Guide : Version 1.5.2


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.

Properties Reference

Property Default Value Description
public
id data_table Id to be used for the form
css_class data Css class to be used with the form
headers
array()
The table headers
table_attrs
array('cellpadding' => '0', 'cellspacing' => '0', )
The table attributes
header_on_class on Table header on class
header_asc_class asc Table header asc class
header_desc_class desc Table header desc class
row_alt_class alt Row alternating class
sort_js_func alert('you must assign a javascript function to the $sort_js_func attribute of the Data_table instance'); return false; Javascript sorting function
body_attrs
array()
The tbody attributes
action_delimiter | The text delimiter between each action
actions_field first Which column the actions are in, first or last
auto_sort 1 Should sorting be done automatically?
only_data_fields
array()
Data columns that won't be displayed'
default_field_action none The default column action
row_id_key id The <tr> id prefix
row_action none Does the row have actions?
data
array()
The data applied to the table
rows
array()
An array of table rows
inner_td_class none The css class to be used for the span inside the td
no_data_str No data to display. The default string to display when no data exists
lang_prefix table_header_ The language prefix to associate with table headers
field_styles
array()
Styles to apply to the data columns. Index is the column and the value is the style
protected
_ordering N/A Sorting order
_field N/A Sorted column
_field_formatters N/A An array of function to format columns
_actions N/A Table row actions
_render_row_index N/A Internal pointer to the row index

Function Reference [+]

$this->data_table->initialize([$params=array()])

Initialize preferences.

Returns

void

Parameters

(array) $params 

$this->data_table->set_params($params)

Set object parameters.

Returns

void

Parameters

(array) $params 

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

Assign data to the table to be rendered. If headers is empty, the table will automatically create them from the data array.

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

Returns

void

Parameters

(array) $data table data
(array) $headers header key/names
(mixed) $attrs row attributes

Example


	$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);
	

$this->data_table->render(['$echo'=FALSE])

Render the entire table.

Returns

string

Parameters

(boolean) $echo echo the output?

$this->data_table->render_headers($headers)

Render the headers of the table.

Returns

string

Parameters

(array) $headers of Data_table_header(s)

$this->data_table->render_rows($rows)

Render the rows of the table.

Returns

string

Parameters

(array) $rows of Data_table_row(s)

$this->data_table->render_fields($fields)

Render the columns of the table.

Returns

string

Parameters

(array) $fields of Data_table_col(s)

$this->data_table->clear()

Same as reset.

Returns

void

$this->data_table->reset()

Reset the header and rows data.

Returns

void

$this->data_table->add_action('$label', '$action', ['$type'='url'], [$attrs=array()])

Add actions to each row. 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.

Returns

void

Parameters

(string) $label text label to display for the action
(string) $action a link path or a function
(string) $type the type (either url or func)
(mixed) $attrs the anchor attributes (e.g. target="_blank")

Example


	// with just a url
	$action = array('EDIT' => 'example/edit/{id}');
	$this->data_table->add_action($field, $action);
	</pre>

	<pre class="brush: php">
	// with a function
	$delete_func = '
	$CI =& get_instance();
	$link = "";
	if ($CI->auth->has_permission("delete"))
	{
		$url = site_url("example/delete/".$cols["id"]);
		$link = "<a href=\"".$url."\">DELETE</a>";
	}
	return $link;';

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

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

Add a formatter to a column.

The formatter should be a function that accepts an array of field values.

Returns

void

Parameters

(string) $field text label to display for the action
(string) $func a string that represents a function name

Example


	public 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_sorting('$field', ['$ordering'=TRUE])

Sets the sorting information of which column and which direction.

Returns

void

Parameters

(string) $field which column to display sorted
(boolean) $ordering is the order ascending?

$this->data_table->get_sorted_order('$col', ['$ordering'=TRUE])

Sets the sorting information of which column and which direction.

Returns

void

Parameters

(string) $col which column to display sorted
(boolean) $ordering is the order ascending?

$this->data_table->get_sorted_field('$col', ['$ordering'=TRUE])

Sets the sorting information of which column and which direction.

Returns

void

Parameters

(string) $col which column to display sorted
(boolean) $ordering is the order ascending?

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

Set the table html attributes.

Returns

void

Parameters

(array) $attrs table attributes

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

Set the table tbody attributes.

Returns

void

Parameters

(array) $attrs tbody attributes

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

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

Returns

Data_table_header object

Parameters

(string) $key the column key value to associate to the row data
(string) $name header name
(string) $sorting_param sorting parameter to use with sorting
(mixed) $attrs header attributes
(mixed) $index string or in index array value for the header

Example


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

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

Add multiple headers to the table using an array instead of a Data_table_header object.

Returns

void

Parameters

(array) $headers key/values of column and display names for headers
(array) $sorting_params sorting parameters
(mixed) $attrs header attributes

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=array()], [$attrs=array()], ['$index'=NULL], ['$action'=NULL])

Add a single row to the table.

Returns

void

Parameters

(array) $columns columns data
(mixed) $attrs row attributes
(mixed) $index string or in index array value for the header
(mixed) $action actions for the row

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=array()], ['$action'=NULL])

Add multiple headers to the table using an array instead of a Data_table_header object.

Returns

void

Parameters

(array) $data row data
(array) $attrs row attributes
(mixed) $action row actions

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);