问题描述
我在组件"/models/fields/time.php"中使用以下php创建了一个自定义字段类型:
I have a created a custom field type in my components "/models/fields/time.php" with the following php:
defined('JPATH_BASE') or die;
jimport('joomla.form.formfield');
class JFormFieldTime extends JFormField
{
protected $type = 'time';
public function getInput()
{
return '<select id="'.$this->id.'" name="'.$this->name.'">'.
'<option value="08:00:00" > 8:00 AM</option>'.
'<option value="09:30:00" > 9:30 AM</option>'.
'</select>';
}
}
和我的course.xml(/models/forms/course.xml)如下:
and my course.xml (/models/forms/course.xml) as such:
<field
name="starttime"
type="time"
label="COM_CEXPRESS_FORM_LBL_COURSE_STARTTIME"
description="COM_CEXPRESS_FORM_DESC_COURSE_STARTTIME"
required="true"
filter="safehtml" />
该表单将在数据库中保存正确的值(09:30:00),但是在显示该表单时(8:00 AM),未选择正确的值=已选择".但是,如果我将course.xml字段修改为:
The Form will save the correct value within the database (09:30:00), but the correct value isn't selected="selected" when the form is displayed (8:00 AM). However, if I modify the course.xml field to be:
<field
name="starttime"
type="list"
label="COM_CEXPRESS_FORM_LBL_COURSE_STARTTIME"
description="COM_CEXPRESS_FORM_DESC_COURSE_STARTTIME"
required="true"
filter="safehtml">
<option value="08:00:00" > 8:00 AM</option>
<option value="09:30:00" > 9:30 AM</option>
</field>
该表单将正确显示(上午9:30)所选"数据库值.我在此页面上使用了Joomla文档:
the form will correctly display (9:30 AM) the "selected" database value. I used the Joomla Docs per this page:
http://docs.joomla.org/Creating_a_custom_form_field_type
推荐答案
您必须自己在 getInput()中设置所选选项.您可以使用 $ this-> value 获取当前值.
You must set the selected option yourself in getInput(). You can get the current value with $this->value.
除了自己打印元素之外,您还可以使用JHTML:
Instead of printing out the element yourself you could also take use by JHTML:
public function getInput()
{
$options = array(
'08:00:00' => '8:00 AM',
'09:30:00' => '9:30 AM'
);
return JHtml::_('select.genericlist', $options, $this->name, null, 'value', 'text', $this->value, $this->id);
}
这篇关于未显示Joomla 2.5自定义字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!