Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

TreeDropdownField: unselect selected value


Go to End
Reply


21 Posts   5752 Views

Avatar
martimiz

Forum Moderator, 1132 Posts

8 March 2009 at 4:40am

Edited: 08/03/2009 4:41am

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?

Avatar
andy_steel

Community Member, 31 Posts

12 March 2009 at 8:33am

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.

Avatar
MarijnKampf

Community Member, 164 Posts

25 November 2009 at 4:09am

Edited: 18/06/2010 3:24am

Edit: [url=http://www.silverstripe.org/form-questions/show/255686?start=8#post286686]Use updated code instead[/url].

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.

Avatar
andy_steel

Community Member, 31 Posts

25 November 2009 at 5:09am

Good work Marijn. :-)

Avatar
frabraha

Community Member, 49 Posts

10 March 2010 at 3:43am

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.

Avatar
MarijnKampf

Community Member, 164 Posts

10 March 2010 at 3:50am

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?

Avatar
frabraha

Community Member, 49 Posts

10 March 2010 at 3:56am

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

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

Avatar
MarijnKampf

Community Member, 164 Posts

10 March 2010 at 4:12am

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'));

Go to Top