Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sorting and TreeMultiselectField (SS2.4)

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

Page: 1
Go to End
Author Topic: 443 Views
  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Sorting and TreeMultiselectField (SS2.4) Link to this post

    Hello,

    I'm using TreeMultiselectField to allow for selection of pages that goes into a menu.
    I would like to get the pages back in another order than the default.
    Beeing able to output the selected pages from the TreeMultiselectField, in the order they were checked is what i'm looking for.

    Can anyone help me with this? Is it possible and where do I/we start?

    Edit: What alternative ways can you guys recommend?
    I can think of; adding a checkbox for each possible menu to all pages, and then have a function that gets all the pages checked for a certain menu, sorting by a field on each page.

    Hope for some help :o)

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Sorting and TreeMultiselectField (SS2.4) Link to this post

    Ok I have found a way to sort the result output in the template.
    As it turns out it's actually easy.

    I have added a function to my custom SiteConfig:
       public function footerCol3data(){
          return $this->owner->FooterCol3Pages(null, "`SiteConfig_FooterCol3Pages`.`ID`");
       }

    concerning the following relation
       function extraStatics() {
          return array(
             'many_many' => array(
                "FooterCol3Pages" => "SiteTree"
             ),
          );
       }

    and added to the CMS by:
          $fields->addFieldToTab("Root.Menus", new TreeMultiselectField($name = "FooterCol3Pages", $title = "Sider i footer kolonne 3", "SiteTree"));

    In the template i can now loop the data like this:
          <% control SiteConfig.footerCol3data %>
             <li class="{$ClassName}"><a href="$Link"><span>$MenuTitle</span> id: $ID</a></li>
          <% end_control %>

    When TreeMultiselectField is used in the backend, it posts a list of selected page id's, that is then added to the relation table.
    Each selected pages id is written in the order it was clicked in the CMS. That allows us to order the pages by the relation id = giving us the pages in the order they were added.

    NOTE: If editing the selected pages in the TreeMultiselectField, notice that the sort order might not work as expected, this is because the relation table is only updated if the ID of the selected page is not allready in the relation table. To set the sort order, deselect all pages in the TreeMultiselectField, press save. Reselect the pages in the desired order, save and it should be ok.

    Took some time to figure this out, hope it helps someone :o)

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