我创建了一个下拉列表选择列表自定义字段,这是joomla文档中教授的内容,但我不知道在组件模板中调用此字段。
文档中建议的代码是这样的,
//Get custom field
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$cities = JFormHelper::loadFieldType('City', false);
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!
但它如何显示为选择形式?像
<select>
<option></option>
<option></option>
<option></option>
</select>
字段定义代码如下:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
jimport('joomla.form.formfield');
// The class name must always be the same as the filename (in camel case)
class JFormFieldRank extends JFormField {
//The field class must know its own type through the variable $type.
protected $type = 'Rank';
public function getLabel() {
// code that returns HTML that will be shown as the label
}
public function getInput() {
// code that returns HTML that will be shown as the form field
return '<select id="'.$this->id.'" name="'.$this->name.'">'.
'<option value="0" >00 President</option>'.
'<option value="1" >01 Vice President</option>'.
'<option value="10" >10 General Secretary</option>'.
'<option value="20" >20 Financial Chair</option>'.
'<option value="21" >21 Financial Deputy Chair</option>'.
'<option value="30" >30 Academics Chair</option>'.
'<option value="31" >31 Academics Deputy Chair</option>'.
'<option value="40" >40 Public Relation Chair</option>'.
'<option value="41" >41 Public Relation Deputy Chair</option>'.
'<option value="50" >50 Publicity Chair</option>'.
'<option value="51" >51 Publicity Deputy Chair</option>'.
'<option value="60" >60 Social and Sports Chair</option>'.
'<option value="61" >61 Social and Sports Deputy Chair</option>'.
'<option value="70" >70 Logistics and Welfare Chair</option>'.
'<option value="71" >71 Logistics and Welfare Deputy Chair</option>'.
'<option value="80" >80 IPSSSCB Advisor</option>'.
'</select>';
}
}
二,表单文件夹中的xml文件,应该是什么名字?与自定义字段名称相同?这是 rank.xml ?
并在xml文件中,如下所示:我可以更改名称,标签,描述标签吗?
<field name="title" type="Rank" label="JGLOBAL_TITLE"
description="JFIELD_TITLE_DESC"
required="true" />
<fieldset addfieldpath="/administrator/components/com_committee/models/fields">
谢谢!
答案 0 :(得分:0)
对我来说,这看起来像一个完全正常的列表字段,为什么要将其定义为自定义字段?也就是说type =“list”然后把你的xml放在xml文件中,就像任何列表字段一样(核心表单中有几十个例子)。
<field name="title" type="list" label="COM_MYCOMPONENT_FIELD_TITLE_LABEL"
description="COM_MYCOMPONNET_TITLE_DESC"
required="true" >
<option value="0" >00 President</option>
<option value="1" >01 Vice President</option>
<option value="10" >10 General Secretary</option>
<option value="20" >20 Financial Chair</option>
<option value="21" >21 Financial Deputy Chair</option>
<option value="30" >30 Academics Chair</option>
...
<option value="80" >80 IPSSSCB Advisor</option>
</field>
当然,您可以更改文本,这就是将其作为字符串键的重点,您可以在组件语言文件中创建自己的键。再次查看核心表单中的任何表单字段。最佳做法是使用语言键来表示值。