2013-03-25 64 views
0

如何使它在编辑条目时选择了我的自定义字段类型的正确值?我有这个至今:Joomla编辑时的自定义字段类型

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> 

谢谢!

+0

当您使用此字段(例如,在你的组件项目PARAMATERS,在菜单..等) – Craig 2013-03-25 07:05:17

回答

0

使用$this->value获取所选value.Try这个 -

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>'; 
     } 
    } 

希望这会有所帮助。

3

它更容易使用的已经存在,即在地方的延伸JFormFieldJFormFieldList,那么所有你需要做的就是返回option's为您的列表。继承的功能将完成剩下的为您服务 - 包括选择匹配为Joomla http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL $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; 
    } 
} 
0

选择该选项* @copyright(c)2017年YouTech公司。版权所有。 ('_JEXEC')或死亡;或者,

JFormHelper :: loadFieldClass('list');

class JFormFieldSelect extends 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); 
} 

}

相关问题