Magento提供了强大的Grid Widget使我们能够方便的创建数据表格,这一节中我们为新闻模块创建后台数据表格。
1 添加Admin路由
修改/app/code/local/MagentoBoy/News/etc/config.xml
- <config>
-
<admin>
-
<routers>
-
<news>
-
<use>admin</use>
-
<args>
-
<module>MagentoBoy_News</module>
-
<frontName>news</frontName>
-
</args>
-
</news>
-
</routers>
-
</admin>
-
</config>
2 添加后台菜单
为了在CMS菜单下添加News子菜单,新建/app/code/local/MagentoBoy/News/etc/adminhtml.xml
- <?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>
刷新后台,点击News菜单,将跳转到
3 添加Grid表格
1) 添加后台控制器的indexAction
/app/code/local/MagentoBoy/News/controllers/Adminhtml/NewsController.php
- <?php
-
-
class MagentoBoy_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'));
-
-
$this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'));
-
$this->renderLayout();
-
}
-
}
2) 添加Grid Container
添加/app/code/local/MagentoBoy/News/Block/Adminhtml/News.php
- <?php
-
-
class MagentoBoy_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container
-
{
-
public function __construct()
-
{
-
$this->_controller = 'adminhtml_news';
-
$this->_blockGroup = 'news';
-
$this->_headerText = Mage::helper('news')->__('News Manager');
-
$this->_addButtonLabel = Mage::helper('news')->__('Add News');
-
parent::__construct();
-
}
-
}
3) 添加Grid
添加/app/code/local/MagentoBoy/News/Block/Adminhtml/News/Grid.php
- <?php
-
-
class MagentoBoy_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()));
-
}
-
}
在代码中我们使用了Mage::getSingleton('news/news')->getAvailableStatuses()获取可用的状态,我们在
/app/code/local/MagentoBoy/News/Model/News.php中添加getAvailableStatuses()函数:
- <?php
-
-
class MagentoBoy_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();
-
}
-
}
刷新页面观察Grid是否生成。
阅读(851) | 评论(0) | 转发(0) |