Somacon.com: Articles on websites & etc.

§ Home > Index > Web Development

Alternating Row Colors with Zend PartialLoop

How to alternate table or list row colors in the partialLoop call of Zend.

The code snippets below should give you the idea. Every alternating TR tag will have a class of "a". Then, in the style sheet, just define a background color for TR tags with that class. You can alternate the class by using the partialCounter variable of the view, which contains the current index of the array.

partialList.phtml


<tr class="<?php print ($this->partialCounter == 0) ? 'a' ''?>">
    <td><?php echo $this->escape($this->Id); ?></td>
    <td><?php echo $this->escape($this->FirstName); ?></td>
    <td><?php echo $this->escape($this->LastName); ?></td>
</tr>

style.css


<style type="text/css">
table.example tr.a td { background-color: gray; }
</style>

index.phtml


<table class="example">
<tr>
    <th>Id</th>
    <th>Last</th>
    <th>First</th>
</tr>
<?php echo $this->partialLoop('example/partialList.phtml'$this->names); ?>
</table>

Alternative

With the old Zend Framework, you needed to save the row state in the parent view, which is accessed through the PartialLoop helper. The code is the same as above, except the partialList.phtml is changed slightly.

partialList.phtml (Alternative)


<tr class="<?php print (!$this->getHelper('PartialLoop')->view->altrow ^= 1) ? 'a' '';    ?>">
    <td><?php echo $this->escape($this->Id); ?></td>
    <td><?php echo $this->escape($this->FirstName); ?></td>
    <td><?php echo $this->escape($this->LastName); ?></td>
</tr>

See the Zend View Helpers : Partial Helper documentation for more information.


Have you heard of the new, free Automated Feeds offered by Google Merchant Center? Learn more in Aten Software's latest blog post comparing them to traditional data feed files.
Created 2008-06-26, Last Modified 2018-01-25, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.