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
giri rockzzzzgiri rockzzzz 

Dynamic row generation for dependent objects

I have created a dependent objects  drop down lists of type product and item in the visualforce page .
I need to create a command button in which the action is to generate 5 rows in the visualforce page , each row contains same  dependent object drop down list which i have created earlier .

I have generated rows using repeater controller and wrapper class but, the problem is that only last row drop down list works remaining rows remain inactive .
Can any one help me on this issue???

Please see the following code:

 

apex class:

 

public class dependentObjects{

String product;

String item;
String name;
String accname;
String divname;
String startperiod;


   public List<WrapperQueAns> lstQueAns = new List<WrapperQueAns>{};

 public List<WrapperQueAns> propLstQuesAns { get { return lstQueAns; } set { lstQueAns = value; } }


public String getProduct()
{
return this.product;
}
public void setProduct(String s)
{
this.product=s;
}
public String getAccname()
{
return this.accname;
}
public void setAccname(String s)
{
this.accname=s;
}
public String getItem()
{
return this.item;
}
public void setItem(String s)
{
this.item=s;
}
public String getDivname()
{
return this.divname;
}
public void setDivname(String s)
{
this.divname=s;
}

public String getName()
{
return this.name;
}
public void setName(String s)
{
this.name=product;
}

public String getStartperiod()
{
return this.startperiod;
}
public void setStartperiod(String s)
{
this.startperiod=s;
}


 

public List<SelectOption> getProducts(){

List<SelectOption> optionList=new List<SelectOption>();
optionList.add(new SelectOption('','-NONE-'));

for(product__c bp:[select name from product__c order by Name])
{
optionList.add(new SelectOption(bp.id,bp.name));
}
return optionList;
}
public List<SelectOption> getItems() {
      List<SelectOption> optionList = new List<SelectOption>();
     
       optionList.add(new SelectOption('', '- None -'));

   
      if(product!= NULL) {

      
        for (item__c bi : [select name from item__c bi where bi.product__c = :product]){
          optionList.add(new SelectOption(bi.id,bi.name));
        }
      }
      return optionList;
    }
   
    public List<SelectOption> getAccountname()
    {
     List<SelectOption> optionList = new List<SelectOption>();
     optionList.add(new SelectOption('', '- None -'));
     for(Account ac:[select name from Account where type='Client'])
{
optionList.add(new SelectOption(ac.id,ac.name));
}
return optionList;
   
    }
   
    public List<SelectOption> getDivnames(){

List<SelectOption> optionList=new List<SelectOption>();
optionList.add(new SelectOption('','-NONE-'));

for(Division__c di:[select name from Division__c ])
{
optionList.add(new SelectOption(di.id,di.name));
}
return optionList;
}

    public List<SelectOption> getStartp(){

List<SelectOption> optionList=new List<SelectOption>();
optionList.add(new SelectOption('','-NONE-'));

for(Billing_period__c sp:[select name from Billing_period__c ])
{
optionList.add(new SelectOption(sp.id,sp.name));
}
return optionList;
}

public void DynamicRow(){

for(Integer i=0;i<5;i++){

WrapperQueAns wqa = new WrapperQueAns();

wqa.propAns    = '';
 
lstQueAns.add(wqa);

 }

}
  public class WrapperQueAns{public String propAns   { get; set; } }//End Class WrapperQueAns
 
}

 

visualforce page:


<apex:page controller="dependentObjects" >
<apex:form >

<p>
<table>
<tr>
<td>

Account:
<apex:selectList value="{!accname}" size="1" id="accname">
            <apex:selectOptions value="{!Accountname}"/>  
          </apex:selectList>

</td>
<td>
Division:
<apex:selectList value="{!divname}" size="1" id="division">
            <apex:selectOptions value="{!divnames}"/>
          </apex:selectList>

</td>
<td>
Start Period:
<apex:selectList value="{!startperiod}" size="1" id="startp">
            <apex:selectOptions value="{!startp}"/>
            
          </apex:selectList>

</td>
</tr>
<tr>
<td>
product
<apex:selectList value="{!product}" size="1" id="category2">
            <apex:selectOptions value="{!products}"/>
            <apex:actionSupport event="onchange" rerender="features2,test,r"/>
          </apex:selectList>
          </td>
          <td>
Item     
     <apex:selectList value="{!item}" size="1" id="features2" disabled="{!ISNULL(product)}">
         <apex:selectOptions value="{!items}"/>
     </apex:selectList>
     <apex:inputText value="{!name}" id="test"/>
