How to use joomla JHTML::_('select.genericlist') with selected option

7,052 views
Skip to first unread message

Fejér Ferenc

unread,
Sep 28, 2013, 10:10:19 AM9/28/13
to joomla-de...@googlegroups.com
I try for a while to understand how can I add selected option to my dropdownlist my code looks as it follows. I create an option array than I genrate the list, on the next step I check if already has the a filed a selected option if yes I would like show as <option value = "my_value" selected="my_selected">My selected</option>


The code above won't do that

    public function getInput() {
       
        $spec_fields = array();
       
        $jinput = JFactory::getApplication()->input;
        $id = $jinput->get('id');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
                ->select('*')
                ->from('#__immobilien_cat')
                ->order('cat_name');
        $db->setQuery($query);
        $rows = $db->loadObjectList();

        if (isset($id)) {
    
        $query = $db->getQuery(true)
                ->select('cat.id,cat.cat_name')
                ->from('#__imobillien AS immo')
                ->join('LEFT', '#__immobilien_cat AS cat ON immo.category_id = cat.id')
                ->where('immo.id = ' . $id);
        $db->setQuery($query);
        $actual_row = $db->loadRow();
        var_dump($actual_row[1]);
        }
       
        $options[] = JHTML::_('select.option','',JText::_('COM_IMMOS_SELECT_CAT'));
        foreach ($rows as $key => $value) {
            $options[] = JHTML::_('select.option', $value->id, JText::_($value->cat_name));    
        }

        //return JHTML::_('select.genericlist', $options,'jform[category]', 'class="nourritures"','value','text',$actual_row[1]);
        return JHTML::_('select.genericlist', $options, 'jform[category]', 'class = "nourritures"', 'value','text', 'Villa' );
             

    }

Viper

unread,
Sep 28, 2013, 10:29:04 AM9/28/13
to joomla-de...@googlegroups.com
http://api.joomla.org/Joomla-Platform/HTML/JHtmlSelect.html#genericlist
mixed $selected: The key that is selected (accepts an array or a string).

Fejér Ferenc

unread,
Oct 24, 2013, 4:46:32 AM10/24/13
to joomla-de...@googlegroups.com
Can you please give me an example how should that look with my code I was trying over and over but can get it work

Bakual

unread,
Oct 24, 2013, 6:28:28 AM10/24/13
to joomla-de...@googlegroups.com
You don't need the foreach to iterate over the $rows. Just pass the objectlist as a parameter to JHtmlSelect and it will do the magic.
You then specify which properties of the object should be used as value and text, text will also be translated by default.
The $selected has to contain the id of the selected value. JHtml will then assign it itself.

JHtml::_('select.genericlist', $rows, 'jform[category]', 'class = "nourritures"', 'id','cat_name', $row->id);

Fejér Ferenc

unread,
Oct 24, 2013, 6:57:38 AM10/24/13
to joomla-de...@googlegroups.com
thank you thank you thank you!
Reply all
Reply to author
Forward
0 new messages