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 agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

Comments (10)

  • A young boy and his dad went out fishing one fine morning. After a few quiet hours out in the boat, the boy became curious about the world around him. He looked up at his dad and asked "How do fish breath under water?"His dad thought about it for a moment, then replied, "I really don't know, son."The boy sat quietly from another moment, then turned back to his dad and asked, "How does our boat float on the water?"Nnce again his dad replied, "Don’t know, son."Pondering his thoughts again, a short while later, the boy asks "Why is the sky blue?"Again, his dad replied. "Don’t know, son."The inquisitive boy, worried he was annoying his father, asks this time "Dad, do you mind that I'm asking you all of these questions?""Of course not son." replied his dad, "How else are you ever going to learn anything?"

    Posted by replica watches, 06/07/2010 4:40am (23 days ago)

  • Let's say the drop-down list will be poplulated from the product.txt
    file, and there will be a file for each corresponding item in the
    drop-down list. From the user point of view, if he wants to add a new
    product, he will just need to open the product.txt file, and add a new
    line with the product name, "Laptop", then add a new text file named
    "laptop" in the same folder which contains the HTML fragment to be included in the content area.
    What is the easiest way to do this?

    Posted by Mhunter, 14/05/2010 8:04am (3 months ago)

  • @aram: thanks

    Posted by thealien, 07/01/2010 1:14pm (7 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 (7 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 (7 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 (7 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 (8 months ago)

  • You are awesome! Thanks!

    Posted by Jack, 15/12/2009 10:12am (8 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 (8 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 (8 months ago)

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

Post your comment