Magento: Create a Custom Grid on the Frontend

Magento: Create a Custom Grid on the Frontend

Build a Magento custom grid on the storefront with a layout, block and template — a complete module walkthrough you can adapt to any data.

Step 1

Create a layout for action index: app/design/frontend/base/default/modulename.xml

<layout version="0.1.0">
<modulename_index_index>
<reference name="content">
<block type="modulename/blockname" name="modulename" template="modulename/modulename.phtml" />
</reference>
</modulename_index_index>
</layout>

Step 2

app/code/local/Packagename/Modulename/controllers/IndexController.php

class Packagename_Modulename_IndexController extends Mage_Core_Controller_Front_Action
{

  public function indexAction()
  {
    $this->loadLayout();
    $this->renderLayout();
  }
}

 Step 3

app/code/local/Packagename/Modulename/Block/Blockname.php

class Packagename_Modulename_Block_Blockname extends Mage_Core_Block_Template
{
      public function __construct()
      {
           parent::__construct();
           $collection = Mage::getModel('modelname/modelname')->getCollection();
           $this->setCollection($collection);
       }
}

 Step 4

<?php echo $this->getPagerHtml() ?>
<table id="my-data-table" class="data-table">
<colgroup>
<col width="1">
<col width="1">
<col>
</colgroup>
<thead>
<tr class="first last">
<th><?php echo $this->__('No.')?></th>
<th><?php echo $this->__('Name')?></th>
<th><?php echo $this->__('Time')?></th>
</tr>
</thead>
<tbody>
<?php foreach($this->getCollection() as $data): ?>
<tr>
<td><?php echo $person->getData('id')?></td>
<td><?php echo $person->getData('name')?></td>
<td><?php echo Mage::helper('core')->formatDate($data->getData('time'))?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php echo $this->getPagerHtml() ?>
<script type="text/javascript">decorateTable('my-data-table')</script>

Step 5

app/code/local/Packagename/Modulename/Block/Blockname.php

public function _prepareLayout()
{
     parent::_prepareLayout();
     $pager = $this->getLayout()->createBlock('page/html_pager', 'block.pager')->setCollection($this->getCollection());
     $this->setChild('pager', $pager);
     return $this;
}
public function getPagerHtml()
{
     return $this->getChildHtml('pager');
}

Still on Magento 1? This tutorial is for Magento 1.x, which reached end of life in June 2020 and no longer receives security patches. Our team migrates Magento 1 stores to Magento 2, Mage-OS or Adobe Commerce with your SEO and data intact — see our Magento development services.

Further reading: OpenMage, the community-maintained Magento 1 fork. Need a hand? Our Magento development team can help.

Need a hand?

Stuck on a build?
Let's solve it together.

Our engineers fix, upgrade and scale Magento, WordPress and modern stacks every day.