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.
Use 'frame_callback' property in addColumn. Below is the working code of fetching sku's in order grid.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('increment_id', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'increment_id',
'frame_callback' => array($this, 'callback_skus'),
'filter' => false,
));
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view'=> true,
'display_deleted' => true,
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
));
$this->addColumn('status', array(
'header' => Mage::helper('sales')->__('Status'),
'index' => 'status',
'type' => 'options',
'width' => '70px',
'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
));
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
$this->addColumn('action',
array(
'header' => Mage::helper('sales')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('sales')->__('View'),
'url' => array('base'=>'*/sales_order/view'),
'field' => 'order_id',
'data-column' => 'action',
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
}
$this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));
$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));
return parent::_prepareColumns();
}
public function callback_skus($value, $row, $column, $isExport) {
$increment_id = $value;
$_order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
$_items = $_order->getAllItems();
$skus="";
foreach ($_items as $item) {
if($item->getData('product_type') == "simple"){
$skus .= $item->getSku()."<br/>";
}
}
return $skus;
}
Best Answer
Take a look at adminhtml/default/default/template/report/grid.phtml.
On line 136(Magento 1.7.0.2) the following statement starts a loop which outputs the individual rows: getCollection()->getIntervals() as $_index => $_item): ?>.
The $_index variable holds the time information. Than take a look at the parent class of Mage_Adminhtml_Block_Report_Product_Sold_Grid - Mage_Adminhtml_Block_Report_Grid.
On line 147 the following statement sets the time span of the report: $collection->setInterval($from, $to). Hope that this will help you! Regards