+ Start a Discussion

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.


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 


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?


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


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: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:column headerValue="Assigned To">
                <apex:inputField value="{!sb.Assigned_To__c}" required="true"/>
            <apex:column headerValue="Hours Worked">
                <apex:inputField value="{!sb.Hours_Worked__c}" style="width:3em;"/>
            <apex:column headerValue="Percent Complete">
                <apex:inputField value="{!sb.Percent_Complete__c}" style="width:3em;"/>
            <apex:column headerValue="Cost">
                <apex:inputField value="{!sb.Cost__c}"/>


public with sharing class CreateSPrintBacklogContExt {

    public List<SprintBacklog__c> sblogs {get; set;}
    public final Sprint__c parSprint;
    public CreateSPrintBacklogContExt(ApexPages.StandardController myController) {
       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;
       catch (Exception e) {}
        }//end of constructor

    public void addrow() {
        SprintBacklog__c SBacklog = new SprintBacklog__c();
        SBacklog.Sprint__c = parSprint.Id;
    public void removerow(){
        Integer i = sblogs.size();
        String name = sblogs.get(i-1).Work_Item__c;
    public PageReference save() {
        	insert sblogs;
        	PageReference parrec = new PageReference ('/'+parSprint.id);
       		return parrec; 
    	catch (Exception e){
    		ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING ,'You must enter all required fields');
            return null;
        }//end of save
    public PageReference cancel() {
        PageReference parrec = new PageReference ('/'+parSprint.id);
        return parrec; 
 /* Begin Select option */
 List<SelectOption>  SelectListOfWorkItems= new List<SelectOption>();
        public  List<SelectOption> GetWorkItems{
             	 return SelectListOfWorkItems;
        		GetWorkItems = SelectListOfWorkItems;
     Private void setWorkItems(Sprint__c Sprint){   
            for (Work_Item__c WorkItem:[SELECT ID,Name,Assigned_To__c 
                                        FROM Work_item__c 
                                        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 */     




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


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?