5

Once upon a time I tried creating some custom columns. I created the entire XML structure like I should have. I created controllers. I even created the custom grid controller.

After creating my custom grid, I figured the columns would sort. I was wrong, dead wrong. Clicking on the column headers do nothing.

Suggestions?

class Company_Googlemerchant_Block_Adminhtml_Products_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
    parent::__construct();
    $this->setId('gm_product_grid');
    $this->setDefaultSort('id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(false);
}

protected function _prepareCollection()
{
    $storeId = 1;
    $collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($storeId);
    $collection
        ->addAttributeToSelect('enable_googlemerchant')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('entity_id')
        ->addAttributeToSelect('type_id')
        ->addAttributeToSelect('status')
        ->addFieldToFilter('enable_googlemerchant', array( "eq" => '1') )
        ->addFieldToFilter('status', array( "eq" => '1') )
        ->addAttributeToSort('name', 'asc')
        ;

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('id', array(
      'header'    => Mage::helper('googlemerchant')->__('ID'),
      'align'     =>'left',
      'index'     => 'entity_id',
      'width'     => '100px',
    ));

    $this->addColumn('product_name', array(
      'header'    => Mage::helper('googlemerchant')->__('Product Name'),
      'align'     =>'left',
      'index'     => 'name',
      'width'     => '250px',
    ));

    $this->addColumn('type_id', array(
      'header'    => Mage::helper('googlemerchant')->__('Product Type'),
      'align'     =>'left',
      'index'     => 'type_id',
      'width'     => '100px',
    ));

    $this->addColumn('action', array(
        'header'  => Mage::helper('googlemerchant')->__('Action'),
        'width'   => '100px',
        'type'    => 'action',
        'getter'  => 'getId',
        'actions' => array(
            array(
                'caption' => Mage::helper('googlemerchant')->__('Remove from export'),
                'url'     => array( 'base'   => '*/*/removeexport' ),
                'field'   => 'id'
            )
        ),
        'filter'    => false,
        'sortable'  => true,
        'index'     => 'id',
    ));

    return parent::_prepareColumns();
}

}

Nate H
  • 322
  • 1
  • 5
  • 20
  • I think I fixed the problem by changing `setId('gm_product_grid')` to `setId('adminhtml_products_grid')`. Would appreciate verification, however. – Nate H Jul 02 '12 at 19:46

1 Answers1

13

You should remove this line:

->addAttributeToSort('name', 'asc')

If you want to set default sorting by some attribute you need to use setDefaultDir method of the grid block:

$this->setDefaultSort('name');
$this->setDefaultDir('asc');

setId method could not be the reason of this issue. Also you should implement gridAction for you adminhtml controller that will return html of sorted grid. Something like this:

class My_Module_Adminhtml_EntityController extends Mage_Adminhtml_Controller_Action {
...................................................
    public function gridAction()
    {
        $this->loadLayout();
        // for AJAX queries
        $this->getResponse()->setBody(
            // it means that you have difened class My_Module_Block_Adminhtml_Entity_Grid
            $this->getLayout()->createBlock('my_module/adminhtml_entity_grid')->toHtml()
        );
    }
}
Sergii Stotskyi
  • 5,134
  • 1
  • 22
  • 21
  • Serjio, removing `->addAttributeToSort()` fixed the problem. However, you're code about the controller was a bit confusing. `indexAction()` is already defined with `$this->loadLayout()` and `$this->renderLayout();` – Nate H Jul 02 '12 at 21:53
  • 1
    I'm sorry. I think you should implement `gridAction` only if you want to use ajax for grid. For AJAX, you also should create a method ` public function getGridUrl() { return $this->getUrl('*/*/grid', array('_current'=>true)); }` in your Grid block. And in your `__construct` method add this line `$this->setUseAjax(true);`. Also you can remove `$this->loadLayout()` call because it's not necessary in that case – Sergii Stotskyi Jul 02 '12 at 22:03