Home

Dynamically Generating a Dropdown List

If you want to create a drop down list (or option set) dynamically, for example from a set of pages or DataObjects, you can use the DataObject::get() call along with the function toDropdownMap() to generate it like so:

Alternatively you can generate the list from a set of Enum values defined in your $db array like so:

Then use this syntax when adding your form field to the getCMSFields() function:

About the Author

Name: Aram Balakjian

Website: http://www.aabweb.co.uk

Aram is a web designer/developer running London based startup aab web. He recently went full time into web development after leaving his previous career in 3D animation behind.

Comments (10)

  • @aram: thanks

    Posted by thealien, 07/01/2010 1:14pm (2 months ago)

  • How to add more than one field to the dropdownmape.g Dataobject::get("DataObject")->toDropdownMap("ID", "Title-Surname")

    Posted by Motoni, 07/01/2010 3:17am (2 months ago)

  • sure, simply add an if statement to create the array then feed the array into the dropdown, something like:

    if(Dataobject::get("DataObject"))
    $Objects = Dataobject::get("DataObject")->toDropdownMap();
    else
    $Objects = array('No Objects');

    then in your dropdown definition:
    new DropdownField('Options', 'Choose an option', $Objects)

    if you didn't want to show the dropdown at all you could enclose the whole field def in the if statement.

    Aram

    Posted by aram, 06/01/2010 5:31am (2 months ago)

  • Hi,

    I guess there is missing a check somewhere, but I can't figure out how to get it in...

    As long as the underlying dataobject is not yet populated, the snippet is throwing an error: Fatal error: Call to a member function toDropdownMap() on a non-object in C:\wamp\www\ss234\settings\code\HeadDefaultimages.php on line 63

    Is there a way to check if the dataobject is already populated and bypassing the dropdown if not?

    thanks

    Posted by thealien, 05/01/2010 9:18am (2 months ago)

  • sorry to be such a pain Aram, but this is my progress - am i on the right track?

    http://silverstripe.org/data-model-questions/show/275508#post275508

    Posted by Jack, 15/12/2009 11:31pm (3 months ago)

  • You are awesome! Thanks!

    Posted by Jack, 15/12/2009 10:12am (3 months ago)

  • Hi Jack

    Have a look at the grouped dropdown field: http://doc.silverstripe.org/doku.php?id=groupeddropdownfield
    You can pass in an array of arrays so you will need to create a function to contruct the array and then pass it into the field.

    Hope that helps

    Aram

    Posted by Aram, 15/12/2009 9:44am (3 months ago)

  • Hey Aram, thanks for the tip!

    Do you have any idea how i might create a drop-down list of items separated into optgroups? I have an EventPage page which has several EventDate dataobject on it, and i'd like the drop down list to show EventDates under new optgroups for each EventPage (title). Thanks for your help!

    Posted by Jack, 15/12/2009 4:47am (3 months ago)

  • Hi Aram

    Yes, I was planning to do it in the CMS.

    I know it might be simple, but could you write some example code anyway? Cause it not sure yet how to...

    Thanks.

    Posted by Joel Grøndrup, 04/05/2009 6:15am (10 months ago)

  • Hi Joel. Do you mean a dropdown on the front end or in the CMS? Saving dropdown values from the CMS works like any other field, you just need a $db field whith the same name as the field like the Enum example above.

    Posted by aram, 03/05/2009 11:41am (10 months ago)

RSS feed for comments on this page RSS feed for all comments

Post your comment