0

如何做到这一点,以便在我编辑条目时,为我的自定义字段类型选择正确的值?到目前为止我有这个:

class JFormFieldCustom extends JFormField {

    protected $type = 'Custom';

    // getLabel() left out

    public function getInput() {

            return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                        '<option value="1" >1</option>'.
                        '<option value="2" >2</option>'.
                    '</select>';
    }

}

如何将选定的值传递给此类,以便可以执行以下操作:

<option value="1"SELECTED>1</option> 

或者

<option value="2" SELECTED>2</option>

谢谢!

4

3 回答 3

4

使用已经存在的东西更容易,即扩展JFormFieldList代替JFormField,然后您所要做的就是option's为您的列表返回 。继承的功能将为您完成其余的工作 - 包括选择匹配的选项$this->value

<?php
/**
 * Do the Joomla! security check and get the FormHelper to load the class
 */
defined('_JEXEC') or die('Restricted Access');

JFormHelper::loadFieldClass('list');

class JFormFieldMyCustomField extends JFormFieldList
{
    /**
     * Element name
     *
     * @var     string
     */
    public  $type = 'MyCustomField';

    /**
     * getOptions() provides the options for the select
     *
     * @return  array
     */
    protected function getOptions()
    {
        // Create an array for our options
        $options = array();
        // Add our options to the array
        $options[] = array("value" => 1, "text" => "1);
        $options[] = array("value" => 1, "text" => "1);
        return $options;
    }
}
于 2013-03-25T08:39:44.990 回答
0

用于$this->value获取选定的值。试试这个-

 class JFormFieldCustom extends JFormField {

        protected $type = 'Custom';

        // getLabel() left out

        public function getInput() {

                return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                            '<option value="1" <?php echo ($this->value==1)?'selected':''?>>1</option>'.
                            '<option value="2" <?php echo ($this->value==2)?'selected':''?>>2</option>'.
                        '</select>';
        }
    }

希望这会有所帮助。

于 2013-03-25T07:05:33.943 回答
0

选择 Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL * @copyright (c) 2017 YouTech Company。版权所有。* @author macasin */ 定义('_JEXEC')或死;

JFormHelper::loadFieldClass('list');

类 JFormFieldSelect 扩展 JFormFieldList { protected $type = 'select';

protected function getInput()
{
    $html = array();
    $attr = '';

    // Initialize some field attributes.
    $attr .= !empty($this->class) ? ' class=select ' . $this->class . '"' : ' class=select ';
    $attr .= $this->readonly ? ' readonly' : '';
    $attr .= $this->disabled ? ' disabled' : '';
    $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
    $attr .= $this->required ? ' required aria-required="true"' : '';

    // Initialize JavaScript field attributes.
    $attr .= $this->onchange ? ' onchange="' . $this->onchange . '"' : '';

    // Get the field options.
    $options = $this->getOptions();

    // Load the combobox behavior.
    JHtml::_('behavior.combobox');

    $html[] = '<div class="select input-append">';

    // Build the input for the combo box.
    $html[] = '<select name="' . $this->name . '" id="' . $this->id . '" value="'
        . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $attr . ' autocomplete="off" >';

    foreach ($options as $option)
    {
        $html[] = '<option '.($option->value == $this->value ? "selected" : "").' value='.$option->value.'>' . $option->text . '</option>';

    }
    $html[] = '</select></div>';

    return implode($html);
}

}

于 2017-01-13T12:49:58.037 回答