You are here

function theme_ng_table in AngularJS 7

Returns HTML for a table, using AngularJs.

1 theme call to theme_ng_table()
angularjs_examples_nodes_list_form_builder in modules/angularjs_examples/angularjs_examples.module

File

./angularjs.module, line 418

Code

function theme_ng_table($variables) {
  $header = $variables['header'];
  $row = $variables['row'];
  $attributes = $variables['attributes'];
  $caption = $variables['caption'];
  $sticky = $variables['sticky'];
  $empty = $variables['empty'];
  $ng_controller = $variables['ng_controller'];
  $ng_model = $variables['ng_model'];
  $ng_repeat = !empty($variables['ng_repeat']) ? $variables['ng_repeat'] : "item in {$ng_model}";
  $ng_empty = !empty($variables['ng_empty']) ? $variables['ng_empty'] : "!{$ng_model}.length";

  // Add sticky headers, if applicable.
  if (count($header) && $sticky) {
    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'][] = 'sticky-enabled';
  }
  $output = '<table' . drupal_attributes($attributes) . ">\n";
  if (isset($caption)) {
    $output .= '<caption>' . $caption . "</caption>\n";
  }

  // Add the 'empty' row message if available.
  if (empty($row) && $empty) {
    $header_count = 0;
    foreach ($header as $header_cell) {
      if (is_array($header_cell)) {
        $header_count += 1;
      }
      else {
        $header_count++;
      }
    }

    // @todo: Fix.
    $rows[] = array(
      array(
        'data' => $empty,
        'class' => array(
          'empty',
          'message',
        ),
      ),
    );
  }

  // Format the table header:
  if (count($header)) {

    // HTML requires that the thead tag has tr tags in it followed by tbody
    // tags. Using ternary operator to check and see if we have any rows.
    $output .= count($row) ? ' <thead><tr>' : ' <tr>';
    foreach ($header as $cell) {
      $output .= '<th>' . $cell . '</th>';
    }

    // Using ternary operator to close the tags based on whether or not there are rows
    $output .= count($row) ? " </tr></thead>\n" : "</tr>\n";
  }

  // Format the table rows:
  if (!empty($row)) {
    $output .= "<tbody>\n";

    // Build row
    $output .= ' <tr ng-repeat="' . $ng_repeat . '">';
    foreach ($row as $cell) {
      $output .= '<td>' . $cell . '</td>';
    }
    $output .= " </tr>\n";

    // Add empty-text <tr>.
    $output .= '<tr ng-show="' . $ng_empty . '"><td>' . $empty . '</td></tr>';
    $output .= "</tbody>\n";
  }
  $output .= "</table>\n";
  return $output;
}