Multi-select list--- How to do

1,253 views
Skip to first unread message

geeffland

unread,
Dec 7, 2010, 1:05:06 PM12/7/10
to Joomla! CMS Development
Any good advice on how to do a multi-select list for an admin view?...
not sure the admin part is significant...

I had a view which loads a form and in the form I have

<field name="testlist" type="list" multiple="multiple" default=""
label="test label"
description="test desc" >
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
</field>

Then in the template I do the typical $this->form->getLabel and $this-
>form->getInput functions

Then in the database I save the field "testlist" as "1, 2, 3"... I
added some code in the table file that writes the array as an
imploded(", ", $opt) type... which works... the problem I have is how
do I get the list to show the proper selections when it renders? It
always comes out with nothing selected...

Is there a different data format I need to use in the DB? or some
other code missing?

I found some cases in modules that use multi-select lists but no
components.

Thanks,
Greg

Mark Dexter

unread,
Dec 7, 2010, 3:47:08 PM12/7/10
to joomla-...@googlegroups.com
I think you just add multiple="multiple" as an attribute in the
<field> element. For example;

<field name="testlist" type="list" multiple="multiple" default=""

label="test label" multiple="multiple"
description="test desc" >

Mark

> --
> You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
> 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.
>
>

Phil Snell

unread,
Dec 7, 2010, 3:52:45 PM12/7/10
to joomla-...@googlegroups.com
make sure the field name also has [] at the end, for array. like testlist[]

Christophe Demko

unread,
Dec 7, 2010, 5:43:24 PM12/7/10
to Joomla! CMS Development
You can have multiple="true" or multiple="multiple"

The adding of [] at the end of the field is not necessary (see line
483 of formfield.php)

On 7 déc, 21:52, Phil Snell <p...@snellcode.com> wrote:
> make sure the field name also has [] at the end, for array. like testlist[]
> Mark Dexter wrote:I think you just add multiple="multiple" as an attribute in the <field> element. For example; <field name="testlist" type="list" multiple="multiple" default="" label="test label" multiple="multiple" description="test desc" > Mark On Tue, Dec 7, 2010 at 10:05 AM, geeffland<gr...@effland.org>wrote:Any good advice on how to do a multi-select list for an admin view?... not sure the admin part is significant... I had a view which loads a form and in the form I have <field name="testlist" type="list" multiple="multiple" default=""   label="test label"   description="test desc" >   <option value="1">Option 1</option>   <option value="2">Option 2</option>   <option value="3">Option 3</option>   <option value="4">Option 4</option>   <option value="5">Option 5</option> </field> Then in the template I do the typical $this->form->getLabel and $this-form->getInput functionsThen in the database I save the field "testlist" as "1, 2, 3"... I added some code in the table file that writes the array as an imploded(", ", $opt) type... which works...  the problem I have is how do I get the list to show the proper selections when it renders?  It always comes out with nothing selected... Is there a different data format I need to use in the DB?  or some other code missing? I found some cases in modules that use multi-select lists but no components. Thanks, Greg -- You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group. To post to this group, send an email tojoomla...@googlegroups.com. To unsubscribe from this group, send email tojoomla-dev-c...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/joomla-dev-cms?hl=en-GB.

geeffland

unread,
Dec 8, 2010, 9:10:51 AM12/8/10
to Joomla! CMS Development
I guess that is where my question originates... I have added the
multiple="multiple" in the form xml file. The html displays correctly
in such that it shows a multi-select list... But the items are never
selected. I will try adding the [] to see what that does. Since they
were not selected I wondered if it was a bug or something I missed.

