Populate a combo box

1,475 views
Skip to first unread message

Francesco Salvini

unread,
Jul 5, 2011, 4:32:19 AM7/5/11
to joomla-...@googlegroups.com
Hi all, I'm new about Joomla developing and I'm trying to build a new component for my needs.

I wanted to know how I can create and populate a combo item to let user choose in the administrator side some possibility about a field value.

For example, a user has to specify a category of an object between some other table-fields values. I read the page at this link http://docs.joomla.org/Form_field but it doesn't say anything about populate comboboxes. Do you have any suggestion for links about component developing and articles about populate a combobox?

Thanks so much to all for any suggestion!

Regards
Francesco

Dave Havard

unread,
Jul 5, 2011, 4:39:54 AM7/5/11
to joomla-...@googlegroups.com
Hi Francesco


Take a look in administrator/components/com_categories/models/fields/categoryparent.php for an example.

Best Regards
Dave

--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-cms/-/uTAVGTtZsGwJ.
To post to this group, send an email to joomla-...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-cm...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-cms?hl=en-GB.

Francesco Salvini

unread,
Jul 5, 2011, 8:48:55 AM7/5/11
to joomla-...@googlegroups.com
Hi Dave

2011/7/5 Dave Havard <david....@gmail.com>

Hi Francesco


Take a look in administrator/components/com_categories/models/fields/categoryparent.php for an example.

Thanks a lot! With your suggest I'm now able to construct lists with items inside. But if I want to construct specifically combo boxes, how can I do? I supposed to extend the JFormFieldCombo instead of the JFormFieldList class (http://docs.joomla.org/JFormFieldCombo/1.6), but it doesn't work. Do anyone know where I am wrong?

Thanks again a lot!

Regards
Francesco
 

Dave Havard

unread,
Jul 5, 2011, 9:01:54 AM7/5/11
to joomla-...@googlegroups.com
Hi Francesco

If extending the JFormFieldCombo you most likely just want to only implement the getOptions method and leave the getInput alone. So something like:

class MyComboField extends JFormFieldCombo
{
    public $type = 'MyCombo';
    protected function getOptions()
    {
        // Call db and build your options here:
    }
}

getInput will then call your getOptions and do it's magic.

Dave

Francesco Salvini

unread,
Jul 5, 2011, 9:32:39 AM7/5/11
to joomla-...@googlegroups.com
Hi Dave

2011/7/5 Dave Havard <david....@gmail.com>

If extending the JFormFieldCombo you most likely just want to only implement the getOptions method and leave the getInput alone. So something like:

class MyComboField extends JFormFieldCombo
{
    public $type = 'MyCombo';
    protected function getOptions()
    {
        // Call db and build your options here:
    }
}

getInput will then call your getOptions and do it's magic.

I did a similar thing before replying here. That's what I've written:

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
jimport('joomla.form.helper');
jimport( 'joomla.form.fields.combo' );
JFormHelper::loadFieldClass('list');
JFormHelper::loadFieldClass('combo');

class JFormFieldMyCombo extends JFormFieldCombo {

    public $type = 'MyCombo';
   
    protected function getOptions() {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from('#__sm_squadre');
        $db->setQuery((string)$query);
        $messages = $db->loadObjectList();
        $options = array();
        if ($messages)
        {
            foreach($messages as $message)
            {
                $options[] = JHtml::_('select.option', $message->id, $message->descrizione);
            }
        }
        $options = array_merge(parent::getOptions(), $options);
        return $options;

    }
}

?>

and I recall it in the xml file with type='MyCombo' with this XML code:

<field
            name="id_categoria_sportiva"
            type="MyCombo"
            label="COM_SANCAMANAGER_SANCAMANAGER_ID_CATEGORIA_SPORTIVA_LABEL"
            description="COM_SANCAMANAGER_SANCAMANAGER_ID_CATEGORIA_SPORTIVA_DESC"
            size="5"
        />

