Home > concrete5 for developers > How to display table name (in Table addon)

How to display table name (in Table addon)

This short hint is for people who want to customize the “Table” block output (part of the “Tables” addon)

You can edit the block template (or create your new custom template) and put this code:

echo $table["tableName"];

By default, the block template is ‘/packages/swp_tables/blocks/swp_table/view.php’

Here is a full example of template with table title displayed in table head:

<?php   defined('C5_EXECUTE') or die(_("Access Denied.")); ?>
<?php  if (!empty($table)) { ?>
<div class="swp-table swp-table-light-blue">
<table class="swp-table-table">
<?php 
// table header

$columns = $table["columns"];

$colspan = 0;
if (!empty($columns)) {	
	foreach($columns as $column) {
		if ($column["colspan"] > 1) {
			$colspan += $column["colspan"];
		} else {
			$colspan++;
		}
	}
} else {
	$first_row = isset($rows[0]) ? $rows[0] : array();
	$colspan = count($first_row);
}

// displaying table title
echo '<tr class="swp-table-title">';
echo ($colspan > 1) ? '<th colspan="'.$colspan.'">' : '<th>';
echo $table["tableName"];
echo '</th>';
echo '</tr>';

if (!empty($columns)) {
	echo '<tr class="swp-table-head">';
	$is_first = true;
	foreach($columns as $column) {
		if ($column["colspan"] > 1) {
			echo '<th colspan="'.$column["colspan"].'"';
			if ($is_first) {
				echo ' class="swp-table-first-cell"';
			}
			echo '>';
		} else {
			echo '<th';
			if ($is_first) {
				echo ' class="swp-table-first-cell"';
			}
			echo '>';
		}
		echo $column["fieldPublicName"];
		echo '</th>';
		$is_first = false;
	}
	echo '</tr>';
}

if (!empty($rows)) {
	$odd_trigger = false;
	$is_first_row = true;
	$last_index = count($rows) - 1;
	foreach($rows as $k=>$row) {
		$odd_trigger = !$odd_trigger;
		
		if ($is_first_row) {
			$first_row_class = ' swp-table-first-row';
		} else {
			$first_row_class = '';
		}
		
		if ($k == $last_index) {
			$last_row_class = ' swp-table-last-row';
		} else {
			$last_row_class = '';
		}
		
		if ($odd_trigger) {
			echo '<tr class="swp-table-odd-row'.$first_row_class.$last_row_class.'">';
		} else {
			echo '<tr class="swp-table-even-row'.$first_row_class.$last_row_class.'">';
		}
		
		// loop through values
		if (!empty($row["fields"])) {
			$is_first = true;
			foreach($row["fields"] as $value) {
				echo '<td';
				if ($is_first) {
					echo ' class="swp-table-first-cell"';
				}
				echo '>';
				echo $value;
				echo '</td>';
				$is_first = false;
			}
		}		
		
		echo '</tr>';
		$is_first_row = false;
	}
}

?>
</table>
<?php
/* PAGINATION: BEGIN */
if ($this->controller->hasPaging()) {
	echo '<div class="swp-table-paging">';
	echo '<b>'.t("Pages:").'</b> ';
	for($i=1;$i<=$this->controller->totalPages;$i++) {
		if ($i == $this->controller->currentPage) {
			echo ' <b>';
		} else {
			echo ' <a href="'. $this->controller->getPageURL($i) .'">';
		}
		echo $i;
		if ($i == $this->controller->currentPage) {
			echo '</b> ';
		} else {
			echo '</a> ';
		}
	}
	echo '</div>'; // swp-table-paging
}
/* PAGINATION: END */
?></div>
<?php  } // not empty table ?>
Categories: concrete5 for developers Tags:
  1. No comments yet.
  1. March 27th, 2013 at 13:23 | #1
  2. October 24th, 2014 at 01:42 | #2