On Dec 7, 4:43 pm, Christophe Demko <chde...@gmail.com> wrote:
> You can have multiple="true" or multiple="multiple"
>
> The adding of [] at the end of the field is not necessary (see line
> 483 of formfield.php)
>
> On 7 déc, 21:52, Phil Snell <p...@snellcode.com> wrote:
>
>
>
>
>
>
>
> > make sure the field name also has [] at the end, for array. like testlist[]
> > Mark Dexter wrote:I think you just add multiple="multiple" as an attribute in the <field> element. For example; <field name="testlist" type="list" multiple="multiple" default="" label="test label" multiple="multiple" description="test desc" > Mark On Tue, Dec 7, 2010 at 10:05 AM, geeffland<g...@effland.org>wrote:Any good advice on how to do a multi-select list for an admin view?... not sure the admin part is significant... I had a view which loads a form and in the form I have <field name="testlist" type="list" multiple="multiple" default=""   label="test label"   description="test desc" >   <option value="1">Option 1</option>   <option value="2">Option 2</option>   <option value="3">Option 3</option>   <option value="4">Option 4</option>   <option value="5">Option 5</option> </field> Then in the template I do the typical $this->form->getLabel and $this-form->getInput functionsThen in the database I save the field "testlist" as "1, 2, 3"... I added some code in the table file that writes the array as an imploded(", ", $opt) type... which works...  the problem I have is how do I get the list to show the proper selections when it renders?  It always comes out with nothing selected... Is there a different data format I need to use in the DB?  or some other code missing? I found some cases in modules that use multi-select lists but no components. Thanks, Greg -- You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group. To post to this group, send an email tojoomla...@googlegroups.com. To unsubscribe from this group, send email tojoomla-dev-c...@googlegroups.com. For more options, visit this group athttp://groups.google.com/group/joomla-dev-cms?hl=en-GB.

geeffland

unread,
Dec 8, 2010, 12:24:21 PM12/8/10
to Joomla! CMS Development
I just tried this in Beta 15... If I add the [] in the xml file like
name='testlist[]" then the list does not even get included on the edit
page.

On Dec 7, 4:43 pm, Christophe Demko <chde...@gmail.com> wrote:

Phil Snell

unread,
Dec 8, 2010, 12:36:37 PM12/8/10
to joomla-...@googlegroups.com
Oh yes, sorry I misread.  The html element is what needs the [], and hopefully that is what you get in the final output.

geeffland

unread,
Dec 8, 2010, 12:51:04 PM12/8/10
to Joomla! CMS Development
Looking thru the code I did see in formfield where is adds a '[]' on
the end of the name (although my form field is only 473 lines long).

I see looking thru the select.php file it appears to do some special
handling if the 'list.select' is an array... but in my case the
list.select is still "1, 2, 3, 4" as in the DB... Is there somewhere
else, maybe in the table file, where I need to convert the "1, 2, 3,
4" back to an array after reading the text from the database?

Phil Snell

unread,
Dec 8, 2010, 1:00:21 PM12/8/10
to joomla-...@googlegroups.com
you need to run explode(',', $string) on that data to make it an array.  Then you can pass it in as normal.

geeffland

unread,
Dec 8, 2010, 1:06:30 PM12/8/10
to Joomla! CMS Development
Where do I need to insert this code... I tried using that code in the
view.html.php file between the $this->item and the $this->form calls
by this:

$this->item = $this->get('Item');
$this->item->testlist = explode(', ', $this->item->testlist);
$this->form = $this->get('Form')

geeffland

unread,
Dec 8, 2010, 1:17:19 PM12/8/10
to Joomla! CMS Development
OK I found a place where this works...

In the file for the model after getting data from the $data = $this-
>getItem(); there I added the

$data->testlist = explode(', ', $data->testlist);

This function gets called during the form loading and is the point
which sets the internal protected data variable on the form. Doing
this appears to make the list select the selected items...

geeffland

unread,
Dec 8, 2010, 1:23:27 PM12/8/10
to Joomla! CMS Development
that looks to have solved the issue... Thanks for all of the
assistance. Here is a recap in case anyone else runs into this
issue...

Basic steps...
1) include multiple="multiple" or multiple="true" in the <field> node
for the form xml file
2) include in the model file, in the loadFormData function, an
explode(', ', $options) statement to change the text "1, 2, 3, 4"
values back into an array upon reading the form data.
3) include in the table file, in the check, bind, or store function,
an implode(', ', $options) statement to change the array to a comma
separated text string... For my case I put it in the check function...
probably really should go in the store function.
Reply all
Reply to author
Forward
0 new messages