I've created a custom report. The report has a significant number of fields in it which are required for when it's exported in CSV form.

What I'm trying to figure out is if there's a way to omit the rendering of some of the fields on the front-end grid. Most of them aren't really important for the filtering process in admin and are only used when the report is downloaded for further parsing outside of Magento.

I would expect this to type of functionality to reside in the grid class as that's what generates the actual grid and, later on, the exported CSV but I don't see anything that looks like it deals with visibility.

Before I go in and override methods in this class for our custom report, is there a lesser-known way to accomplish this that's easier than my adding some kind of "showInGrid" property via the addColumn property array?

TL,DR is there an easy way to have fields exported (in say, CSV or XML) but omitted from the admin grid?

up vote 3 down vote accepted

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.

  • While this definitely works, I was hoping there would be a more accepted way of sharing the collection but limiting the columns that were used for any given instance of the block (displaying grid, generating CSV/XML exports, etc). I actually implemented the report in the method you suggested in your answer but I thought I'd look for a more elegant solution. – wlvrn Jan 30 '13 at 20:52
  • The problem isn't the collection so much as it is _prepareColumns which will reference an index that doesn't exist in the collection anymore. While there is a 'removeColumn' method, I would say extend the Grid class and call parent::_prepareColumns(), then inject your own removeColumns method. – philwinkle Jan 30 '13 at 21:43
  • @philwinkle : Actually I am using this method magento.stackexchange.com/a/171754/51361 , for adding custom field on sales order grid. but when i export, the custom added column values not exported, how can i solve this error, could you pls help me. – Gem Apr 27 '17 at 7:46

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');
    }
}

If you only need those fields for report, why don't you add them for the report only? Grid blocks have getCollection method which will return collection model and you can add fields you need for your report.

  • The grid view is useful for allowing the user to apply filters to a report (date range, sku, etc). While filters allow them to narrow the data set displayed, they might want the actual export to include, say, customer information or the constituent items that made up a bundle order. It's much simpler (one would think) to create the collection that has all of the data you need, and then show only some of that in the grid view for an admin view but allow the export to include all of the data. Unfortunately, the collection methods are tightly tied to the grid view. – wlvrn Jan 30 '13 at 20:47
  • I understand how grid is working and why it is helpful, what I meant is that csv export is separate action, you create the grid there and use its getCsv to prepare output. So those fields that you need only for csv files can be added to grid collection right before getCsv, this will not have more negative performance impact than doing it for general grid display without actually displaying it – Petar Dzhambazov Jan 30 '13 at 21:20

The easy way to do this is

Fall back the Grid.php file (app/core/Mage/Adminhtml/Block/Sales/Order/Grid.php)

then add your custom column like i did below:

//New columns added but hidded

$this->addColumn('custom_column', array(
   'header' => Mage::helper('sales')->__('Custom Column'),
   'index' => 'custom_column',
   'column_css_class'=>'no-display',
   'header_css_class'=>'no-display',
));

also made change in

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->joinLeft('sales_flat_order', 'main_table.entity_id = sales_flat_order.entity_id',array('custom_column'));     
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

this means we have to add our custom column value to collection. For that we have to join our table with grid collection table.

Now go and check backend, export the orders. there will be our custom column added.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.