3

我正在为网格列表构建一个插件(当然供我个人使用)。现在我在链接http://framework.zend.com/manual/2.1/en/tutorials/tutorial.pagination.html中集成了 ZF2 Paginator 。我正在为分页器(非数组)使用数据库选择。我需要字段名称作为动态名称,所以我可以迭代它们,类似的东西

<?php $headers = $this->paginator->getFirstRow()->getColumns(); ?>
<tr>
<?php foreach ($headers as $col) : ?>
    <th><?php echo $col; ?></th>
<?php endforeach; ?>
</tr>

<?php foreach ($this->paginator as $row) : ?>
    <tr>
    <?php foreach ($row->getColumns() as $col) : ?>
        <td><?php echo $row->{$col}; ?></td>
    <?php endforeach; ?>
    </tr>
<?php endforeach; ?>

由于这是一个实践项目,我真的不需要集成一些已经存在的 3rd 方网格解决方案。我只是想知道是否可以使用 Zend Paginator API 进行类似的操作?

更新:问题终于解决了。解决方案与@netiul 的解决方案非常匹配,并进行了一些修改。

在插件助手中:

$resultSet = $paginator->getIterator(); 

$columnNames = array_keys(get_object_vars($resultSet->getArrayObjectPrototype()));

$grid .= '<thead><tr>';
foreach($columnNames as $header)
{
    $grid .= '<th>'.$header.'</th>';
}
$grid .= '</tr></thead>';

$grid .= '<tbody>';
foreach($resultSet as $row)
{
    $grid .= '<tr>';
    foreach($columnNames as $col)
    {
        $grid .= '<td>'.$row->$col.'</td>';
    }
}
$grid .= '</tbody>';

模型中还需要进行一项更改(唉,我需要在插件之外进行此更改,除了被所有项目模型覆盖的模型父类之外,现在不要如何修复)。

我需要添加结果集缓冲区来修复前向游标错误,例如此结果是仅向前的结果集,不支持向前移动后调用 rewind() - Zend )

在模型中:

public function __construct(Adapter $adapter)
{
    $this->adapter = $adapter;
    $this->resultSetPrototype = new ResultSet();
    $this->resultSetPrototype->buffer();  // New Line added for buffer
    $this->resultSetPrototype->setArrayObjectPrototype(new Leads());
    $this->initialize();
}
4

1 回答 1

3

所以,回顾一下你的问题:你想输出你不知道列名的分页器的内容,因为它们可能是动态的?

假设我们有一个有效的分页器,其结果和列名未知。这些是正确输出它们的步骤:

  1. 获取第一行的列名并将它们放入一个数组中。
  2. 生成表格/网格的标题。
  3. 使用数组遍历行以匹配表头序列。

这可能如下所示。

$firstItem = reset($paginator->getIterator());
$columnNames = array_keys(get_object_vars($firstItem));

// optional do some sorting here like `sort($columNames)`

echo '<thead><tr>';
foreach ($columnNames as $columnName) {
    echo '<th>' . $columnName . '</th>';
}
echo '</tr></thead>';

echo '<tbody>';
foreach ($paginator as $item) {
    echo '<tr>';
    foreach ($columnNames as $columnName) {
        echo '<td>' . $item->$columnName . '</td>';
    }
    echo '</tr>';
}
echo '</tbody>';
于 2014-05-28T13:12:39.500 回答