Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Use TreeMultiselectField in frontend form

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

Page: 1
Go to End
Author Topic: 1152 Views
  • m-phil
    Avatar
    Community Member
    37 Posts

    Use TreeMultiselectField in frontend form Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Use TreeMultiselectField in frontend form Link to this post

    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

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: Use TreeMultiselectField in frontend form Link to this post

    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...

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: Use TreeMultiselectField in frontend form Link to this post

    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.

    1152 Views
Page: 1
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.