function theme_datatable in DataTables 6
Same name and namespace in other branches
- 7.2 datatables.module \theme_datatable()
- 7 datatables.module \theme_datatable()
Return a themed DataTable.
This function takes the same parameters as theme_table, but also allows the inclusion of DataTable specific information in the $header and $attributes parameters in order to configure a DataTable. If an id is not set in the $attributes paramater, a unique one is generated.
To set features and options for the DataTable, add $parameters['datatable_options'] as an associative array. For example:
$parameters['datatables_options'] = array(
'bFilter' => FALSE,
// Disable filtering of data.
'bInfo' => TRUE,
// Show the table information display.
'aaSorting' => array(
// Sort by 3rd column first, and then 4th column.
array(
2,
'asc',
),
array(
3,
'desc',
),
),
);
See http://datatables.net/usage/features and http://datatables.net/usage/options for a full list of features and options.
To enable column specific options, set the datatable_options for each of the columns in the $header parameter. For example:
$header = array(
array(
'data' => t('Column 1'),
'datatable_options' => array(
'bSortable' => TRUE,
),
),
array(
'data' => t('Column 2'),
'datatable_options' => array(
'bSearchable' => FALSE,
),
),
);
Note: if the aaColumns option is enabled in $parameters['datatable_options'], then all datatable_options in the $header parameter will be ignored, since the parameters will override column options. See http://datatables.net/usage/columns for a full list of column options.
Parameters
$header: An array containing the table headers. Each element of the array can be either a localized string or an associative array with the following keys:
- "data": The localized title of the table column.
- Any HTML attributes, such as "colspan", to apply to the column header cell.
- "datatable_options": An associative array containing DataTable column specific features/options.
$rows: An array of table rows. Every row is an array of cells, or an associative array with the following keys:
- "data": an array of cells
- Any HTML attributes, such as "class", to apply to the table row.
Each cell can be either a string or an associative array with the following keys:
- "data": The string to display in the table cell.
- "header": Indicates this cell is a header.
- Any HTML attributes, such as "colspan", to apply to the table cell.
Here's an example for $rows:
$rows = array(
// Simple row
array(
'Cell 1',
'Cell 2',
'Cell 3',
),
// Row with attributes on the row and some of its cells.
array(
'data' => array(
'Cell 1',
array(
'data' => 'Cell 2',
'colspan' => 2,
),
),
'class' => 'funky',
),
);
$attributes: An array of HTML attributes to apply to the table tag. If the datatable_options is set, then those options are passed to the dataTable constructor.
$caption: A localized string to use for the <caption> tag.
Return value
An HTML string representing the table.
See also
theme_table
File
- ./
datatables.module, line 119 - Provides integration of the jQuery DataTables plugin
Code
function theme_datatable($header, $rows, $attributes = array(), $caption = NULL) {
$datatable_options = $attributes['datatable_options'];
// Column settings can either be set with the global options
// or in each header definition.
if (!$datatable_options['aoColumns']) {
foreach ($header as $key => $cell) {
$datatable_options['aoColumns'][] = $cell['datatable_options'];
unset($header[$key]['datatable_options']);
}
}
// Set unique id
if (!$attributes['id']) {
$attributes['id'] = _datatables_get_id();
}
drupal_add_css(drupal_get_path('module', 'datatables') . '/dataTables/media/css/demo_table.css');
drupal_add_js(drupal_get_path('module', 'datatables') . '/dataTables/media/js/jquery.dataTables.js');
drupal_add_js(drupal_get_path('module', 'datatables') . '/js/datatables.js');
drupal_add_js(array(
'datatables' => array(
'#' . $attributes['id'] => $datatable_options,
),
), 'setting');
unset($attributes['datatable_options']);
return theme('table', $header, $rows, $attributes, $caption);
}