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.

Data Model Questions /

Sorting and TreeMultiselectField (SS2.4)


Reply


2 Posts   499 Views

Avatar
Nobrainer Web

Community Member, 137 Posts

6 July 2012 at 8:58am

Edited: 07/07/2012 8:46am

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)

Avatar
Nobrainer Web

Community Member, 137 Posts

7 July 2012 at 11:12am

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)