Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top