Monday, April 15, 2013

Creating an optgroup dynamically


$results = $database->loadObjectList();
        $groups = array();
        foreach ($results as $result) {
            $groups[$result->regionname][$result->locid] = $result->locname;
        }
        foreach($groups as $label => $opt):
        $options[] = JHTML::_('select.optgroup', $label);
        foreach ($opt as $id => $name):
        $options[] = JHTML::_('select.option', $id, $name);
        endforeach;
        $options[] = JHTML::_('select.option', '');
        endforeach;
       
        JHTML::_('select.genericlist', $options, 'locid', 'class="chzn-select" style="width:122px;" tabindex="4"','value', 'text', '', false);
        $select = JHTML::_(
         'select.genericlist', // Because we are creating a 'select' element
         $options,             // The options we created above
         'locid',        // The name your select element should have in your HTML
         'size="1" ',          // Extra parameters to add to your element
         'value',              // The name of the object variable for the option value
         'text',               // The name of the object variable for the option text
         'selected_key',       // The key that is selected (accepts an array or a string)
         false                 // Translate the option results?
        );

   
        print_r($select);

No comments:

Post a Comment