我正在为网格列表构建一个插件(当然供我个人使用)。现在我在链接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();
}