zuborawka/cakephp-thread-utility

用于处理由 CakePHP Model::find('threaded') 生成线程数据的工具类

安装: 12

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:cakephp-plugin

v1.0 2014-02-03 12:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:17:06 UTC


README

用于处理由 CakePHP Model::find('threaded') 生成线程数据的工具类

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

用法

在 CategoriesController 中

// Category 模型作为树状结构

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

在视图中

<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>

输出示例

<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>