Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Grid with custom Data Collection
 
samueleorso
Jr. Member
 
Total Posts:  3
Joined:  2010-02-26
 

Hi all,
I would like to display data in a magento adminhtml page using the Grid Widget.
The data are generated on the fly by a controller and they aren’t stored in the magento database, and of course, I didn’t create and configured a Model.
Is it possible to create a Magento Collection by hand and use it inside a Grid for render its content? Is also possible to use filter and sorter features of Grid widget?
I wrote the following code but the Grid is always rendered empty. Have you any hints to solve this problem?

Thanks a lot in advance…

<?php

class <Namespace>_<Module>_Block_Adminhtml_<Module>_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

  
protected $collection;
  
  public function 
__construct()
  
{
    $this
->setId('<module>Grid');
    
$this->setDefaultSort('fielda');
    
$this->setDefaultDir('ASC');
    
$this->setSaveParametersInSession(false);
  
}

  
protected function _prepareCollection()
  
{
    $this
->collection = new Varien_Data_Collection();
    
$this->loadCollection();
    
$this->setCollection($this->collection);
    return 
parent::_prepareCollection();
  
}
  
  
protected loadCollection()
  
{
    $object 
= new Varien_Object();
    
$object->setData('fielda','a');
    
$object->setData('fieldb','b');
    
$object->setData('fieldc','c');
    
$this->collection->addItem($object);
  
}

  
protected function _prepareColumns()
  
{
    $this
->addColumn('fielda', array('header'     =>  Mage::helper('<module>')->__('Field A'),
                                      
'align'     =>  'left',
                                      
'width'     =>  '50px',
                                      
'index'     =>  'fielda',
                                    ));
    
$this->addColumn('fieldb', array('header'     =>  Mage::helper('<module>')->__('Field B'),
                                      
'align'     =>  'left',
                                      
'width'     =>  '50px',
                                      
'index'     =>  'fieldb',
                                     ));
    
$this->addColumn('fieldc', array('header'     =>  Mage::helper('<module>')->__('Field C'),
                                      
'align'     =>  'left',
                                      
'width'     =>  '50px',
                                      
'index'     =>  'fieldc',
                                     ));
    return 
parent::_prepareColumns();
  
}
  
  
public function getRowUrl($row)
  
{
    
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
  
}
  
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top