2

我已经为一个模块创建了我的自定义表单字段。但是,它会起作用,但是当我回到模块时,我不知道以前的值或保存的值是什么,因为我没有在那里选择它。

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

class JFormFieldSlidercategory extends JFormField {

    protected $type = 'Slidercategory';

    // getLabel() left out

    public function getInput() {

                $db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query->select('id,title');
                $query->from('#__h2mslider_categories');
                $db->setQuery((string)$query);
                $messages = $db->loadObjectList();
                $options ='';


                if ($messages)
                {
                        foreach($messages as $message) 
                        {
                                $options .= '<option value="'.$message->id.'" >'.$message->title.'</option>';
                        }
                }


                $options = '<select id="'.$this->id.'" name="'.$this->name.'">'.
                       '<option value="0" >--select a category--</option>'.
                       $options.
                       '</select>';

                return $options ;


    }
}

我需要返回保存值的函数。

4

1 回答 1

3

你可以使用这个来获得价值- $this->value

或者你可以试试这个代码选择框 -

// No direct access to this file
defined('_JEXEC') or die;

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

class JFormFieldSlidercategory extends JFormFieldList
{
    /**
     * The field type.
     *
     * @var     string
     */
    protected $type = 'Slidercategory';

    /**
     * Method to get a list of options for a list input.
     *
     * @return  array       An array of JHtml options.
     */
    protected function getOptions() 
    {
        $db = JFactory::getDBO();
            $query = $db->getQuery(true);
            $query->select('id,title');
            $query->from('#__h2mslider_categories');
            $db->setQuery((string)$query);
            $messages = $db->loadObjectList();

        $options = array();
        if ($messages)
        {
            foreach($messages as $message) 
            {
                $options[] = JHtml::_('select.option', $message->id, $message->title);
            }
        }

        $options = array_merge(parent::getOptions(), $options);     
        return $options;
    }
}
于 2013-01-08T08:51:42.033 回答