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

Use TreeMultiselectField in frontend form


Reply

4 Posts   1179 Views

Avatar
m-phil

15 July 2010 at 9:21am Community Member, 37 Posts

Hey, I want to use a TreeMultiselectField like this

in the frontend.
But I want to set the selectable objects with a query, like $objects = DataObject::get('Group', ...);
There is no relation between the objects in the dropdown and the page.
How can I implement it?
Perhaps something like this?

$dropdown = new TreeDropdownField(...);
$dropdown->setObjects($objects);

Avatar
Willr

15 July 2010 at 11:23am Forum Moderator, 5511 Posts

You can only use TreeMultiSelectField with dataobjects that implement Hierarchy (eg SiteTree and Groups). You can't set object sets AFAIK but you can set a class name and the root node ID (level as to start at). Also some filter function which may provide something but I haven't used it myself.

http://api.silverstripe.org/2.4/forms/fields-relational/TreeDropdownField.html

Avatar
m-phil

15 July 2010 at 5:49pm (Last edited: 16 July 2010 6:13pm), Community Member, 37 Posts

thx for your reply!
ok in this case I want to display some group DataObjects (which -> depends on the current user), but does the page must have a relation like has_many?
The problem is, that another class have this relation, but the form should be displayed somewhere else.
I think it is easier to create a new "FlexTreeMultiselectField" by my own to handle that...

Avatar
m-phil

16 July 2010 at 6:14pm Community Member, 37 Posts

OK here is a solution by using the TreeMultiselectField:
The easiest way is to check the ajax request, which gets a 404 error
I used this to to add a function to handle the request, its name is always the Form ID like "ContactForm" on the current page.
It needs a valid HTML with a <ul>, see below:

function ContactForm() {
   $objects = ... // Perhaps a db query
   $HTMLStr = '<ul class="tree">';
   foreach ($objects as $object) {
      $HTMLStr .= '<li id="selector-ViewerGroups-'.$object->ID.'" class="Group closed"><a>'.$object->Title.'</a></li>';
   }
   $HTMLStr .= '</ul>';
   echo $HTMLStr;
   exit;
}


By default its styled by cms.