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
jdeveloperjdeveloper 

How to add row in pageblocktable?

Hi guys i was wondering how to add rows in a pageblock table. for example you want to make a to-do list. each row is a task.  The columns of the table are task(DropDown list), Deadline(Date), Hours worked(Double). If you want to add a task you click on the Add Row button and adds a  row to the table.

surasura

Hi I think you can  do it easily be maintining a list of objects you need to display on pb table of  the controller  of vf page

 

On your button click just add the new object to the list you iterate in pb table and rerender the pageblock that page block contains 

 

hope this will help 

kmorf_entransformkmorf_entransform

thanks for the help but there is a little problem. When the pageblock rerenders all the values of the first column(SelectOptios) are reset to the default values. Is there a way i can overcome this?

surasura

From what I get you should display the field the value of select options list get assigned rather than the selectoption list ,if you can put the code of pb table I may be able to help more

kmorf_entransformkmorf_entransform

ok here is the code

<apex:form > 
    <apex:pageblock title="Sprint Backlog Entry">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            <apex:commandButton value="Cancel" action="{!cancel}" rerender="error"/>
        </apex:pageBlockButtons>
  
        <apex:pageblockTable value="{!sblogs}" var="sb" id="table">
          
            <apex:column headerValue="Task" style="width:110px;border:1px solid #f3f3eb;">
                <apex:selectList multiselect="false" size="1" value="{!sb.Work_Item__c}" style="width:400px;">  
                        <apex:selectOptions value="{!GetWorkItems}"/>
                </apex:selectList>
            </apex:column>  
                          
            <apex:column headerValue="Assigned To">
                <apex:inputField value="{!sb.Assigned_To__c}" required="true"/>
            </apex:column>
            <apex:column headerValue="Hours Worked">
                <apex:inputField value="{!sb.Hours_Worked__c}" style="width:3em;"/>
            </apex:column>
            <apex:column headerValue="Percent Complete">
                <apex:inputField value="{!sb.Percent_Complete__c}" style="width:3em;"/>
            </apex:column>
            <apex:column headerValue="Cost">
                <apex:inputField value="{!sb.Cost__c}"/>
            </apex:column>
        </apex:pageblockTable>
        

 

public with sharing class CreateSPrintBacklogContExt {

    public List<SprintBacklog__c> sblogs {get; set;}
    public final Sprint__c parSprint;
    
    public CreateSPrintBacklogContExt(ApexPages.StandardController myController) {
        //parSprint=(Sprint__c)myController.getrecord();
        
        
       try {
        parSprint=[Select ID,Name,Project__c,Release__c 
        from Sprint__c 
        WHERE ID = :((Sprint__c)myController.getrecord()).ID];
        
        
        
        sblogs = new List<SprintBacklog__c>();
        SprintBacklog__c SBacklog = new SprintBacklog__c();
        SBacklog.Sprint__c = parSprint.Id;
        sblogs.add(SBacklog);
       
        
       }
       catch (Exception e) {}
       
      
        
        }//end of constructor

    public void addrow() {
    	  	
        SprintBacklog__c SBacklog = new SprintBacklog__c();
        SBacklog.Sprint__c = parSprint.Id;
        sblogs.add(SBacklog);
        
        }
            
    public void removerow(){
    	
        Integer i = sblogs.size();
        String name = sblogs.get(i-1).Work_Item__c;
        sblogs.remove(i-1);}
            
    public PageReference save() {
    	try{
        	insert sblogs;
        	PageReference parrec = new PageReference ('/'+parSprint.id);
        	parrec.setRedirect(true);
       		return parrec; 
    	}
    	catch (Exception e){
    		ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING ,'You must enter all required fields');
            ApexPages.addMessage(msg);
            return null;
    	}
    	
    	
        }//end of save
        
    public PageReference cancel() {
        PageReference parrec = new PageReference ('/'+parSprint.id);
        parrec.setRedirect(true);
        return parrec; 
        }    
        
 /* Begin Select option */
 
 List<SelectOption>  SelectListOfWorkItems= new List<SelectOption>();
        public  List<SelectOption> GetWorkItems{
        	get{        	
        	       setWorkItems(parSprint);          
             	 return SelectListOfWorkItems;
        	}
        	set{
        		GetWorkItems = SelectListOfWorkItems;
        	}
                   
        }
     Private void setWorkItems(Sprint__c Sprint){   
            SelectListOfWorkItems.CLEAR();            
          
          
            for (Work_Item__c WorkItem:[SELECT ID,Name,Assigned_To__c 
                                        FROM Work_item__c 
                                        WHERE
                                        Work_Completion__c <> 100.0
                                        AND  Work_Item__c.Project__r.name=:parSprint.Project__c
                                        AND  Work_Item__c.Assigned_To__c=:NULL
                                        AND  Work_Item__c.Release__r.id=:parSprint.Release__c
                                         ]){
                                        	
                               
               			SelectListOfWorkItems.ADD(new SelectOption(WorkItem.id, WorkItem.Name));                                        
           } 
       
          
           
        }
 /* End Select Option */     

 

 

surasura

On your add row method make sure you assign the selected value to get getworkitems list

kmorf_entransformkmorf_entransform

Im not sure what you mean. can u give me an example

Mamadou Diallo 16Mamadou Diallo 16
Hi Sura can you give me an example?