Implementing Ajax Paging in Magento Frontend
In today’s tutorial, we’ll guide you through implementing Ajax paging for a custom module in Magento frontend. This will allow smooth and efficient navigation of product lists without a full page reload.
Block File: Product Listing Block
First, let’s create the product listing block. Create a new file named productlist.php under your module’s block directory:
class Namesapce_ModuleName_Block_Productlist extends Mage_Core_Block_Template
{
public function __construct()
{
$this->_setProductCollection();
}
private function _setProductCollection()
{
/** YOUR COLLECTION */
$collection = Mage::getModel('catalog/product')->getCollection();
$this->setCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array(5 => 5));
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
PHTML File: Product Listing Template
Next, create a new template file named productlist.phtml under your module’s template directory:
<table class="data-table gapBottom14" id="order-table">
<colgroup>
<col width="23%" />
<col width="67%" />
<col width="10%" />
</colgroup>
<thead>
<tr class="first last">
<th rowspan="1"><span class="nobr"><?php echo $this->__('sku'); ?></span></th>
<th rowspan="1"><span class="nobr"><?php echo $this->__('NAME'); ?></span></th>
</tr>
</thead>
<tbody>
<?php foreach($products as $product): ?>
<tr class="first odd">
<td><?php echo $product->getSku(); ?></td>
<td class=""><?php echo $product->getName(); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo $this->getPagerHtml(); ?>
Adding Ajax Paging to Your Module
Add JavaScript
Add the following jQuery code to your parent block’s script:
jQuery(document).ready(function() {
jQuery(".pagination a").live('click', function(event) {
event.preventDefault();
page = jQuery(this).html();
updateTable(page);
});
function updateTable(pageno){
jQuery.ajax({
type: "get",
url:'<?php echo $this->getUrl('*/*/filterproduct'); ?>?p='+pageno,
success: function(data) {
jQuery("#productlist").html(data);
}
});
}
});
Add Action for Filter Products
Add the following action to your module’s controller:
public function filterproductAction()
{
echo $this->getLayout()->createBlock('modulename/productlist')->setTemplate('modulename/productlist.phtml')->toHtml();
}
Conclusion
That’s it! You now have implemented Ajax paging in Magento frontend for your custom module. This will provide a better user experience, especially when dealing with large product lists.
Remember to replace Namesapce_ModuleName and modulename with the actual namespace and name of your module throughout the code examples above.
The key steps involved:
- Create a new block file for the product listing.
- Prepare the layout and set up the collection in the block.
- Create the PHTML template for displaying the products and pager.
- Add JavaScript to handle Ajax paging clicks.
- Create an action in your controller to update the product list.
This approach allows you to keep the page content dynamic while minimizing the need for full page reloads, making your Magento store more efficient and responsive.
For more advanced customizations and best practices in Magento development, feel free to explore our comprehensive guides on LaravelCompany.com.
Happy coding!