24. Magento 创建新闻模块(4)

本文介绍如何在Magento中为新闻模块添加批量操作功能,包括批量删除和更改状态等,通过修改GridBlock和后台控制器实现。

在上一节的Magento新闻模块开发中,我们已经为新闻模块创建了后台,使我们可以在后台添加、修改和删除新闻,这一节我们我们将增加批量的功能,使我们可以在后台批量修改和删除新闻。

1 修改Grid Block
首先在/app/code/local/Www/News/Block/Adminhtml/News/Grid.php中添加_prepareMassaction()函数

<?php

class Www_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    //...

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('news_id');
        $this->getMassactionBlock()->setFormFieldName('news');

        $this->getMassactionBlock()->addItem('delete', array(
             'label' => Mage::helper('news')->__('Delete'),
             'url' => $this->getUrl('*/*/massDelete'),
             'confirm' => Mage::helper('news')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('news/news')->getStatusesOptionsArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));

        $this->getMassactionBlock()->addItem('status', array(
            'label' => Mage::helper('news')->__('Change status'),
            'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
            'additional' => array(
                'visibility' => array(
                    'name' => 'status',
                    'type' => 'select',
                    'class' => 'required-entry',
                    'label' => Mage::helper('news')->__('Status'),
                    'values' => $statuses
                )
            )
        ));
        return $this;
    }
}

以上的代码使用了getStatusesOptionsArray()函数,我们在/app/code/local/MagentoBoy/News/Model/News.php中添加:

<?php

class Www_News_Model_News extends Mage_Core_Model_Abstract
{
    //...

    public function getStatusesOptionsArray()
    {
        return array(
            array(
                'label' => Mage::helper('news')->__('Enabled'),
                'value' => self::STATUS_ENABLED
            ),
            array(
                'label' => Mage::helper('news')->__('Disabled'),
                'value' => self::STATUS_DISABLED
            )
        );
    }
}

刷新后台我们将观察到在表格的顶部出现了批量选择的工具栏,在Action选择框中我们可以选择Delete和Change status,当选择Change status时选择框右边将会显示Status选择框,可以选择Enabled和Disabled,并且在每条新闻记录的左侧出现了选择框。

2 修改后台控制器

上面的代码中,我们使用了$this->getUrl('*/*/massDelete')和$this->getUrl('*/*/massStatus', array('_current'=>true))作为批量删除和修改的提交地址,现在我们需要在后台控制器中添加massDelete()和massStatus()函数。
修改/app/code/local/Www/News/controllers/Adminhtml/NewsController.php
<?php

class Www_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
{
    //...

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('news_id');
        $this->getMassactionBlock()->setFormFieldName('news');

        $this->getMassactionBlock()->addItem('delete', array(
             'label' => Mage::helper('news')->__('Delete'),
             'url' => $this->getUrl('*/*/massDelete'),
             'confirm' => Mage::helper('news')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('news/news')->getStatusesOptionsArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));

        $this->getMassactionBlock()->addItem('status', array(
            'label' => Mage::helper('news')->__('Change status'),
            'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
            'additional' => array(
                'visibility' => array(
                    'name' => 'status',
                    'type' => 'select',
                    'class' => 'required-entry',
                    'label' => Mage::helper('news')->__('Status'),
                    'values' => $statuses
                )
            )
        ));
        return $this;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值