You are here

function theme_footable in FooTable 7.2

Returns HTML for a table.

$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' => array(
      'funky',
    ),
  ),
);
  • attributes: An array of HTML attributes to apply to the table tag.
  • caption: A localized string to use for the <caption> tag.
  • colgroups: An array of column groups. Each element of the array can be either:

    • An array of columns, each of which is an associative array of HTML attributes applied to the COL element.
    • An array of attributes applied to the COLGROUP element, which must include a "data" attribute. To add attributes to COL elements, set the "data" attribute with an array of columns, each of which is an associative array of HTML attributes.

    Here's an example for $colgroup:

$colgroup = array(
  // COLGROUP with one COL element.
  array(
    array(
      'class' => array(
        'funky',
      ),
    ),
  ),
  // Colgroup with attributes and inner COL elements.
  array(
    'data' => array(
      array(
        'class' => array(
          'funky',
        ),
      ),
    ),
    'class' => array(
      'jazzy',
    ),
  ),
);

These optional tags are used to group and set properties on columns within a table. For example, one may easily group three columns and apply same background style to all.

  • sticky: Use a "sticky" table header.
  • empty: The message to display in an extra row if table does not have any rows.
  • breakpoints: A keyed array of breakpoints. Each key represents the name of a breakpoint, used the the column headers. The values are integers containing the breakpoint in pixels. If this value is left empty, all enabled FooTable breakpoints will be used.
  • expand_all: Whether or not to expand all rows of the table.
  • expand_first: Whether or not to expand the first rows details.
  • show_header: Whether or not to display a header row in the table.
  • toggle_column: Specify which column the toggle is appended to in a row ("first" or "last").

Parameters

array $variables: An associative array containing:

  • 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.
    • "field": The database field represented in the table column (required if user is to be able to sort on this column).
    • "sort": A default sort order for this column ("asc" or "desc"). Only one column should be given a default sort order because table sorting only applies to one column at a time.
    • "breakpoints": An array containing FooTable breakpoint machine names at which point this column should be hidden.
    • Any HTML attributes, such as "colspan", to apply to the column header cell.
  • 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.
    • "no_striping": a boolean indicating that the row should receive no 'even / odd' styling. Defaults to FALSE.

    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:

Return value

string An HTML string representing the FooTable table.

See also

theme_table()

File

./footable.theme.inc, line 102
FooTable theme functions.

Code

function theme_footable($variables) {

  // Add FooTable class.
  if (empty($variables['attributes']['class']) || !in_array('footable', $variables['attributes']['class'])) {
    $variables['attributes']['class'][] = 'footable';
  }
  if (empty($variables['attributes']['id'])) {
    $variables['attributes']['id'] = drupal_html_id('footable');
  }
  $footable = array();

  // Breakpoints.
  if (empty($variables['breakpoints'])) {
    $variables['breakpoints'] = array();
    foreach (footable_breakpoint_load_multiple() as $breakpoint) {
      $variables['breakpoints'][$breakpoint->machine_name] = (int) $breakpoint->breakpoint;
    }
  }
  $footable['breakpoints'] = $variables['breakpoints'];

  // Expand all rows.
  $footable['expandAll'] = !empty($variables['expand_all']);

  // Expand first row.
  $footable['expandFirst'] = !empty($variables['expand_first']);

  // Show header.
  $footable['showHeader'] = !empty($variables['show_header']);

  // Expandable column.
  $footable['toggleColumn'] = $variables['toggle_column'];
  foreach ($variables['header'] as &$cell) {
    if (!is_array($cell)) {
      $cell = array(
        'data' => $cell,
      );
    }

    // Add datatype.
    if (empty($cell['data-type'])) {
      $cell['data-type'] = 'html';
    }

    // Add breakpoints.
    if (!empty($cell['breakpoints']) && empty($cell['data-breakpoints'])) {
      $cell['data-breakpoints'] = implode(' ', $cell['breakpoints']);
    }
  }
  footable_libraries_load();
  drupal_add_js(array(
    'footable' => array(
      $variables['attributes']['id'] => $footable,
    ),
  ), array(
    'type' => 'setting',
  ));
  drupal_add_js(drupal_get_path('module', 'footable') . '/footable.js');
  return theme('table', $variables);
}