</td>

<td>
              Sep10  
                <input name="sum" value="{!name}" size="5" id="r" type="text"/>
               
</td>

<td>                
           Oct10     
                <input name="sum" value="2" size="5" type="text"/>
                </td>
              
                <td>
                  Nov10
                <input name="sum" value="1" size="5" type="text"/>
                </td>
              
                <td>
                  Dec10
                <input name="sum" value="0" size="5" type="text"/>
                </td>
                
                <td>
                Total:
                &nbsp;&nbsp;
                
                
                <input name="totalSum" id="totalSum" value="6" size="2" readonly="readonly" type="text"/>
               </td>
              </tr>
              </table>  
              </p>
                
                  
            
</apex:form>

<apex:outputPanel id="refreshdata">
<apex:repeat value="{!propLstQuesAns}" var="varLQA" id="textquesrp">
<apex:form >

 <p>

product
<apex:selectList value="{!product}" size="1" id="category2">
            <apex:selectOptions value="{!products}"/>
            <apex:actionSupport event="onchange" rerender="features2,test"/>
          </apex:selectList>
          
Item     
     <apex:selectList value="{!item}" size="1" id="features2" disabled="{!ISNULL(product)}">
         <apex:selectOptions value="{!items}"/>
     </apex:selectList>
     <apex:inputText value="{!name}" id="test"/>

 </p>

</apex:form>
</apex:repeat>
</apex:outputPanel>
<apex:form >
<apex:commandButton action="{!DynamicRow}" reRender="refreshdata" value="ADD ROW"/>
</apex:form>

</apex:page>

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi giri,

You will have to include the Select Lists that you have placed within your Repaet Element, in your Wrapper class

 

I have made few twicks in your classes and VF page, hope this helps

apex class:

 

public class dependentObjects{

 

String product;

String item;
String name;
String accname;
String divname;
String startperiod;


   public List<WrapperQueAns> lstQueAns = new List<WrapperQueAns>{};

 public List<WrapperQueAns> propLstQuesAns { get { return lstQueAns; } set { lstQueAns = value; } }


public String getProduct()
{
return this.product;
}
public void setProduct(String s)
{
this.product=s;
}
public String getAccname()
{
return this.accname;
}
public void setAccname(String s)
{
this.accname=s;
}
public String getItem()
{
return this.item;
}
public void setItem(String s)
{
this.item=s;
}
public String getDivname()
{
return this.divname;
}
public void setDivname(String s)
{
this.divname=s;
}

public String getName()
{
return this.name;
}
public void setName(String s)
{
this.name=product;
}

public String getStartperiod()
{
return this.startperiod;
}
public void setStartperiod(String s)
{
this.startperiod=s;
}

     public List<SelectOption> getAccountname()
    {
     List<SelectOption> optionList = new List<SelectOption>();
     optionList.add(new SelectOption('', '- None -'));
     for(Account ac:[select name from Account where type='Client'])
    {
    optionList.add(new SelectOption(ac.id,ac.name));
    }
    return optionList;
       
        }
       
        public List<SelectOption> getDivnames(){
    
    List<SelectOption> optionList=new List<SelectOption>();
    optionList.add(new SelectOption('','-NONE-'));
    
    for(Division__c di:[select name from Division__c ])
    {
    optionList.add(new SelectOption(di.id,di.name));
    }
    return optionList;
    }
    
        public List<SelectOption> getStartp(){
    
    List<SelectOption> optionList=new List<SelectOption>();
    optionList.add(new SelectOption('','-NONE-'));
    
    for(Billing_period__c sp:[select name from Billing_period__c ])
    {
    optionList.add(new SelectOption(sp.id,sp.name));
    }
    return optionList;
    }
    
    public void DynamicRow(){
    
    List<product__c> lstProduct = [select name from product__c order by Name];
    List<item__c> lstItem = new List<item__c>();
    if(product!= NULL)
    {
        lstItem = [select name from item__c bi where bi.product__c = :product];
    }
    for(Integer i=0;i<5;i++){
    
    WrapperQueAns wqa = new WrapperQueAns(lstProduct, lstItem);
    
    wqa.propAns    = '';
    
    lstQueAns.add(wqa);
    
     }
    
    }
  public class WrapperQueAns{
      public String propAns   { get; set; }
      public String strProduct   { get; set; }
      public String strItem   { get; set; }
      public List<product__c> lstProd = new List<product__c>();
    public List<item__c> lstItemValue = new List<item__c>();
      public WrapperQueAns(List<product__c> lstProducts, List<item__c> lstItems)
      {
          lstProd = lstProducts;
          lstItemValue = lstItems;
      }
      
