Dashboard > Community Wiki > ... > Template Examples > Content of children pages sorted in the parent page
Content of children pages sorted in the parent page Log In View a printable version of the current page.

Added by GrĂ©gory Joseph , last edited by GrĂ©gory Joseph on May 20, 2008  (view change)
Labels: 

This template shows a solution how get some specified nodeDatas out of the first paragraph of each subpage on the top page. And the hole stuff sorted by a specified nodeData. Clear?

I had the following problem:

  • parentpage
    • childpage1
    • childpage2
    • childpage3
    • childpage4
    • ...

Every childpage has only one paragraph with some nodeDatas. Some of these nodeDatas should be shown on the fatherpage, sorted by one of these nodeDatas.

Here's the solution:

<%!
  //Comparator for sorting
  public class CaseInsensitiveComparator
  implements java.util.Comparator {
    public int compare(Object o1, Object o2) {
      Content c1=(Content)o1;
      Content c2=(Content)o2;
      String s1 = c1.getNodeData("yourNodeDataForSorting").getString().trim();
      String s2 = c2.getNodeData("yourNodeDataForSorting").getString().trim();
      return s1.compareTo(s2);
    }
  }
	
  //
  //Method for collecting the paragraphs in the childPages -> only one paragraph is existing
  private void collectParagraphs(Collection myChildren, ArrayList childParagraphs) {
  for (Iterator myChildrenIterator = myChildren.iterator(); myChildrenIterator.hasNext();) {
    		
    //go throug the paragraph
    Content para = (Content)myChildrenIterator.next();
    //
    //Get the nodeData
    Iterator paraNodes = para.getChildren().iterator();
    Content nodes = (Content)paraNodes.next();

    //Fill the ArrayList
    childParagraphs.add(nodes);
			
  }
}
%>

Now use it...

<% 
  //Get actPage
  Content actPage = Resource.getActivePage(request);
			
  //get children of actPage
  Collection children = actPage.getChildren();
  Iterator childrenIterator = children.iterator();
	
  //
  //ArrayList for the paragraphs in the childPage
  ArrayList childParagraphs = new ArrayList();	
	
  //Iterate trough children
  while (childrenIterator.hasNext()) {
				
    Content child = (Content)childrenIterator.next();
				
    //Get the paragraphs and iterate through
    Collection myChildren = child.getChildren(ItemType.NT_CONTENTNODE,"nameOfTheParagraph");
    //Get the paragraphs
    collectParagraphs(myChildren,childParagraphs);	
  }

  //Sort the paragraphs
  Collections.sort(childParagraphs, new CaseInsensitiveComparator());
	
  //Iterate through the sorted paragraphs
  Iterator childParagraphsIterator = childParagraphs.iterator();
  while (childParagraphsIterator.hasNext()) {

    //Get the paragraph
    Content paragraph = (Content)childParagraphsIterator.next();
    	
    //Now get your Strings
    String someString = paragraph.getNodeData("nodeDataofYourChoice").getString();
  }

%>

In its previous incarnation on JspWiki, this page was last edited on Feb 9, 2007 10:11:22 AM by Cbu

Powered by a free Atlassian Confluence Open Source Project License granted to Magnolia International. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators