这篇文章,我将会介绍一些admin grid里面的方法和操作
如果我们想要给数据的一行加一个编辑的链接,比如当我们点击某一行时,跳转到编辑页面,把这段代码加入到Grid.php中
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
从上面的代码就可以看到这个方法的作用了,一个跳转到编辑控制器并且带一个id参数的链接
如果你想要一个下拉框的列在你的Grid里面,比如说像下面截图这样的
只需要在Grid.php文件的_prepareColumns()方法中加入下面这段代码
$this->addColumn('type', array(
'header' => Mage::helper('simple')->__('Blog Type'),
'align' =>'left',
'width' => '8px',
'index' => 'type',
'type' => 'options',
'options' => array('1' => 'Normal','2' => 'Admin' , '3' => 'Guest')
));
Mass Actions在admin grid里面是非常有用的,而且用的也很频繁,从下面图片中可用看出Mass Actions是什么
Mass Actions主要是用来进行自定义选择删除操作之类的操作,比如在删除产品时,可以通过复选框进行自定义选择,然后选择Actions中的Delete选项,点击提交,跳转到deleteAction下进行删除操作,加入下面代码到Grid.php中
protected function _prepareMassaction()
{
$this->setMassactionIdField('blogid');
$this->getMassactionBlock()->setFormFieldName('blog');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('simple')->__('Delete'),
'url' => $this->getUrl('*/*/delete'),
'confirm' => Mage::helper('simple')->__('Are you sure?')
));
$statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('simple')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('simple')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
在这里简要分析下主要的代码
$this->getMassactionBlock()->setFormFieldName('blog');
这行主要作用是,定义要post到控制器中表单元素的名字为blog,而名字为blog的元素值就是我们自己定选择的用‘,’隔开的ids
'url' => $this->getUrl('*/*/massDelete')
这行主要是设置跳转到指定控制器的URL
'confirm' => Mage::helper('simple')->__('Are you sure?')
这行是在我们点击提交时,弹出的确认对话框
接下来我们实现一个删除mass action,把下面这段代码放入你的控制器里,我这里是BlogController.php
public function deleteAction() {
$ids = $this->getRequest()->getParam('blog');
if($ids ) {
try {
$model = Mage::getModel('simple/blog');
foreach ($ids as $id){
$model->load($id)->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
$this->_redirect('*/*/');
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
Export CSV/XML
如果你想添加import/export功能,如下图
首先我们把下面这两行加入到Grid.php中的_prepareColumns()方法里面
$this->addExportType('*/*/exportCsv', Mage::helper('simple')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('simple')->__('XML'));
然后在你的控制器里为它们添加action
public function exportCsvAction()
{
$fileName = 'blog.csv';
$grid = $this->getLayout()->createBlock('simple/adminhtml_blog_grid');
$this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}
public function exportXmlAction()
{
$fileName = 'blog.xml';
$grid = $this->getLayout()->createBlock('simple/adminhtml_blog_grid');
$this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
}
在你的grid里,你想对搜索、分页、或者其他的一些操作应用AJAX,接下来的这些是你需要做的
在Grid.php文件里的构造方法中加入下面这两行
$this
->setSaveParametersInSession(true);
$this
->setUseAjax(true);
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
然后在Grid.php文件里加入下面这个方法
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
这个URL就是AJAX请求的URL,获取内容动态插入到Grid中
下面在控制器里加入请求的控制器方法的代码
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('simple/adminhtml_blog_grid')->toHtml()
);
}
一般在magento中,默认的都会添加一个Add按钮,但是如果我们想要更多的自定义的按钮,该如何去做呢
如果你想要这样做的话,我们需要去修改Grid 容器(Container)文件,我这里是Blog.php文件,把下面代码加到文件中
public function __construct()
{
$this->_controller = 'adminhtml_blog';
$this->_blockGroup = 'simple'; //module name
$this->_headerText = Mage::helper('simple')->__('Blog Manager');
$this->_addButtonLabel = Mage::helper('simple')->__('Add Blog');
$this->_addButton('button1', array(
'label' => Mage::helper('simple')->__('Button Label1'),
'onclick' => 'setLocation(\'' . $this->getUrl('*/*/button1') .'\')',
'class' => 'add',
));
$this->_addButton('button2', array(
'label' => Mage::helper('simple')->__('Button Label2'),
'onclick' => 'setLocation(\'' . $this->getUrl('*/*/button2') .'\')',
'class' => 'remove',
));
parent::__construct();
}
如果我们想修改默认的page size,我们需要在你自己的Grid.php文件里覆盖父类中的
protected function _preparePage()
{
$this->getCollection()->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
$this->getCollection()->setCurPage($this->getParam($this->getVarNamePage(), $this->_defaultPage));
}
你可以根据你的需要,自定义$this->_defaultLimit and $this->_defaultPage的值
如果你想对Grid里的每一行进行一些自定义操作
在Grid.php中加入以下代码
$this->addColumn('action',
array(
'header' => Mage::helper('simple')->__('Action'),
'type' => 'action',
'getter' => 'getId',
'width' => '30px',
'actions' => array(
array(
'caption' => Mage::helper('simple')->__('Edit'),
'url' => $this->getUrl("*/*/edit"),
'field' => 'blogid'
),
array(
'caption' => Mage::helper('simple')->__('Delete'),
'url' => $this->getUrl("*/*/delete"),
'field' => 'blogid'
)
),
'filter' => false,
'sortable' => false
));
我们可以在Grid里面进行其他的一些定制,感兴趣的可以去Mage_Adminhtml_Block_Widget_Grid 类文件里一探究竟
原文地址:http://excellencemagentoblog.com/admin-part2-series-magento-admin-forms-grids-controllers-tabs