Adding a column to export but hiding from the grid?
You can use set column_css_class
and header_css_class
as no-display
to the column you want to hide. This will hide your column from the admin grid. However, this column will be present in the exported csv or xml.
$this->addColumn('YOUR_COLUMN_ID', array(
'header' => Mage::helper('YOUR_HELPER_NAME')->__('YOUR_COLUMN_NAME'),
'width' => '150px',
'index' => 'YOUR_COLUMN_INDEX',
'column_css_class'=>'no-display',
'header_css_class'=>'no-display',
));
There is a method _afterLoadCollection()
in Mage/Adminhtml/Block/Widget/Grid.php that can be overrided to remove/add columns for export or display with $this->_isExport
.
NOTE: Adding this to the _prepareCollection()
won't work as the collection filters will not have been submitted, resulting in the entire collection dumped minus any filtering.
protected function _afterLoadCollection() {
if(!$this->_isExport) {
$this->removeColumn('columnToRemove');
}
}
Create a separate block definition - e.g. duplicate the Grid block to another block that's specific for your CSV; I would call this Csvgrid.php
instead of Grid.php
- it would contain all of the same functionality that the normal Grid.php
contains, but omit the one column.
In your controller:
public function exportCsvAction()
{
$fileName = 'myreport_'.date('Y_m_d_h_i_s').'.csv';
$content = $this->getLayout()->createBlock('mymodule/adminhtml_reports_csvgrid')->getCsv();
}
When duplicating the Grid, place Csvgrid.php
into the same physical directory as Grid.php
but rename it accordingly - don't forget to change the class name!
Edit:
So it turns out that Mage_Adminhtml_Block_Widget_Grid
has a method called removeColumn
- defined as:
/
**
* Remove existing column
*
* @param string $columnId
* @return Mage_Adminhtml_Block_Widget_Grid
*/
public function removeColumn($columnId)
{
if (isset($this->_columns[$columnId])) {
unset($this->_columns[$columnId]);
if ($this->_lastColumnId == $columnId) {
$this->_lastColumnId = key($this->_columns);
}
}
return $this;
}
My guess is that because Mage_Adminhtml_Block_Report_Grid
extends Mage_Adminhtml_Block_Widget_Grid
it inherits this method and should be able to be used. I would, in that case, create a new block Grid and extend the Grid that your current report is in. From there you can use your own prepareColumns
method, call the parent::_prepareColumns()
and then call removeColumn
..
Best of luck.