标签:
修改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‘); } }
标签:
原文地址:http://my.oschina.net/xinson/blog/515845