Using .size() to check folder contents

nicban's Avatar

nicban

06 Apr, 2016 09:37 PM

I have a button on my folder that are a plus and minus. When you click the plus , the contents of the folder is shown; when you click the minus the contents of the folder gets hidden
I'm wanting to remove this button if there is only one item in the folder.
So I thought using the .size() method would work for this, but I'm not having any luck.
Basically if there is only one item in this folder don't show the button at all. Any help would be great. I have attached part of my code that I'm having problems with.
                               Thanks.

  1. 1 Posted by Ryan Griffith on 07 Apr, 2016 02:27 PM

    Ryan Griffith's Avatar

    Hi,

    I believe instead of checking $item.size() you would want to either check $rootItems.children.size() or use $foraech.count (number of items to iterate in the loop).

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by nicban on 07 Apr, 2016 03:49 PM

    nicban's Avatar

    Hi Ryan,
    I did try your solutions with no luck. I have include my whole code
    Here a little more background information

    So I created this navigation. I have a base folder with multiple site folders Inside each site folder are sub folders. These sub folder are my navigation.
     
    I have one folder with one page in it, its the index page.
    So since this has only one item in my folder, I don't want this plus button showing on the navigation indicating there is something more in the folder.
    When I try both solutions I either get all buttons showing or none.

  3. 3 Posted by Ryan Griffith on 07 Apr, 2016 05:49 PM

    Ryan Griffith's Avatar

    Thank you for providing your full code, definitely helpful.

    I believe what you will want to do is tweak the conditional to $item.children.size() because that is what you are looping over directly after.

    I also want to mention you can run into a case where children is greater than 0, but there are no items to display. This is because you are checking for the rblShowInNav Dynamic Metadata field within the loop, after the children size is checked. Your best bet to avoid this would be to loop over the children and save them to a temporary array if the metadata field's value is Yes. Then, you would check the size of your temporary array and loop over that instead. Consider the following snippet:

    <li class="$liClass">
      #if($item.metadata.getDynamicField("navigationFolder").value != "hide")      
        #set ($tempArray = [])
        #if ($item.children.size() > 0)
          #foreach($page in $item.children)  
            #if($page.metadata.getDynamicField("rblShowInNav").value == "Yes")
              #set ($_void = $tempArray.add($page))
            #end
          #end
        #end
    
        #if($tempArray.size() < 1)
          <a class="$!aclass" href="/$item.path/index" itemprop="url"><span itemprop="name">$fname</span></a>
        #else
          <a class="$!aclass" href="/$item.path/index" itemprop="url"><span itemprop="name">$fname</span></a> <button class="more" tabindex="1">more</button>
    
          ##CHILDREN
          <ul class="children" style="$ulStyle">
           #foreach($page in $tempArray)  
             #set($pname = $page.metadata.displayName.replaceAll('&amp;', '&').replaceAll('&', '&amp;'))  
             #set($title = $page.metadata.title.replaceAll('&amp;', '&').replaceAll('&', '&amp;'))
             #set($title = $page.metadata.getDynamicField("txtNavTitle").value.replaceAll('&amp;', '&').replaceAll('&', '&amp;'))
             #set($id = $title.replaceAll("[^A-Za-z0-9]", "").toLowerCase())
             
             <li>
                 #if($page.identifer.type == "page")
                   #if($page.name != "index" )
                         #if($page.path==$currentPage.path)
                             <a class="active current" href="$page.path">$title</a>
                         #else
                             <a href="$page.path">$title</a>
                         #end
                    #elseif($page.name == "index" )
                    #end
    
                 #elseif($page.identifer.type == "symlink")
                         #set($exLink = $page.metadata.title )                                        
                         #set($exLinkTarget = $page.metadata.getDynamicField("target").value)
                             <a href="$page.path" target="$!exLinkTarget">$exLink</a>                    
                 #elseif($page.identifer.type == "reference")                                        
                         #set( $referencedPage = $page.ReferencedAsset )                 
                         <a href="$referencedPage.path">$referencedPage.metadata.title</a>                                    
                 #end 
             </li> 
           #end
          </ul>
        #end
        #set ($tempArray = [])  
      #end
    </li>
    

    Please let me know if you have any questions.

    Thanks!

  4. Tim closed this discussion on 27 Apr, 2016 08:42 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM