theme_table
includes/theme.inc, строка 1296
- Версии
- 5 – 6
theme_table($header, $rows, $attributes = array(), $caption = NULL)
Возвращает темизированную таблицу.
Каждая ячейка может задаваться либо строкой (содержимым ячейки), либо ассоциативным массивом со следующими ключами:
'data'
: Строка, которую нужно отобразить в ячейке таблицы.'header'
: Является ли эта ячейка заголовком таблицы.- Произвольные атрибуты HTML, например,
'colspan'
; они будут применены к данной ячейке.
Пример заполнения $rows
:
$rows = array( // Простая строка array( 'Cell 1', 'Cell 2', 'Cell 3' ), // Строка с атрибутами; некоторые из ячеек этой строки также содержат атрибуты. array( 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky' ) );
Параметры
$header
Массив, содержащий заголовок таблицы. Каждый элемент массива может быть как локализированной строкой, так и ассоциативным массивом с ключами:
'data'
: Локализированный заголовок колонки.'field'
: Колонка таблицы в базе данных (обязательно, если для колонки включена сортировка).'sort'
: Порядок сортировки по-умолчанию — по возрастанию ('asc'
) или по убыванию ('desc'
).- Любые атрибуты HTML, такие как
'colspan'
; они будут применены к «заголовочной» ячейке таблицы.
$rows
Массив строк таблицы. Каждая строка может являеться массивом ячеек, или ассоциативным массивом с ключами:
'data'
: массив ячеек- Любые атрибуты HTML, такие как
'colspan'
; они будут применены к данной строке.
$attributes
Любые аттрибуты HTML, для применения к тегу <table>
.
$caption
Локализированная строка для использоватия в теге <caption>
.
Возвращаемое значение
Представление таблицы в виде HTML.
Связанные темы
Код
<?php
function theme_table($header, $rows, $attributes = array(), $caption = NULL) {
// Add sticky headers, if applicable.
if (count($header)) {
drupal_add_js('misc/tableheader.js');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$attributes['class'] = empty($attributes['class']) ? 'sticky-enabled' : ($attributes['class'] .' sticky-enabled');
}
$output = '<table'. drupal_attributes($attributes) .">\n";
if (isset($caption)) {
$output .= '<caption>'. $caption ."</caption>\n";
}
// Format the table header:
if (count($header)) {
$ts = tablesort_init($header);
// HTML requires that the thead tag has tr tags in it follwed by tbody
// tags. Using ternary operator to check and see if we have any rows.
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts);
$output .= _theme_table_cell($cell, TRUE);
}
// Using ternary operator to close the tags based on whether or not there are rows
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
}
else {
$ts = array();
}
// Format the table rows:
if (count($rows)) {
$output .= "<tbody>\n";
$flip = array('even' => 'odd', 'odd' => 'even');
$class = 'even';
foreach ($rows as $number => $row) {
$attributes = array();
// Check if we're dealing with a simple or complex row
if (isset($row['data'])) {
foreach ($row as $key => $value) {
if ($key == 'data') {
$cells = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$cells = $row;
}
if (count($cells)) {
// Add odd/even class
$class = $flip[$class];
if (isset($attributes['class'])) {
$attributes['class'] .= ' '. $class;
}
else {
$attributes['class'] = $class;
}
// Build row
$output .= ' <tr'. drupal_attributes($attributes) .'>';
$i = 0;
foreach ($cells as $cell) {
$cell = tablesort_cell($cell, $header, $ts, $i++);
$output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";
}
}
$output .= "</tbody>\n";
}
$output .= "</table>\n";
return $output;
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии