Example #1
0
 /**
  * Render the table structure
  * Main method
  *
  * @param \FrenchFrogs\Table\Table\Table $table
  * @return mixed|string
  * @throws \Exception
  */
 public function table(Table\Table $table)
 {
     // Headers
     $head = '';
     $headers = [];
     $hasStrainer = false;
     foreach ($table->getColumns() as $column) {
         /** @var Column\Column $column */
         $label = $column->getLabel();
         if ($column->hasDescription()) {
             $label .= ' <i class="fa fa-question-circle" data-toggle="tooltip" title="' . $column->getDescription() . '"></i>';
         }
         $head .= html('th', ['class' => 'text-center'], $label);
         $headers[] = $column->getName();
         $hasStrainer = $hasStrainer || $column->hasStrainer();
     }
     $head = html('tr', ['class' => 'heading'], $head);
     // Strainer
     if ($hasStrainer) {
         // initialisation des strainer
         $strainer = '';
         foreach ($table->getColumns() as $column) {
             /** @var Column\Column $column */
             $content = '';
             if ($column->hasStrainer()) {
                 $content = $column->getStrainer()->render();
             }
             $strainer .= html('th', ['class' => 'text-center'], $content);
         }
         $head .= html('tr', ['class' => 'filter'], $strainer);
     }
     // Data
     $body = '';
     foreach ($table->getRows() as $row) {
         $line = '';
         foreach ($table->getColumns() as $name => $column) {
             $attributes = $column->getAttributes();
             if ($table->hasIdField()) {
                 if (!isset($row[$table->getIdField()])) {
                     throw new \LogicException($table->getIdField() . ' column is not found');
                 }
                 //too soon for you
                 //                  $attributes['data-id'] = sprintf('%s#%s', $row[$table->getIdField()], $name);
             }
             // remove class in colum because it set in column for datatable
             if ($table->isDatatable()) {
                 unset($attributes['class']);
             }
             $line .= html('td', $attributes, $column->render((array) $row)) . PHP_EOL;
         }
         $body .= html('tr', [], $line);
     }
     // Footer
     $footer = '';
     if ($table->isDatatable()) {
         $this->render('datatable', $table);
     } elseif ($table->hasFooter()) {
         $current = $table->getPage();
         $footer .= sprintf('<li class="disabled"><span>&laquo;</span></li>');
         for ($i = 1; $i <= min(10, $table->getPagesTotal()); $i++) {
             $footer .= html('li', ['class' => $current == $i ? 'active' : null], sprintf('<a href="%s">%s</a>', $table->getPageUrl($i), $i));
         }
         $footer .= sprintf('<li><a href="%s" rel="next">&raquo;</a></li>', '#');
         $footer = html('ul', ['class' => 'pagination'], $footer);
         $footer = html('td', ['colspan' => count($headers)], $footer);
         $footer = html('tr', [], $footer);
         $footer = html('tfoot', ['class' => 'text-center'], $footer);
     }
     // Bootstrap class management
     $table->addClass(Style::TABLE_CLASS);
     if ($table->isStriped()) {
         $table->addClass(Style::TABLE_CLASS_STRIPED);
     }
     if ($table->isBordered()) {
         $table->addClass(Style::TABLE_CLASS_BORDERED);
     }
     if ($table->isCondensed()) {
         $table->addClass(Style::TABLE_CLASS_CONDENSED);
     }
     if ($table->hasHover()) {
         $table->addClass(Style::TABLE_CLASS_HOVER);
     }
     $html = html('table', $table->getAttributes(), html('thead', [], $head) . html('tbody', [], $body) . $footer);
     // responsive
     if ($table->isResponsive()) {
         $html = html('div', ['class' => Style::TABLE_CLASS_RESPONSIVE], $html);
     }
     if ($table->hasPanel()) {
         $html = $table->getPanel()->setBody($html)->render();
     }
     return $html;
 }