Magento网格列排序

5

曾经我试图创建一些自定义列。我像应该做的那样创建了整个XML结构。我创建了控制器,甚至创建了自定义网格控制器。

在创建我的自定义网格后,我认为列会排序。但我错了,彻底错了。单击列标题不起作用。

有什么建议吗?

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();
}

}


我认为通过将 setId('gm_product_grid') 更改为 setId('adminhtml_products_grid'),我解决了这个问题。但是,我希望能够得到确认。 - Nate H
1个回答

13

你应该删除这行代码:

->addAttributeToSort('name', 'asc')
如果您想根据某个属性设置默认排序,您需要使用网格块的setDefaultDir方法:
$this->setDefaultSort('name');
$this->setDefaultDir('asc');

setId方法不可能是这个问题的原因。而且你应该为你的adminhtml控制器实现gridAction,它将返回排序后的网格的HTML。类似于这样:

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()
        );
    }
}

Serjio,移除->addAttributeToSort()解决了问题。不过,你关于控制器的代码有点令人困惑。indexAction()已经定义了$this->loadLayout()$this->renderLayout(); - Nate H
1
对不起,我认为您应该仅在想要使用ajax的情况下实现gridAction。对于AJAX,您还应该在Grid块中创建一个方法public function getGridUrl() { return $this->getUrl('*/*/grid', array('_current'=>true)); }。在您的__construct方法中添加此行$this->setUseAjax(true);。此外,您可以删除$this->loadLayout()调用,因为在这种情况下它是不必要的。 - Sergii Stotskyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接