magento新闻模块开发(三)

修改Grid Block

在/app/code/local/Xinson/News/Block/Adminhtml/News/Grid.php中添加_prepareMassaction()函数

<?php

class Xinson_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/Xinson/News/Model/News.php中添加

<?php

class Xinson_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,并且在每条新闻记录的左侧出现了选择框。


修改后台控制器

上面的代码中,我们使用了$this->getUrl('*/*/massDelete')和$this->getUrl('*/*/massStatus', array('_current'=>true))作为批量删除和修改的提交地址,现在我们需要在后台控制器中添加massDelete()和massStatus()方法。

修改/app/code/local/Xinson/News/controllers/Adminhtml/NewsController.php

<?php

class Xinson_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
{
    //...
    public function massDeleteAction()
    {
        $newsIds = $this->getRequest()->getParam('news');
        $newsModel =  Mage::getModel('news/news');
        if(!is_array($newsIds)) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('adminhtml')->__('Please select item(s)')
            );
        } else {
            try {
                foreach ($newsIds as $newsId) {
                    $new = $newsModel->load($newsId);
                    $new->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted',
                        count($newsIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }
    
    public function massStatusAction()
    {
        $newsIds = $this->getRequest()->getParam('news');
        $newsModel =  Mage::getSingleton('news/news');
        if(!is_array($newsIds)) {
            Mage::getSingleton('adminhtml/session')
                ->addError($this->__('Please select item(s)'));
        } else {
            try {
                foreach ($newsIds as $newsId) {
                        $newsModel->load($newsId)
                        ->setIsActive($this->getRequest()->getParam('status'))
                        ->setIsMassupdate(true)
                        ->save();
                }
                $this->_getSession()
                    ->addSuccess(
                        $this->__('Total of %d record(s) were successfully updated',
                            count($newsIds))
                    );
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
    }
}


转载于:https://my.oschina.net/xinson/blog/515845

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值