Utility class to handle threaded data generated by CakePHP Model::find('threaded')
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Test/Utility
Utility
.gitignore
LICENSE
README.md
composer.json

README.md

cakephp-thread-utility

Utility class to handle threaded data generated by CakePHP Model::find('threaded')

http://blog.xao.jp/blog/cakephp/reconstruct-threaded-data-to-table-rows/

Usage

in CategoriesController

// Category model acts as Tree

$threaded = $this->Category->find('threaded');
$tableRows = ThreadUtility::threadToRows($threaded);

in view

<table>
  <tbody>
  <?php
  foreach ($tableRows as $tableRow):
    ?>
    <tr>
    <?php
    foreach ($tableRow as $tableCell):
      $colspan = $tableCell['colspan'] > 1 ? ' colspan="' . $tableCell['colspan'] . '"' : '';
      $rowspan = $tableCell['rowspan'] > 1 ? ' rowspan="' . $tableCell['rowspan'] . '"' : '';
      $name = h($tableCell['Category']['name']);
      printf('<td%s%s>%s</td>', $colspan, $rowspan, $name);
    endforeach;
    ?>
    </tr>
    <?php
  endforeach;
  ?>
  </tbody>
</table>

an example of output

<table>
  <tbody>
    <tr>
      <td rowspan="2">Music</td>
      <td>Pops</td>
    </tr>
    <tr>
      <td>Jazz</td>
    </tr>
    <tr>
      <td colspan="2">Architecture</td>
    </tr>
    <tr>
      <td>Food</td>
      <td>Pasta</td>
    </tr>
  </tbody>
</table>
Music Pops
Jazz
Architecture
Food Pasta