您的位置:首页 > 其它

MAGENTO 后台模块开发二

2016-05-09 06:43 519 查看
在这一章节,我们将会看到关于 Grid 更为复杂一点的操作,接下去所讲的都是前一章节的延续

修改 Grid 表中行的 URL

如果你想要详细定义 Grid 表中行的 URL, 比如,当用户点击 Grid 表中的任意一行,它将跳转到指定的页面,可以添加如下方法到 Grid.php 文件中

1234public function getRowUrl($row){ return $this->getUrl('*/*/edit', array('id' => $row->getId()));}
这个方法还是比较容易看懂的, URL 就是你 controller 中的 editAction 方法,它将行的 id 号作为参数进行传递列_下拉框 – Drop Down Column Type假设,你想要在 Grid 表中添加一个下拉框,比如你想要添加如下图片中显示的:

你可以使用如下方法:

1

2

3

4

5

6

7

$this->addColumn('dropdown1',
array(

'header'
=>
Mage::helper('employee')->__('Employee
Type'),

'align' =>
'left',

'index' =>
'type',

'type'
=>
'options',

'options' =>
array('1'
=>
'Normal',
'2'
=>
'Admin'
,
'3'
=>
'Guest')

));

批量操作 – Mass Actions

批量操作功能十分有用,并且使用频率也非常高, 下图所展示的就是批量操作





通常应用于多行被同时选中时,可将下列代码添加到 Grid.php 文件中

123456789101112131415161718192021222324252627282930protected function _prepareMassaction(){ $this->setMassactionIdField('employee_id'); $this->getMassactionBlock()->setFormFieldName('employee'); $this->getMassactionBlock()->addItem('delete', array( 'label' => Mage::helper('employee')->__('Delete'), 'url' => $this->getUrl('*/*/massDelete'), 'confirm' => Mage::helper('employee')->__('Are you sure?'), )); $statuses = Mage::getSingleton('employee/status')->getOptionArray(); array_unshift($statuses, array('label'=>'', 'value'=>'')); $this->getMassactionBlock()->addItem('status', array( 'label' => Mage::helper('employee')->__('Change status'), 'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)), 'additional' => array( 'visibility' => array( 'name' => 'status', 'type' => 'select', 'class' => 'required-entry', 'label' => Mage::helper('employee')->__('Status'), 'values' => $statuses ) ) )); return $this;}
这里我们先来看下:

1

2

'url'
=>
$this->getUrl('*/*/massDelete') ==> 给
URL
指定了一个方法

'confirm'
=>
Mage::helper('employee')->__('Are
you sure?') ==> 这个就是在提交前的
confirm
提示框效果,做再次确认

在你的 controller 文件中,你需要去执行这些批量操作,同样的你也需要执行单个删除操作 deleteAction, 添加至 EmployeeController.php 文件中:

123456789101112131415161718192021222324252627282930313233343536373839public function deleteAction() { if( $this->getRequest()->getParam('id') > 0 ) { try { $model = Mage::getModel('employee/employee'); $model->setId($this->getRequest()->getParam('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('*/*/');} public function massDeleteAction() { $ids = $this->getRequest()->getParam('employee'); if(!is_array($ids)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)')); } else { try { foreach ($ids as $id) { $employee = Mage::getModel('employee/employee')->load($id); $employee->delete(); } Mage::getSingleton('adminhtml/session')->addSuccess( Mage::helper('adminhtml')->__( 'Total of %d record(s) were successfully deleted', count($ids) ) ); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirect('*/*/index');}
如果想你在 Grid 表中添加 导入/导出 选项,如下图所展示:

我们只需要将这两行代码添加至 _prepareColumns() 方法中

1

2

$this->addExportType('*/*/exportCsv',
Mage::helper('employee')->__('CSV'));

$this->addExportType('*/*/exportXml',
Mage::helper('employee')->__('XML'));

同样你需要在 controller 中为此添加对应的方法

12345678910111213public function exportCsvAction(){ $fileName = 'employee.csv'; $content = $this->getLayout()->createBlock('employee/adminhtml_employee_grid')->getCsv(); $this->_sendUploadResponse($fileName, $content);} public function exportXmlAction(){ $fileName = 'employee.xml'; $content = $this->getLayout()->createBlock('employee/adminhtml_employee_grid')->getXml(); $this->_sendUploadResponse($fileName, $content);}
Grid 表 ==> 列中的搜索和过滤 – Custom Search/Filter For a ColumnGrid 表中有那么单独一列,你希望搜索功能以不同的方式工作。例如它的 collection 是多表联查的集合,对于列的索引,你希望搜索功能工作起来不同或完全不工作你需要在 Grid.php 中重写这个 protected 方法 _addColumnFilterToCollection($column):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

protected
function
_addColumnFilterToCollection($column)

{

if
($this->getCollection())
{

if
($column->getId()
==
'websites')
{

$this->getCollection()->joinField('websites',

'catalog/product_website',

'website_id',

'product_id=entity_id',

null,

'left');

}

}

return
parent::_addColumnFilterToCollection($column);

}

基于 Ajax 的 Grid

在你 Grid 中, 如果你希望 搜索/分页 和其他一些操作都是基于 Ajax 工作,那么你就要在你的 Grid.php 文件中的 __construct() 方法中添加:

12$this->setSaveParametersInSession(true);$this->setUseAjax(true);
并同时添加另外一个方法:

1

2

3

4

public
function
getGridUrl()

{

return
$this->getUrl('*/*/grid',
array('_current'=>true));

}

这个 URL 将会被称作 Ajax 请求,为了获得 Grid 的内容,你需要在 EmployeeContrller.php 文件中添加:

1234567public function gridAction(){ $this->loadLayout(); $this->getResponse()->setBody( $this->getLayout()->createBlock('employee/adminhtml_employee_grid')->toHtml() );}
在 Grid 中添加不同的按钮在默认的 Grid 表中,你只有一个 “Add” 按钮,就像我们所演示的 “Add Employee”,如果你需要添加多个按钮时,如下图:

真的要做到,那么我们需要修改 Excellence/Employee/Block/Adminhtml/Employee.php 文件中的 __construct() 方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public
function
__construct()

{

$this->_controller
=
'adminhtml_employee';

$this->_blockGroup
=
'employee';

$this->_headerText
=
Mage::helper('employee')->__('Employee
Manager');

$this->_addButtonLabel
=
Mage::helper('employee')->__('Add
Employee');

$this->_addButton('button1',
array(

'label'
=>
Mage::helper('employee')->__('Button
Label1'),

'onclick'
=>
'setLocation(\''
.
$this->getUrl('*/*/button1')
.'\')',

'class'
=>
'add',

));

$this->_addButton('button2',
array(

'label'
=>
Mage::helper('employee')->__('Button
Label2'),

'onclick'
=>
'setLocation(\''
.
$this->getUrl('*/*/button2')
.'\')',

'class'
=>
'remove',

));

parent::__construct();

}

改变默认分页的 Size

你需要在 Grid.php 文件中重写 _preparePage() 这个方法,

123456789protected function _preparePage(){ $this->getCollection()->setPageSize( $this->getParam($this->getVarNameLimit(), $this->_defaultLimit) ); $this->getCollection()->setCurPage( $this->getParam($this->getVarNamePage(), $this->_defaultPage) );}
现在你可以根据你自己的需要设置 $this->_defaultLimit 和 $this->_defaultPage为 Grid 表中的行添加不同的选项如图所示:



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

$this->addColumn('action',

array(

'header'
=>
Mage::helper('employee')->__('Action'),

'type'
=>
'action',

'getter'
=>
'getId',

'actions' =>
array(

array(

'caption'
=> Mage::helper('employee')->__('Edit'),

'url'
=> $this->getUrl("*/*/edit"),

'field'
=> 'id'

),

array(

'caption'
=> Mage::helper('employee')->__('Delete'),

'url'
=> $this->getUrl("*/*/delete"),

'field'
=> 'id'

)

),

'filter' => false,

'sortable' => false

)

);

在 Grid 表中,我们还可以自定义其他很多东西,你需要打开 Mage_Adminhtml_Block_Widget_Grid 这个类进行更多的探索和研究

source: http://www.sunzhenghua.com/magento-admin-module-development-part2-grid-forms-tabs-addbutton
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: