3

私はグリッドリスト用のプラグインを構築しています(もちろん個人的な使用のため)。今、リンクhttp://framework.zend.com/manual/2.1/en/tutorials/tutorial.pagination.htmlのように ZF2 Paginator を統合しました。ページネーターに 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; ?>

これは実践プロジェクトであるため、既存のサードパーティ グリッド ソリューションを統合する必要はまったくありません。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>';

モデルにも 1 つの変更が必要です (残念ながら、プラグインの外部でこの変更を行う必要があります。すべてのプロジェクト モデルによってオーバーライドされるモデルの親クラスを除いて、修正する方法はありません)。

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();
}
4

1 に答える 1