Magento Grid:使用addColumn动作的更好方法是什么?

时间:2012-06-21 13:55:37

标签: php magento adminhtml

此功能设置Magento Grid以显示具有相应“删除”操作的文件名列表。

问题是删除操作永远不会传递参数'filename'。 (参见http://www.premasolutions.com/content/magento-manage-category-product-grid-edit-link)我有TESTDUMP用于验证,但它从未在下一页打印过。

'params'是'addColumn-> actions-> url'的合法行为吗?

更新:为控制器添加了构造和准备集合。也许是因为我正在使用的收藏类型?

class Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid

Rogue_Googlemerchant_Block_Adminhtml_Exporter_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

public function __construct()
{
    parent::__construct();
    $this->setId('googlemerchantGrid');
    // This is the primary key of the database
    $this->setDefaultSort('filename');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection()
{
    $basePath = Mage::getBaseDir('base');
    $feedPath = $basePath . '/opt/googlemerchant/';
    $errPath = $basePath . '/var/log/googlemerchant/';
    $flocal = new Varien_Io_File();
    $flocal->open(array('path' => $feedPath));
    $dataCollection = new Varien_Data_Collection();

    foreach ($flocal->ls() as $item) {
        $dataObject = new Varien_Object();
        $dataObject->addData(
            array(
                'filename'     => $item['text'],
                'size'         => $item['size'] / 1000 . ' kb',
                'date_modified'=> $item['mod_date']
            )
        );
        $dataCollection->addItem($dataObject);
    }

    $this->setCollection($dataCollection);

    return parent::_prepareCollection();
}

    protected function _prepareColumns()
    {
        $this->addColumn('filename', array(
          'header'    => Mage::helper('googlemerchant')->__('File'),
          'align'     =>'left',
          'index'     => 'filename',
          'width'     => '200px',
        ));

        $this->addColumn('action', array(
            'header'  => Mage::helper('googlemerchant')->__('Action'),
            'width'   => '50px',
            'type'    => 'action',
    //      'getter'  => 'getId',
            'actions' => array(
                array(
                    'caption' => Mage::helper('googlemerchant')->__('Delete'),
                    'url'     =>
                        array(
                            'base'   => '*/*/delete',
                            'params' => array('filename' => 'TESTDUMP')
                        ),
                    'field'   => 'filename'
                )
            ),
            'filter'    => false,
            'sortable'  => false,
    //       'index'     => 'filename',
    //       'is_system' => true,
    ));
    }

}

class Rogue_Googlemerchant_Adminhtml_ExporterController

class Rogue_Googlemerchant_Adminhtml_ExporterController extends Mage_Adminhtml_Controller_Action
{

    public function deleteAction()
    {
        $filename = $this->getRequest()->getParam('filename');
        $basePath = Mage::getBaseDir('base');
        $feedPath = $basePath . '/opt/googlemerchant/';
        $errPath = $basePath . '/var/log/googlemerchant/';
        $flocal = new Varien_Io_File();
        $flocal->open(array('path' => $feedPath));
d($filename);
        if ($filename) {
            try{
                $flocal->rm($filename);
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('googlemerchant')->__('The file has been deleted.'));
                $this->_redirect('*/*/');
            }
            catch (Mage_Core_Exception $e) {
                $this->log($e);
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/index');
                return;
            }
        }
die('here');
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find the file to delete.'));
        $this->_redirect('*/*/');
    }
}

1 个答案:

答案 0 :(得分:5)

对集合项使用action列的getter来检索field参数的参数值。

我不确定您为什么要指定硬编码的文件名,或者是否应该可以使用,但是如果您添加列配置

'getter'  => 'getFilename'

并从应该执行的操作中删除params