繁体   English   中英

编辑时的Joomla自定义字段类型

[英]Joomla custom field type when editing

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

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>

谢谢!

使用已有的内容会更容易,例如,使用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;
    }
}

使用$this->value来获取选择的$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>';
        }
    }

希望这会有所帮助。

选择Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU / GPL * @版权所有(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);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM