在这一章节中,我们将会了解关于 Magento 后台表单更多延续的操作
在之前的一些章节里,我们仅仅只是了解了下后台表单的基础操作, 所以在这次旅程中我们将熟悉更多关于后台表单的操作
为表单 容器/视窗 添加按钮
在表单里,我们可以根据自己的需求添加多个按钮, 如下图所示,我在表单里额外添加了4个按钮
首先我们修改下上次建立过的 Excellence_Employee_Block_Adminhtml_Employee_Edit.php, 代码如下:
上面代码中我们使用到的方法是 $this->addButton, 让我们现在来详细看下各个参数的作用
添加多个表单选项卡和基于 Ajax 的选项卡
添加多个选项卡,那么我们就要修改下之前我们所建立的 Tabs.php 文件了
正如如上代码所示,添加一个正常的 html 选项卡所需要的操作是:
代码十分简单,我们只是定义了一下 label, title 和所对应的 content 内容
而一个基于 Ajax 的选项卡,那就有一点复杂了
代码中我们定义了 ‘class’ 的值为 ‘ajax’,并且用 ajax url 去加载所对应的内容,在我们的后台控制器里, 这个 URL 会对应我们的 ajaxAction, 代码如下:
通常,我们会用基于 Ajax 的选项卡 – 所对应的内容区域里面加载 Grid 表, 如何在选项卡所对应的内容区域里加载 Grid 表,请见后一章节
创建新的文本标签
在上一章节中,我们已经看到了在 Magento 表单中所有不同类型的 文本框 或 HTML 标签, 现在让我们看下如何来添加一个新的类型,首先添加如下代码至 Form.php 文件中的 _prepareForm() 方法当中:
在这里,我们调用了 addType() 方法, 并且定义了该标签的唯一 id 和 class 文件,下一步就是来为其添加标签
在 addField() 方法中我们使用了 custom_field 作为一个新的标签类型,我们也同时定义了两个变量 custom1 和 custom2, 这么做只是为了演示如何把变量传到 custom field 类中, 接下去我们需要创建 custom field 类了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class
Excellence_Employee_Block_Adminhtml_Employee_Edit_Tab_Field_Custom
extends
Varien_Data_Form_Element_Abstract
{
public
function
__construct
(
$attributes
=
array
(
)
)
{
parent
::
__construct
(
$attributes
)
;
}
public
function
getElementHtml
(
)
{
$value
=
$this
->
getValue
(
)
;
$custom1
=
$this
->
getCustom1
(
)
;
$custom2
=
$this
->
getCustom2
(
)
;
$html
=
'<p id="'
.
$this
->
getHtmlId
(
)
.
'"'
.
$this
->
serialize
(
$this
->
getHtmlAttributes
(
)
)
.
'>在这里我可以放自定义的 html 或 javascript</p>'
;
$html
.
=
"<p>这里就可以接收我们刚传的变量了 </p>"
;
$html
.
=
"<b>Custom1:</b> $custom1 <br/>"
;
$html
.
=
"<b>Custom2:</b> $custom2 <br/>"
;
$html
.
=
$this
->
getAfterElementHtml
(
)
;
return
$html
;
}
}
|
这里值得注意的是,我们一定要继承 Varien_Data_Form_Element_Abstract 这个类,并且要重写 getElementHtml() 这个方法,那样你就可以写你自定义的 html 代码了
source: http://www.sunzhenghua.com/magento-admin-module-development-part5-grid-forms-tabs-ajax-advanced