Magento 网格列排序

标签 magento adminhtml

曾几何时,我尝试创建一些自定义列。我创建了整个 XML 结构,就像我应该拥有的那样。我创建了 Controller 。我什至创建了自定义网格 Controller 。

创建自定义网格后,我认为列会排序。我错了,大错特错。单击列标题什么也不做。

建议?

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

}

最佳答案

您应该删除此行:

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

如果要通过某些属性设置默认排序,则需要使用网格块的 setDefaultDir 方法:
$this->setDefaultSort('name');
$this->setDefaultDir('asc');

setId 方法不可能是这个问题的原因。您还应该为您的 adminhtml Controller 实现 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()
        );
    }
}

关于Magento 网格列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298081/

相关文章:

php - Magento Staging - 仅首页显示

layout - 在 Magento 之外加载 Magento 子 html

Magento - 'proceed to checkout' 按钮,如果未登录,则转到登录页面

magento - magento 中 paypal 快速结帐的 ipn url 是什么

Magento Grid Container Block 未加载网格

Magento:将选项卡添加到管理员订单详细信息页面

php - Magento 自定义模块观察者不工作

magento - 构建网格时,如何在addColumn()方法中使用 'concat'类型?

magento - 管理员中自定义 Magento 配置中的 404 错误

magento - 在 Magento 中为 Status、Tax Class 设置默认值