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
Indranil Deria 7Indranil Deria 7 

How can I add a new row in existing table by clicking on button ?

Hi,
I want to add a new row in a table which is generated dynamically and newly created row automatically mapped with the table.  

 For Example: 
I have one custom object "Sample" and table look like: -
User-added image

For adding row in to the table I have written custom controller but that doesn’t work for, the issue is the table is generated dynamically, I can't manage the visualforce page and controller's tables values. 
ex:
<apex:pageBlockTable value="{!selected}" var="a" id="table">
</apex:pageBlockTable>

How can I pass the Visualforce table value into apex controller?
jessie gracejessie grace
hi...
refer this...
https://developer.salesforce.com/forums/?id=906F0000000963NIAQ
let me know if it helps..
Thanks,
Jessie.
Indranil Deria 7Indranil Deria 7
Hi Jessie,
 I have already gone through that link.
Let me explain the scenario first:  
Suppose You have 6 records in Sample object, for some reason go to the mass edit of the page and select 4 records out of 6 and check the fields values of those records. Then you find some issue of one of them and decide to narrow down the record in another new record for that creating one row into existing 4 records table, new row means new record and that is dynamically mapped to the sample object.
Please let me know your thoughts on this.
 
Thanks
Indranil
jessie gracejessie grace
oh ok..do u wanna add records to ur object dynamically by adding rows?is this ur scenario if im not wrong...
Indranil Deria 7Indranil Deria 7
Yep,
The actual requirement is creating new record while the user is in mass edit page(editing mode), Please check my previously uploaded screenshot you will easily understand the requirement.
jessie gracejessie grace
thats what...refer the link i sent..edit the field names u require..that works perfect for accounts object..or post ur code...so that i can get where u r really missing
Indranil Deria 7Indranil Deria 7
Hi,
Bellow mention is the code which I have Implemented earlier.

Page:
<apex:pageBlock>
   <apex:pageBlock >
                Note: All modifications made on the page will be lost if Return button is clicked without clicking the Save button first. 
   </apex:pageBlock>
   <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Return" action="{!cancel}"/>
                <apex:commandButton id="cmdAdd" value="+" action="{!addRows}" reRender="out1"/>
        <apex:param name="rowIndex" value="{!rowNum}" />
        <apex:variable var="rowNum" value="{!rowNum+1}"/>
        </apex:commandButton>
   </apex:pageBlockButtons>
   <apex:pageBlockTable value="{!Samp}" var="a" rows="{!rows}" >
        <apex:column headerValue="Name">
                    <apex:inputField value="{!a.name}"/>
                </apex:column>
                
        <apex:column headerValue="Test Application">
                    <apex:inputField value="{!a.Test_Application__c}"/>
                </apex:column>
                
        <apex:column headerValue="Test Attribute A">
                    <apex:inputField value="{!a.Test_Attribute_A__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute B">
                    <apex:inputField value="{!a.Test_Attribute_B__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute C">
                    <apex:inputField value="{!a.Test_Attribute_C__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute D">
                    <apex:inputField value="{!a.Test_Attribute_D__c}"/>
                </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock> 



Controller:

public with sharing class RowsController{
    public integer rows{ get; set; }
    public List<Sample__c> Samp{ get; set; }
   
    public Integer rowNum{ get; set; }
      
    string Name = '';
    string Test_Application__c = '';
    string Test_Attribute_A__c = '';
    string Test_Attribute_B__c = '';
    string Test_Attribute_C__c = '';    
    string Test_Attribute_D__c = '';

    public RowsController() {
           
        CGSS = new List<CG_Sample_Selection__c>();
        rows = 3;        
        getName();
        getTest_Application__c();
        getTest_Attribute_A__c();
    getTest_Attribute_B__c();
    getTest_Attribute_C__c();
    getTest_Attribute_D__c();
        filterRows();
    }
    
    public void filterRows(){
        for(integer i=1;i<4;i++){ 
            Name = 'null';Test_Application__c = 'null'; Test_Attribute_A__c = '';Test_Attribute_B__c = '';Test_Attribute_C__c = '';Test_Attribute_D__c = '';
            Sample__c ss = new Sample__c(rows,Name,Test_Application__c,Test_Attribute_A__c,Test_Attribute_B__c,Test_Attribute_C__c,Test_Attribute_D__c);
            Samp.add(ss);
        }
    }
    
    public void addRows(){
        rows = rows + 1;
        Sample__c ss = new Sample__c(rows,Name,Test_Application__c,Test_Attribute_A__c,Test_Attribute_B__c,Test_Attribute_C__c,Test_Attribute_D__c);
        Samp.add(ss);
    }

    public class Sample__c{
        public integer rowCount{ get; set; }
        public string Name{ get; set; }
        public string Test_Application__c{ get; set; }
        public string Test_Attribute_A__c{ get; set; }
    public string Test_Attribute_B__c{ get; set; }
    public string Test_Attribute_C__c{ get; set; }
    public string Test_Attribute_D__c{ get; set; }


        public Sample__c(integer i,string str1,string str2,string str3,string str4,string str5,string str6){
             this.rowCount = i;
             this.Name = str1;
             this.Test_Application__c = str2;
             this.Test_Attribute_A__c = str3;
         this.Test_Attribute_B__c = str4;
         this.Test_Attribute_C__c = str5;
         this.Test_Attribute_D__c = str6;    
        }
    }
}

 
jessie gracejessie grace
  <apex:commandButton id="cmdAdd" value="+" action="{!addRows}" reRender="out1"/>
in this line u used "out1" id which u dint refer anywhere in ur code...
to my guess,include id in this 
"<apex:pageBlockTable value="{!Samp}" var="a" rows="{!rows}" >"..
let me know if it works!
 
Indranil Deria 7Indranil Deria 7
Ok Jessie, I will try let you know.
jessie gracejessie grace
yes ok..if u use rerender,add the id of the page block.
(for.ex:<apex:pageblock id="out1").it means u are asking the pageblock to refresh again.
to ur scenario,try like this...


<apex:pageBlock>
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Return" action="{!cancel}"/>
                <apex:commandButton id="cmdAdd" value="+" action="{!addRows}" reRender="out1"/>
        <apex:param name="rowIndex" value="{!rowNum}" />
        <apex:variable var="rowNum" value="{!rowNum+1}"/>
<apex:pageBlock id="out1">
   <apex:pageBlockTable value="{!Samp}" var="a" rows="{!rows}" >
        <apex:column headerValue="Name">
                    <apex:inputField value="{!a.name}"/>
                </apex:column>
                
        <apex:column headerValue="Test Application">
                    <apex:inputField value="{!a.Test_Application__c}"/>
                </apex:column>
                
        <apex:column headerValue="Test Attribute A">
                    <apex:inputField value="{!a.Test_Attribute_A__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute B">
                    <apex:inputField value="{!a.Test_Attribute_B__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute C">
                    <apex:inputField value="{!a.Test_Attribute_C__c}"/>
                </apex:column>
    <apex:column headerValue="Test Attribute D">
                    <apex:inputField value="{!a.Test_Attribute_D__c}"/>
                </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock> 
</apex:pageBlock>