Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » TreeDropdownField: unselect selected value

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 5400 Views
  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    TreeDropdownField: unselect selected value Link to this post

    I would like to use the TreeDropdownField as an optional field in a form, so I want to be able to 'unselect' a selected page. Is this possible? Is there some way to add a 'none' kind of option?

  • andy_steel
    Avatar
    Community Member
    31 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    I would also like to do this. I've had a look at the TreeDropdownField class and can't see any easy way of doing it.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    Edit: Use updated code instead.

    Came across the same issue. My solution was to extend the TreeDropdownField and adding the optional field there. You can use the OptionalTreeDropdownField where ever you would normally use the TreeDropdownField.

    Simply create a file OptionalTreeDropdownField.php in your \mysite\code\ folder with the following code:

    <?php
    /**
    * TreeDropdown-like field that gives you a tree of items including an empty field, using ajax.
    * Author: Marijn Kampf www.exadium.com
    * Date: 24 Nov 2009
    * Version: 1.0
    */
    class OptionalTreeDropdownField extends TreeDropdownField {
       function gettree() {
          echo '<ul class="tree"><li id="" class="l"><a>' . _t('OptionalTreeDropdownField.NONE', "(None)", PR_MEDIUM, 'Non selected value of a dropdown') . '</a>';
          parent::gettree();
          echo '</ul>';
       }
    }

    ?>

    Usage example:

          $fields->addFieldToTab("Root.Content.Sitemap", new OptionalTreeDropdownField('BasePage', "Base Page", 'SiteTree', 'URLSegment', 'MenuTitle'));

    I felt that this would be the best way to achieve this, but there may be a better solution possible.

  • andy_steel
    Avatar
    Community Member
    31 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    Good work Marijn.

  • frabraha
    Avatar
    Community Member
    49 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    I have some problems with the OptionalTreeDropdownField.
    It doesn't save the value when I try to publish it. It just says Choose when I refresh the page.

    With the normal TreeDropDownField you just write 'BasePageID', "Base Page", 'SiteTree' and it all works fine, but that doesn't work here.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    frabraha, if you use TreeDropDownField it works, but once you change your code to OptionalTreeDropDownField it doesn't?

    Can you post a sample of your code?

  • frabraha
    Avatar
    Community Member
    49 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    public static $has_one = array(   
    'InternLink' => 'SiteTree',);   

    $fields->addFieldToTab('Root.Content.Main', new OptionalTreeDropdownField("InternLinkID", "Velg hvor artikkelen skal linkes til", 'SiteTree', 'URLSegment', 'MenuTitle'));

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: TreeDropdownField: unselect selected value Link to this post

    Are you sure you're not getting an error when you change it to:

    $fields->addFieldToTab('Root.Content.Main', new TreeDropdownField("InternLinkID", "Velg hvor artikkelen skal linkes til", 'SiteTree', 'URLSegment', 'MenuTitle'));

    5400 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.