But on the screen, I visualize only a simple text field and not a combobox field.

Do you have some other suggestions to solve this problem? Where I mistake?

Thanks a lot!

Regards
Francesco

Dave Havard

unread,
Jul 5, 2011, 10:19:21 AM7/5/11
to joomla-...@googlegroups.com
Hi Francesco

Well, there is nothing wrong with your code. Have just had a look through the JFormFieldCombo and the associated javascript and it appears to be broken. The javascript is looking for any select elements with a class of combobox which it will then customise, however the JFormFieldCombo code outputs a UL element instead.

You can fix this by changing your JFormFieldCombo::getInput to look like this:

protected function getInput()
{
// Initialize variables.
$html = array();
$attr = '';

// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="combobox '.(string) $this->element['class'].'"' : ' class="combobox"';
$attr .= ((string) $this->element['readonly'] == 'true') ? ' readonly="readonly"' : '';
$attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';

// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';

// Get the field options.
$options = $this->getOptions();

// Load the combobox behavior.
JHtml::_('behavior.combobox');

// Build the list for the combo box.
$html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);

return implode($html);
}

In essence I have just changed it to no longer output the text field and ul element and just render a select element. I have tested this an it works as expected.

Rather than change the core for now you might want to consider just overriding the getInput code with this version in your MyCombo class.

Does anyone know if this is a known bug or are we both missing something obvious? Happy to submit patch.

Thanks
Dave

--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.

elin

unread,
Jul 5, 2011, 11:42:20 AM7/5/11
to joomla-...@googlegroups.com
I think there is definitely an issue in comboBox BUT you can look at how the moduleposition field in com_modules to see how it is handled there, which is with a combination of a blank text field that you can type in and a modal selector.

I didn't try it, but if that works it would be great for you to put it two places, in the CMS tracker as a patch and then in github.

Elin

Francesco Salvini

unread,
Jul 6, 2011, 9:40:55 AM7/6/11
to joomla-...@googlegroups.com
Hi Dave

2011/7/5 Dave Havard <david....@gmail.com>
Hi Francesco

Well, there is nothing wrong with your code. Have just had a look through the JFormFieldCombo and the associated javascript and it appears to be broken. The javascript is looking for any select elements with a class of combobox which it will then customise, however the JFormFieldCombo code outputs a UL element instead.

You can fix this by changing your JFormFieldCombo::getInput to look like this:

protected function getInput()
{
// Initialize variables.
$html = array();
$attr = '';

// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="combobox '.(string) $this->element['class'].'"' : ' class="combobox"';
$attr .= ((string) $this->element['readonly'] == 'true') ? ' readonly="readonly"' : '';
$attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';

// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';

// Get the field options.
$options = $this->getOptions();

// Load the combobox behavior.
JHtml::_('behavior.combobox');

// Build the list for the combo box.
$html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);

return implode($html);
}

In essence I have just changed it to no longer output the text field and ul element and just render a select element. I have tested this an it works as expected.

Thanks for the suggestion: I tried it, changing the code of the method getInput in libraries/joomla/form/field/combo.php file, but it doesn't work for me. Surely I mistaked something else, but on my screen I continue seeing a blank textbox, without any text inside.

Thanks for all your suggestions.

Regards
Francesco

Dave Havard

unread,
Jul 6, 2011, 10:37:26 AM7/6/11
to joomla-...@googlegroups.com
Looks like you might have done something wrong since the code is not outputting a text field any more. Can you post your MyCombo control file please?

--

Francesco Salvini

unread,
Jul 6, 2011, 11:10:13 AM7/6/11
to joomla-...@googlegroups.com
That's the code I used.

The MyCombo is called IdCategoria and that's the code of admin/models/fields/IdCategoria.php:


<?php
// No direct access to this file
defined('_JEXEC') or die;
 
// import the list field type

