如何在Magento网格中创建新的getter

时间:2013-03-22 12:02:25

标签: magento getter

我的自定义模块网格中有以下代码。

     $this->addColumn('action',
     array(
            'header'    =>  Mage::helper('module')->__('Action'),
            'width'     => '100',
            'type'      => 'action',
            'getter'    => 'getId',     
            'actions'   => array(
                array(
                    'caption'   => Mage::helper('module')->__('Edit'),
                    'url'       => array('base'=> '*/*/edit'),
                    'field'     => 'id',                
                )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
    )); 

我想知道有没有办法在网格中制作新的getter。我这样做的目的是在URL中传递额外的参数。

通过使用它,我得到以下编辑网址

http://domain.com/index.php/module/adminhtml_module/edit/id/5/key/a19618bbaa3ee98ed395bc2fa552de35/

如果将另一个getter追加到url之类的话 'getter'=> 'getStoreId',

比我的网址应该像:

http://domain.com/index.php/module/adminhtml_module/edit/id/5/store/5/key/a19618bbaa3ee98ed395bc2fa552de35/

任何人都可以指导我,我该怎么做。?

我尝试使用以下代码,但它无效。

    $this->addColumn('action',
     array(
            'header'    =>  Mage::helper('module')->__('Action'),
            'width'     => '100',
            'type'      => 'action',
            'getter'    => array('getId','getStoreId'),     
            'actions'   => array(
                array(
                    'caption'   => Mage::helper('module')->__('Edit'),
                    'url'       => array('base'=> '*/*/edit'),
                    'field'     => array('id',store_id),                
                )
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
    ));

2 个答案:

答案 0 :(得分:2)

要将商店密钥添加到网址,请尝试将actions更改为

'actions'   => array(
    array(
        'caption' => Mage::helper('catalog')->__('Edit'),
        'url'     => array(
            'base'=>'*/*/edit',
            'params'=>array('store'=>$this->getRequest()->getParam('store'))
        ),
        'field'   => 'id'
    )
),

根据需要更新$this->getRequest()->getParam('store')

参考/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php

$this->addColumn('action',
    array(
        'header'    => Mage::helper('catalog')->__('Action'),
        'width'     => '50px',
        'type'      => 'action',
        'getter'     => 'getId',
        'actions'   => array(
            array(
                'caption' => Mage::helper('catalog')->__('Edit'),
                'url'     => array(
                    'base'=>'*/*/edit',
                    'params'=>array('store'=>$this->getRequest()->getParam('store'))
                ),
                'field'   => 'id'
            )
        ),
        'filter'    => false,
        'sortable'  => false,
        'index'     => 'stores',
));

答案 1 :(得分:1)

如果要从网格传递多个值,则可以使用自定义渲染器:

    $this->addColumn('action',
     array(
        'header'    =>  Mage::helper('module')->__('Action'),
        'width'     => '100',
        'type'      => 'text',
        'filter'    => false,
        'sortable'  => false,
        'is_system' => true,
        'renderer'  => 'Company_MyModule_Block_Adminhtml_Renderer_Actionlink',
    ));

渲染器:

class Company_MyModule_Block_Adminhtml_Renderer_Actionlink extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
  public function render(Varien_Object $row) {
    $url=$this->getUrl('*/*/edit', array('id'=>$row->getId(), 'storeId' => $row->getStoreId()));
    return sprintf("<a href='%s'>%s</a>", $url, Mage::helper('catalog')->__('Edit'));
  }
}