Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38234
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-08 16:34
文章分类

全部博文(31)

文章存档

2015年(31)

我的朋友
最近访客

分类: 系统运维

2015-06-09 18:06:39

Magento提供了强大的Grid Widget使我们能够方便的创建数据表格,这一节中我们为新闻模块创建后台数据表格。

1 添加Admin路由
修改/app/code/local/MagentoBoy/News/etc/config.xml
  1. <config>
  2.     <admin>
  3.         <routers>
  4.             <news>
  5.                 <use>admin</use>
  6.                 <args>
  7.                     <module>MagentoBoy_News</module>
  8.                     <frontName>news</frontName>
  9.                 </args>
  10.             </news>
  11.         </routers>
  12.     </admin>
  13. </config>
2 添加后台菜单
为了在CMS菜单下添加News子菜单,新建/app/code/local/MagentoBoy/News/etc/adminhtml.xml
  1. <?xml version="1.0"?>
  2. <config>
  3.     <menu>
  4.         <cms>
  5.             <children>
  6.                 <news translate="title" module="news">
  7.                     <title>News</title>
  8.                     <action>news/adminhtml_news</action>
  9.                     <sort_order>40</sort_order>
  10.                 </news>
  11.             </children>
  12.         </cms>
  13.     </menu>
  14.     <acl>
  15.         <resources>
  16.             <admin>
  17.                 <children>
  18.                     <cms>
  19.                         <children>
  20.                             <news translate="title" module="news">
  21.                                 <title>News</title>
  22.                                 <sort_order>40</sort_order>
  23.                             </news>
  24.                         </children>
  25.                     </cms>
  26.                 </children>
  27.             </admin>
  28.         </resources>
  29.     </acl>
  30. </config>
刷新后台,点击News菜单,将跳转到

3 添加Grid表格
1) 添加后台控制器的indexAction
/app/code/local/MagentoBoy/News/controllers/Adminhtml/NewsController.php
  1. <?php

  2. class MagentoBoy_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
  3. {
  4.     public function indexAction()
  5.     {
  6.         $this->_title($this->__('CMS'))->_title($this->__('News'));

  7.         $this->loadLayout();
  8.         $this->_setActiveMenu('cms/news');
  9.         $this->_addBreadcrumb(Mage::helper('adminhtml')->__('News Manager'), Mage::helper('adminhtml')->__('News Manager'));

  10.         $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'));
  11.         $this->renderLayout();
  12.     }
  13. }
2) 添加Grid Container
添加/app/code/local/MagentoBoy/News/Block/Adminhtml/News.php
  1. <?php

  2. class MagentoBoy_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container
  3. {
  4.     public function __construct()
  5.     {
  6.         $this->_controller = 'adminhtml_news';
  7.         $this->_blockGroup = 'news';
  8.         $this->_headerText = Mage::helper('news')->__('News Manager');
  9.         $this->_addButtonLabel = Mage::helper('news')->__('Add News');
  10.         parent::__construct();
  11.     }
  12. }
3) 添加Grid
添加/app/code/local/MagentoBoy/News/Block/Adminhtml/News/Grid.php
  1. <?php

  2. class MagentoBoy_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid
  3. {
  4.     public function __construct()
  5.     {
  6.         parent::__construct();
  7.         $this->setId('newsGrid');
  8.         $this->setDefaultSort('news_id');
  9.         $this->setDefaultDir('ASC');
  10.         $this->setSaveParametersInSession(true);
  11.     }

  12.     protected function _prepareCollection()
  13.     {
  14.         $collection = Mage::getModel('news/news')->getCollection();
  15.         $this->setCollection($collection);
  16.         return parent::_prepareCollection();
  17.     }

  18.     protected function _prepareColumns()
  19.     {
  20.         $this->addColumn('news_id', array(
  21.             'header' => Mage::helper('news')->__('ID'),
  22.             'align' =>'right',
  23.             'width' => '50px',
  24.             'index' => 'news_id',
  25.         ));

  26.         $this->addColumn('title', array(
  27.           'header' => Mage::helper('news')->__('Title'),
  28.           'align' =>'left',
  29.           'index' => 'title',
  30.         ));

  31.         $this->addColumn('is_active', array(
  32.             'header' => Mage::helper('news')->__('Status'),
  33.             'align' => 'left',
  34.             'width' => '80px',
  35.             'index' => 'is_active',
  36.             'type' => 'options',
  37.             'options' => Mage::getSingleton('news/news')->getAvailableStatuses(),
  38.         ));

  39.         $this->addColumn('created_at', array(
  40.           'header' => Mage::helper('news')->__('Created At'),
  41.           'align' =>'left',
  42.           'width' => '180px',
  43.           'type' => 'datetime',
  44.           'index' => 'created_at',
  45.         ));

  46.         $this->addColumn('updated_at', array(
  47.           'header' => Mage::helper('news')->__('Updated At'),
  48.           'align' =>'left',
  49.           'width' => '180px',
  50.           'type' => 'datetime',
  51.           'index' => 'updated_at',
  52.         ));
  53.         
  54.         $this->addColumn('action',
  55.             array(
  56.                 'header' => Mage::helper('news')->__('Action'),
  57.                 'width' => '50px',
  58.                 'type' => 'action',
  59.                 'getter' => 'getId',
  60.                 'actions' => array(
  61.                     array(
  62.                         'caption' => Mage::helper('news')->__('Edit'),
  63.                         'url' => array('base'=>'*/*/edit'),
  64.                         'field' => 'id'
  65.                     )
  66.                 ),
  67.                 'filter' => false,
  68.                 'sortable' => false,
  69.                 'index' => 'stores',
  70.         ));

  71.         return parent::_prepareColumns();
  72.     }

  73.     public function getRowUrl($row)
  74.     {
  75.         return $this->getUrl('*/*/edit', array('id' => $row->getId()));
  76.     }
  77. }
在代码中我们使用了Mage::getSingleton('news/news')->getAvailableStatuses()获取可用的状态,我们在
/app/code/local/MagentoBoy/News/Model/News.php中添加getAvailableStatuses()函数:
  1. <?php

  2. class MagentoBoy_News_Model_News extends Mage_Core_Model_Abstract
  3. {
  4.     //....

  5.     const STATUS_ENABLED = 1;
  6.     const STATUS_DISABLED = 0;

  7.     public function getAvailableStatuses()
  8.     {
  9.         $statuses = new Varien_Object(array(
  10.             self::STATUS_ENABLED => Mage::helper('news')->__('Enabled'),
  11.             self::STATUS_DISABLED => Mage::helper('news')->__('Disabled'),
  12.         ));

  13.         return $statuses->getData();
  14.     }
  15. }
刷新页面观察Grid是否生成。

阅读(794) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~