我正在构建一个用于网格列表的插件(当然供我个人使用)。现在我在链接中集成了 ZF2 Paginator http://framework.zend.com/manual/2.1/en/tutorials/tutorial.pagination.html .我正在为分页器(不是数组)使用 DB Select。我需要动态名称的字段,所以我可以迭代它们,类似的东西
<?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; ?>
由于这是实践项目,我真的不需要集成一些已经存在的第 3 方网格解决方案。我只想知道使用 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>';
还需要对模型进行更改(唉,我需要在插件外部进行此更改,现在不知道如何修复,除了被所有项目模型覆盖的模型父类)。
我需要添加结果集缓冲区来修复前向游标错误,如 This result is a forward only result set, calling rewind() after moving forward is not supported - 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();
}
最佳答案
那么,回顾一下您的问题:您想输出分页器的内容,但您不知道其列名,因为它们可能是动态的?
假设我们有一个有效的分页器,其中包含结果和未知的列名。这些是正确输出它们的步骤:
- 获取第一行的列名并将它们放入一个数组中。
- 生成表格/网格的标题。
- 使用数组遍历行以匹配表头序列。
可能如下所示。
$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>';
关于php - 需要来自 Zend Paginator 对象的动态列信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472743/