      public List<SelectOption> getProducts()
      {
        List<SelectOption> optionList=new List<SelectOption>();
        optionList.add(new SelectOption('','-NONE-'));
        if(lstProd != null && !lstProd.isEmpty())
        {
            for(product__c bp:lstProd)
            {
                optionList.add(new SelectOption(bp.id,bp.name));
            }
        }
        return optionList;
    }
    public List<SelectOption> getItems()
    {
      List<SelectOption> optionList = new List<SelectOption>();
      optionList.add(new SelectOption('', '- None -'));
        if(lstItemValue!= NULL && !lstItemValue.isEmpty())
        {
            for (item__c bi : lstItemValue)
            {
                  optionList.add(new SelectOption(bi.id,bi.name));
            }
          }
      return optionList;
    }

      }//End Class WrapperQueAns

 

}

 

 

visualforce page:


<apex:page controller="dependentObjects" >
<apex:form >

<p>
<table>
<tr>
<td>

Account:
<apex:selectList value="{!accname}" size="1" id="accname">
            <apex:selectOptions value="{!Accountname}"/>  
          </apex:selectList>

</td>
<td>
Division:
<apex:selectList value="{!divname}" size="1" id="division">
            <apex:selectOptions value="{!divnames}"/>
          </apex:selectList>

</td>
<td>
Start Period:
<apex:selectList value="{!startperiod}" size="1" id="startp">
            <apex:selectOptions value="{!startp}"/>
            
          </apex:selectList>

</td>
</tr>
<tr>
<td>
product
<apex:selectList value="{!product}" size="1" id="category2">
            <apex:selectOptions value="{!products}"/>
            <apex:actionSupport event="onchange" rerender="features2,test,r"/>
          </apex:selectList>
          </td>
          <td>
Item     
     <apex:selectList value="{!item}" size="1" id="features2" disabled="{!ISNULL(product)}">
         <apex:selectOptions value="{!items}"/>
     </apex:selectList>
     <apex:inputText value="{!name}" id="test"/>
</td>

<td>
              Sep10  
                <input name="sum" value="{!name}" size="5" id="r" type="text"/>
               
</td>

<td>                
           Oct10     
                <input name="sum" value="2" size="5" type="text"/>
                </td>
              
                <td>
                  Nov10
                <input name="sum" value="1" size="5" type="text"/>
                </td>
              
                <td>
                  Dec10
                <input name="sum" value="0" size="5" type="text"/>
                </td>
                
                <td>
                Total:
                &nbsp;&nbsp;
                
                
                <input name="totalSum" id="totalSum" value="6" size="2" readonly="readonly" type="text"/>
               </td>
              </tr>
              </table>  
              </p>
                
                  
            
</apex:form>

<apex:outputPanel id="refreshdata">
<apex:repeat value="{!propLstQuesAns}" var="varLQA" id="textquesrp">
<apex:form >

 <p>

product
<apex:selectList value="{!varLQA.strProduct}" size="1" id="category2">
            <apex:selectOptions value="{!varLQA.Products}"/>

            <apex:actionSupport event="onchange" rerender="features2,test"/>
          </apex:selectList>
          
Item     
     <apex:selectList value="{!varLQA.strItem}" size="1" id="features2" disabled="{!ISNULL(product)}">
         <apex:selectOptions value="{!varLQA.Items}"/>

     </apex:selectList>
     <apex:inputText value="{!name}" id="test"/>

 </p>

</apex:form>
</apex:repeat>
</apex:outputPanel>
<apex:form >
<apex:commandButton action="{!DynamicRow}" reRender="refreshdata" value="ADD ROW"/>
</apex:form>

</apex:page>

 

 

giri rockzzzzgiri rockzzzz

Hello,

the problem which i am facing is that ...when we generate drop down list dynamically only the last row drop down list works...

for eaxmple:

If we generate 5 rows...only the 5th row drop downlist works...the first 4 rows does not work..i.e values are not getting populated.

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Giri,

 

I have tried the code snippet that i had posted earlier, in my development org, jst by replacing the Custom objects (used in your code) by Standard ones, and everything seems to work fine.