用于自定义模块的magento中的Ajax分页

Kan*_*tel 6 magento

今天我在magento前端实现了Ajax分页.请看看它可能有助于您理解.

阻止文件...

class Namesapce_ModuleName_Block_Productlist extends Mage_Core_Block_Template
{

    public function __construct() 
    {
        $this->_setProductCollaction();
    }

    private function _setProductCollaction()
    {

        /** YOUR COLLACTION **/
    $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');
}
}  
Run Code Online (Sandbox Code Playgroud)

您的PHTML文件

<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();?>
Run Code Online (Sandbox Code Playgroud)

//添加此块您在AJAX Paging中添加产品列表的位置.

并在父BLOCK中添加1个Jquery.

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);
     }
});
}
Run Code Online (Sandbox Code Playgroud)

//其中productlist是父块的div id.

添加1个操作以进行更新,以便在分页时单击时刷新页面块.

public function filterproductAction()
{
    echo         

  Mage::app()->getLayout()
  ->createBlock('modulename/productlist')->setTemplate('modulename    
 /productlist.phtml')->toHtml();
}
Run Code Online (Sandbox Code Playgroud)