function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sdetweilsdetweil 

nested datalist problems

I am building a tree view using datalist over a set of custom data.

 

the controller has two functions.. the constructor, which builds the outermost list, once

and a function that uses the data in the outer list, once per item to get the sub items.

 

the subfunction uses a static counter to index into the outer list, and then increments the coutner for the

next call .. whihc never comes.

 

the apex code is

 

<apex:datalist value={!outer} var="top">

    <apex:outputtext value="{!top.name}"/>

    <apex:datalist value="{!inner}" var="inner">

         <apex:outputtext value="{!inner.label}"/>

    </apex:datalist>

</apex:datalist>

 

the tree is built correctly as far as indentation is concerned, but the getInner() function is only called once,

according the debug log I built.

 

the getInner() is called with the static index set to 0, it correctly uses he first element of the outer list as the key on a new soql stmt, and build the inner list.. then increments the static counter.

 

but getInner() is never called again.

 

is there some way to specify that getInner() should be called everytime as the data is 'volitle'?

 

 

the apex code for the controller is

 

public class classname {

  private static integer index = 0;

  public List <class> familys {get; set;}

  public List <class2> getinner()
  {
      List<class2> varname = [select label from class2 where name = :familys.get(index).name];
      index = index +1;
      return varname;
  }

  public classname(){ 
     familys = [select name from class order by name asc];
  }
}

 

 

so the constructor builds the outer list one time;

and then the getInner() is called to get the dependant inner list

 

the generated output is

 

<ul>
 <li>outer 1</li>
  <ul>
     <li>inner 1 of outer 1</li>
     <li>inner 2 of outer 1</li>
     <li>inner 3 of outer 1</li>
  </ul>
 <li>outer 2</li>
  <ul>
     <li>inner 1 of outer 1</li>
     <li>inner 2 of outer 1</li>
     <li>inner 3 of outer 1</li>
  </ul>
 <li>outer 3</li>
  <ul>
     <li>inner 1 of outer 1</li>
     <li>inner 2 of outer 1</li>
     <li>inner 3 of outer 1</li>
  </ul>
</ul>

 

but the function to get the inner list is only called ONE time.

 

I can change the initial value of index from 0 to some other value, and the appropriate dependant list is built.

so I know the code in the getInner() function works properly if it would be called.

 

what did I misunderstand?

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You are not geeting inner list on the basis of outer in the page. For innerlist you should create a map in controller

Map<ID , List<class2>> mapID_InnerList =  new <Id , List<class2>>();

fill this map 

 

in vfp

 

 

<apex:datalist value={!outer} var="top">

    <apex:outputtext value="{!top.name}"/>

    <apex:datalist value="{!mapIndex_InnerList[top.id]" var="inner">

         <apex:outputtext value="{!inner.label}"/>

    </apex:datalist>

</apex:datalist>

 

 

 

for more you can see : http://forceschool.blogspot.com/2011/06/show-hierarchy-in-visualforce-page.html

 

All Answers

Shashikant SharmaShashikant Sharma

You are not geeting inner list on the basis of outer in the page. For innerlist you should create a map in controller

Map<ID , List<class2>> mapID_InnerList =  new <Id , List<class2>>();

fill this map 

 

in vfp

 

 

<apex:datalist value={!outer} var="top">

    <apex:outputtext value="{!top.name}"/>

    <apex:datalist value="{!mapIndex_InnerList[top.id]" var="inner">

         <apex:outputtext value="{!inner.label}"/>

    </apex:datalist>

</apex:datalist>

 

 

 

for more you can see : http://forceschool.blogspot.com/2011/06/show-hierarchy-in-visualforce-page.html

 

This was selected as the best answer
sdetweilsdetweil

fabulous!.. that was the key I was looking for.  I didn't get that idea from your blog post however..

 

 

thank you

Shashikant SharmaShashikant Sharma

Your welcome :)