ragi96 / table-field

A Table Field for Kirby V3
MIT License
22 stars 7 forks source link

Optional Table Headings Code Snippet #12

Open Rostiger opened 1 year ago

Rostiger commented 1 year ago

This is a great plugin, thank you for writing it! I extended the block template as well as the snipped to include an optional table heading and thought that others might find it useful too:

name: table
icon: table
fields:
  heading:
    label: Heading
    type: toggle
    default: true
    help: Turns the first row of a table into a heading.
  table:
    label: Table
    type: table
    maxColumns: 10
    minColumns: 2
<?php $table = $data->table()->toTable(); ?>
<?php if($table != null): ?>
 <table>
  <?php foreach ($table as $tableRow => $element): ?>
    <tr>
     <?php foreach ($element as $tableCell): ?>
      <?php if ($tableRow === array_key_first($table) && $block->heading()->isTrue()): ?>
       <th><?= $tableCell; ?></th>
      <?php else: ?>
       <td><?= $tableCell; ?></td>
      <?php endif; ?>
     <?php endforeach; ?>
    </tr>
  <?php endforeach; ?>
 </table>
<?php endif; ?>