Magento提供了强大的Grid Widget使我们能够方便的创建数据表格,现在我们为新闻模块创建后台数据表格。
修改config.xml配置文件,添加admin路由
<config> <admin> <routers> <news> <use>admin</use> <args> <module>Xinson_News</module> <frontName>news</frontName> </args> </news> </routers> </admin> </config>
添加后台菜单
/app/code/local/Xinson/News/etc/adminhtml.xml 加到cms菜单里面。
<?xml version="1.0"?> <config> <menu> <cms> <children> <news translate="title" module="news"> <title>News</title> <action>news/adminhtml_news</action> <sort_order>40</sort_order> </news> </children> </cms> </menu> <acl> <resources> <admin> <children> <cms> <children> <news translate="title" module="news"> <title>News</title> <sort_order>40</sort_order> </news> </children> </cms> </children> </admin> </resources> </acl> </config>
添加Grid表格
后台控制器indexAction
/app/code/local/Xinson/News/controllers/Adminhtml/NewsController.php
<?php class Xinson_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action { public function indexAction() { $this->_title($this->__('CMS'))->_title($this->__('News')); $this->loadLayout(); $this->_setActiveMenu('cms/news');//当前选中状态 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('News Manager'), Mage::helper('adminhtml')->__('News Manager'));//Mage::helper('adminhtml')->__ 这样是访问语言包 $this->__addBreadcrumb $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'));//增加block $this->renderLayout(); } }
添加Block
/app/code/local/Xinson/News/Block/Adminhtml/News.php
<?php class Xinson_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container { public function __construct() { $this->_controller = 'adminhtml_news'; //就是 Adminhtml/NewsController.php $this->_blockGroup = 'news'; //这是block组 $this->_headerText = Mage::helper('news')->__('News Manager'); $this->_addButtonLabel = Mage::helper('news')->__('Add News'); parent::__construct(); } }
添加grid文件
/app/code/local/Xinson/News/Block/Adminhtml/News/Grid.php
<?php class Xinson_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('newsGrid'); $this->setDefaultSort('news_id'); $this->setDefaultDir('ASC'); $this->setSaveParametersInSession(true); } protected function _prepareCollection() { $collection = Mage::getModel('news/news')->getCollection(); $this->setCollection($collection); return parent::_prepareCollection(); } // 加列表 列值 protected function _prepareColumns() { $this->addColumn('news_id', array( 'header' => Mage::helper('news')->__('ID'), 'align' =>'right', 'width' => '50px', 'index' => 'news_id', )); $this->addColumn('title', array( 'header' => Mage::helper('news')->__('Title'), 'align' =>'left', 'index' => 'title', )); $this->addColumn('is_active', array( 'header' => Mage::helper('news')->__('Status'), 'align' => 'left', 'width' => '80px', 'index' => 'is_active', 'type' => 'options', 'options' => Mage::getSingleton('news/news')->getAvailableStatuses(), )); $this->addColumn('created_at', array( 'header' => Mage::helper('news')->__('Created At'), 'align' =>'left', 'width' => '180px', 'type' => 'datetime', 'index' => 'created_at', )); $this->addColumn('updated_at', array( 'header' => Mage::helper('news')->__('Updated At'), 'align' =>'left', 'width' => '180px', 'type' => 'datetime', 'index' => 'updated_at', )); $this->addColumn('action', array( 'header' => Mage::helper('news')->__('Action'), 'width' => '50px', 'type' => 'action', 'getter' => 'getId', 'actions' => array( array( 'caption' => Mage::helper('news')->__('Edit'), 'url' => array('base'=>'*/*/edit'), 'field' => 'id' ) ), 'filter' => false, 'sortable' => false, 'index' => 'stores', )); return parent::_prepareColumns(); } public function getRowUrl($row) { return $this->getUrl('*/*/edit', array('id' => $row->getId())); } }
由于 _prepareColumns有 Mage::getSingleton('news/news')->getAvailableStatuses()
在 /app/code/local/Xinson/News/Model/News.php中添加getAvailableStatuses()方法
<?php class Xinson_News_Model_News extends Mage_Core_Model_Abstract { //.... const STATUS_ENABLED = 1; const STATUS_DISABLED = 0; public function getAvailableStatuses() { $statuses = new Varien_Object(array( self::STATUS_ENABLED => Mage::helper('news')->__('Enabled'), self::STATUS_DISABLED => Mage::helper('news')->__('Disabled'), )); return $statuses->getData(); } }
新闻模块创建和编辑
添加后台控制器的newAction和editAction方法
/app/code/local/Xinson/News/controllers/Adminhtml/NewsController.php
<?php class Xinson_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action { //... public function newAction() { $this->getRequest()->setParam('id', 0); $this->_forward('edit'); } public function editAction() { $this->_title($this->__('CMS'))->_title($this->__('News')); $newsId = $this->getRequest()->getParam('id'); $newsModel = Mage::getModel('news/news')->load($newsId); if ($newsModel->getId() || $newsId == 0) { $this->_title($newsModel->getId() ? $newsModel->getTitle() : $this->__('New News')); Mage::register('news_data', $newsModel); $this->loadLayout(); $this->_setActiveMenu('cms/news'); $this->_addBreadcrumb(Mage::helper('adminhtml')->__('News Manager'), Mage::helper('adminhtml')->__('News Manager'), $this->getUrl('*/*/')); $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Edit News'), Mage::helper('adminhtml')->__('Edit News')); $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news_edit')) ->_addLeft($this->getLayout()->createBlock('news/adminhtml_news_edit_tabs')); $this->renderLayout(); } else { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('news')->__('The news does not exist.')); $this->_redirect('*/*/'); } } }
添加Form Container
/app/code/local/Xinson/News/Block/Adminhtml/News/Edit.php
<?php class Xinson_News_Block_Adminhtml_News_Edit extends Mage_Adminhtml_Block_Widget_Form_Container { public function __construct() { $this->_objectId = 'id'; $this->_blockGroup = 'news'; $this->_controller = 'adminhtml_news'; parent::__construct(); //更新按钮文字 $this->_updateButton('save', 'label', Mage::helper('news')->__('Save News')); $this->_updateButton('delete', 'label', Mage::helper('news')->__('Delete News')); } public function getHeaderText() { //判断区分 新增和编辑 if(Mage::registry('news_data') && Mage::registry('news_data')->getId()) { return Mage::helper('news')->__("Edit News '%s'", $this->htmlEscape(Mage::registry('news_data')->getTitle())); } else { return Mage::helper('news')->__('Add News'); } } }
添加Form
/app/code/local/Xinson/News/Block/Adminhtml/News/Edit/Form.php
<?php class Xinson_News_Block_Adminhtml_News_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { //编辑的form的参数,action地址 protected function _prepareForm() { $form = new Varien_Data_Form(array( 'id' => 'edit_form', 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))), 'method' => 'post', )); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); } }
添加Tabs
/app/code/local/Xinson/News/Block/Adminhtml/News/Edit/Tabs.php
<?php //这是左边栏 class Xinson_News_Block_Adminhtml_News_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs { public function __construct() { parent::__construct(); $this->setId('news_tabs'); $this->setDestElementId('edit_form'); $this->setTitle(Mage::helper('news')->__('News Information')); } protected function _beforeToHtml() { $this->addTab('form_section', array( 'label' => Mage::helper('news')->__('News Information'), 'title' => Mage::helper('news')->__('News Information'), 'content' => $this->getLayout()->createBlock('news/adminhtml_news_edit_tabs_form')->toHtml(), )); return parent::_beforeToHtml(); } }
添加Tab Form
/app/code/local/Xinson/News/Block/Adminhtml/News/Edit/Tabs/Form.php
<?php //这是编辑区 class Xinson_News_Block_Adminhtml_News_Edit_Tabs_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $model = Mage::registry('news_data'); $form = new Varien_Data_Form(); $fieldset = $form->addFieldset('news_form', array('legend'=>Mage::helper('news')->__('News information'))); if ($model->getNewsId()) { $fieldset->addField('news_id', 'hidden', array( 'name' => 'news_id', )); } $fieldset->addField('title', 'text', array( 'name' => 'title', 'label' => Mage::helper('news')->__('News Title'), 'title' => Mage::helper('news')->__('News Title'), 'required' => true, )); $fieldset->addField('is_active', 'select', array( 'name' => 'is_active', 'label' => Mage::helper('news')->__('Status'), 'title' => Mage::helper('news')->__('News Status'), 'required' => true, 'options' => $model->getAvailableStatuses(), )); $fieldset->addField('content', 'editor', array( 'name' => 'content', 'label' => Mage::helper('news')->__('Content'), 'title' => Mage::helper('news')->__('Content'), 'required' => true, 'style' => 'width:700px; height:300px;', )); $form->setValues($model->getData()); $this->setForm($form); return parent::_prepareForm(); } }
刷新后台页面,点击Add News按钮新建新闻或点击表格中的某一行编辑新闻,观察是否显示编辑表单,下面我们为表单添加保存和删除的功能。
后台控制器的saveAction和deleteAction
/app/code/local/Xinson/News/controllers/Adminhtml/NewsController.php
<?php class Xinson_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action { //... public function saveAction() { if ($data = $this->getRequest()->getPost()) { $id = $this->getRequest()->getParam('id'); $model = Mage::getModel('news/news')->load($id); if (!$model->getId() && $id) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('news')->__('This news no longer exists.')); $this->_redirect('*/*/'); return; } if ($model->getCreatedAt() == NULL || $model->getUpdatedAt() == NULL) { $data['created_at'] = now(); $data['updated_at'] = now(); } else { $data['updated_at'] = now(); } $model->setData($data); try { $model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('news')->__('The news has been saved.')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId(), '_current'=>true)); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } $this->_redirect('*/*/'); } public function deleteAction() { if ($id = $this->getRequest()->getParam('id')) { try { $model = Mage::getModel('news/news'); $model->setId($id); $model->delete(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('news')->__('The news has been deleted.')); $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('news')->__('Unable to find a news to delete.')); $this->_redirect('*/*/'); } }