jimport('joomla.form.helper');
jimport( 'joomla.form.fields.combo' );

JFormHelper::loadFieldClass('combo');
 
/**
 * SancaManager Form Field class for the SancaManager component
 */
class JFormFieldIdCategoria extends JFormFieldCombo
{
    /**
     * The field type.
     *
     * @var        string
     */
    public $type = 'IdCategoria';
 
    /**
     * 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('a.s_id AS id_stagione, a.s_name AS anno, b.name AS categoria_squadre');
        $query->from('#__bl_seasons AS a');
                $query->join('', '#__bl_tournament AS b ON a.t_id = b.id');
        $db->setQuery((string)$query);
        $stagioni = $db->loadObjectList();
        $options = array();
        if ($stagioni)
        {
            foreach($stagioni as $stagione)
            {
                $options[] = JHtml::_('select.option', $stagione->id_stagione, $stagione->anno.' - '.$stagione->categoria_squadre);

            }
        }
        $options = array_merge(parent::getOptions(), $options);
        return $options;
    }
}
?>

I recall this field in the XML file with

<field
            name="id_categoria_sportiva"
            type="IdCategoria"

            label="COM_SANCAMANAGER_SANCAMANAGER_ID_CATEGORIA_SPORTIVA_LABEL"
            description="COM_SANCAMANAGER_SANCAMANAGER_ID_CATEGORIA_SPORTIVA_DESC"
            size="5"
        />

and I modified the JFormFieldCombo::getInput() in libraries/joomla/form/fields/combo.php with your code


// Initialize variables.
$html = array();
$attr = '';

// Initialize some field attributes.
$attr .= $this->element['class'] ? ' class="combobox '.(string) $this->element['class'].'"' : ' class="combobox"';
$attr .= ((string) $this->element['readonly'] == 'true') ? ' readonly="readonly"' : '';
$attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$attr .= $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';

// Initialize JavaScript field attributes.
$attr .= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';

// Get the field options.
$options = $this->getOptions();

// Load the combobox behavior.
JHtml::_('behavior.combobox');

// Build the list for the combo box.
$html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);

return implode($html);

I have to say that now, preparing this post, I retried to use this combo field, and now the blank text field doesn't appear. Now I see a list of items, where the first is "type custom..." (and I don't know why) and then there are all the strings I want, but the object created is a simple list, not a combo box.

Thanks a lot for all your help!

Dave Havard

unread,
Jul 6, 2011, 11:29:35 AM7/6/11
to joomla-...@googlegroups.com
Remove the size="5" attribute from your xml and you are good to go.

Francesco Salvini

unread,
Jul 6, 2011, 11:33:34 AM7/6/11
to joomla-...@googlegroups.com
Hi Dave

2011/7/6 Dave Havard <david....@gmail.com>

Remove the size="5" attribute from your xml and you are good to go.

Finally it works, thank you so much!! :-)

Only a last thing: according to you why in the combo appears also the "type custom..." label?

Dave Havard

unread,
Jul 6, 2011, 11:47:14 AM7/6/11
to joomla-...@googlegroups.com

I think if no value exists that will appear to give the user a prompt for they should do.

Francesco Salvini

unread,
Jul 6, 2011, 12:05:43 PM7/6/11
to joomla-...@googlegroups.com
Ok, thanks a lot again!

Regards
Francesco

2011/7/6 Dave Havard <david....@gmail.com>

elin

unread,
Jul 6, 2011, 12:09:43 PM7/6/11
to joomla-...@googlegroups.com
Something is going wrong with the $html[]  inside the $options as $option in the core. That's why it is not building the list though the html  is there.

Elin

elin

unread,
Jul 6, 2011, 12:57:10 PM7/6/11
to joomla-...@googlegroups.com
Custom is for the combo type ... i.e. you will do a custom value rather than one on the list.

Elin
Reply all
Reply to author
Forward
0 new messages