chrisoconnell/cao-html-table

一个用于渲染HTML表格的Zend Framework 2视图助手

dev-master 2015-04-11 21:37 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:27:43 UTC


README

Zend Framework 2视图助手渲染HTML表格

输入可以是数组或CaoHtmlTable\Model\Table的实例。代码将尽可能利用给定的内容。

安装

主要设置

通过克隆项目

  1. 通过克隆到./vendor/中安装CaoHtmlTable ZF2模块。
  2. 将此项目克隆到您的./vendor/目录。

使用Composer

  1. 在您的composer.json中添加此项目

    "require": {
        "chrisoconnell/cao-html-table": "dev-master"
    }
  2. 现在运行以下命令来让Composer下载CaoHtmlTable:

    $ php composer.phar update

安装后

  1. 在您的application.config.php文件中启用它。

    <?php
    return array(
        'modules' => array(
            // ...
            'CaoHtmlTable',
        ),
        // ...
    );

示例 - 带有标题行的表格

  1. 将表格行定义为关联数组的数组。

    $rows = array(
      array('header col1' => 'col1 row1', 'header col2' => 'col2 row1'),
      array('header col1' => 'col1 row2', 'header col2' => 'col2 row2'),
    );

    标题行将从第一个数组的键中设置。

  2. 在您的视图脚本(.phtml文件)内调用视图助手。

    echo $this->htmlTable($rows);
  3. 这将输出以下内容

    <table>
     <thead>
      <tr>
        <th>header col1</th><th>header col2</th>
      </tr>
     </thead>
     <tbody>
      <tr>
        <td>col1 row1</td><td>col2 row1</td>
      </tr>
      <tr>
        <td>col1 row2</td><td>col2 row2</td>
      </tr>
     </tbody>
    </table>

注意,数组的键被映射到标题行。

示例 - 没有标题行的表格

  1. 将表格行定义为数组的数组。

    $rows = array(
      array('col1 row1', 'col2 row1'),
      array('col1 row2', 'col2 row2'),
    );
  2. 在您的视图脚本(.phtml文件)内调用视图助手。

    echo $this->htmlTable($rows);
  3. 这将输出以下内容

    <table>
     <tbody>
      <tr>
        <td>col1 row1</td><td>col2 row1</td>
      </tr>
      <tr>
        <td>col1 row2</td><td>col2 row2</td>
      </tr>
     </tbody>
    </table>

注意,没有输出标题行。

示例 - 使用CaoHtmlTable\Model\Table的实例

当您需要更多的表格灵活性时(例如设置标题、CSS类)时,您需要创建一个CaoHtmlTable\Model\Table的实例来用作视图助手输入。

  1. 定义实例并设置数据。

    $rows = array(
        array('col1 row1', 'col2 row1'),
        array('col1 row2', 'col2 row2', 'col 3 row2'),
    );
    $table = new CaoHtmlTable\Model\Table($rows);
    $table->setAttributes(array('class' => 'selected', 'id' => 'list'))
          ->setCaption('My Table Caption')
          ->setHeaderRow(array('Header 1', 'Header 2', 'Header 3'));

在设置标题行时,标题的索引将与行的索引匹配。如果未找到匹配的行索引,则表格单元格将被设置为&nbsp;

  1. 在您的视图脚本(.phtml文件)内调用视图助手。

    echo $this->htmlTable($table);
  2. 这将输出以下内容

    <table class="selected" id="list">
      <caption>My Table Caption</caption>
      <thead>
        <tr>
          <th>Header 1</th><th>Header 2</th><th>Header 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>col1 row1</td><td>col2 row1</td><td>&nbsp;</td>
        </tr>
        <tr>
          <td>col1 row2</td><td>col2 row2</td><td>col 3 row2</td>
        </tr>
      </tbody>
    </table>