Is there a Zend Helper to generate a Html Table using and array as input ?
问题:
回答1:
partialLoop()
is probably best if you need a lightweight, easily customizable table generator. If you want something a little more to take all but the business logic of report generation in Zend, take a look at zfdatagrid.
回答2:
Most of all I use partialLoop() to generate tables. But sometimes, for simple data that don't require formatting, I use my simple view helper: https://gist.github.com/812481 . Usage:
<?php echo $this->table()->setRows($rows); ?>
or...
<?php echo $this->table(null, $rows); ?>
The $rows can be associative array or any object that has toArray method (Zend_Db_Table_Rowset, Doctrine_Collection etc.). Following is more complicated example, with headers, caption, additional column:
echo $this->table()
->setCaption('List of something')
->setAttributes(array('class' => 'mytable', 'id' => 'currenciesList'))
->setColumns(array(
'currency' => 'Currency',
'rate' => 'Rate',
'edit_options' => '' // Custom column
))
// content for custom column.
->setCellContent(
'<a href="/currency/delete/{id}" class="deleteLink">Delete</a>', 'edit_options'
)
->setFooter('Something to write in footer...')
->setEmptyRowContent('Nothing found')
->setRows($rows);
But this approach is not as convenient as partialLoop, cause it takes input data and display it as is - it doesn't allow you to format values using Zend_Date, Zend_Currency or do custom cell formatting.
回答3:
There is no native table zend view helper. However, you could use partialLoop view helper to ease generation of tables.
回答4:
You can also use a PEAR's HTML_Table package. You can throw an array to the table class and it is gonna populate the table for you. It has some nice features like colouring odd and even lines, set parameters per row, per columns and per table body.
Find more info at http://pear.php.net/package/HTML_Table/docs/latest/HTML_Table/HTML_Table.html