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
anuj huriaanuj huria 

delete row from table

im unable to get the index of row...see my code 


vf page:-

<apex:page controller="t6">
  <apex:form >
      <apex:pageBlock id="yu">
          <apex:variable var="rowNumber" value="{!0}"/>
          <apex:pageBlockButtons >
              <apex:commandButton value="Add row" action="{!ar}" reRender="yu"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection >
              <apex:pageBlockTable value="{!qe}" var="y">
                  <apex:column headerValue="index">
                      <apex:outputText value="{0}">
                          <apex:param value="{!rowNumber+1}"/>
                      </apex:outputText>
                  </apex:column>
                  <apex:column >
                      <apex:commandButton value="remove" action="{!remove}" reRender="eee"/>
                          <apex:param name="index" value="{!rowNumber}" />
                  </apex:column>
                  <apex:column headerValue="name">
                      <apex:inputField value="{!y.name}"/>
                  </apex:column>
                  <apex:column headerValue="account" >
                      <apex:inputField value="{!y.Account__c}" >
                          <apex:actionSupport event="onchange" action="{!chng}" />
                      </apex:inputField>
                  </apex:column>
                   <apex:column headerValue="contacts">
                      <apex:selectList size="1" multiselect="false" >
                          <apex:selectOptions value="{!connames}">
                          </apex:selectOptions>
                      </apex:selectList>
                  </apex:column> 
                  <apex:column headerValue="amount">
                      <apex:inputField value="{!y.currency__c}"/>
                      <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>


controller:-


public class t6 
{
    public List<sales__c> qe {get;set;}
    public Integer rowNumber{get;set;}
    
    
    //Integer inx;
    sales__c s;
    String t; 
    public t6()
    {
      s=new sales__c();
      qe=new List<sales__c>();
      qe.add(s);  
    }
    
    public void ar()
    {
        sales__c s1=new sales__c();
        qe.add(s1);
    }
    public void remove()
    {
        
        //qe.remove(1);
        //rowNumber = Integer.valueOf(ApexPages.currentPage().getParameters().get('index'));
        System.debug(rowNumber);
       
        
    }
    public void chng()
    {
        sales__c f=new sales__c();                               
        f=qe[qe.size()-1];
        t=f.account__c;
        
    }
    
    public List<SelectOption> getConnames()
    {
        List<SelectOption> allc=new List<SelectOption>();
        
        for(Contact c:[SELECT id,name,email from Contact where Account.Id=:t])
        {
            allc.add(new SelectOption(c.id,c.name));
        }
        
       return allc; 
       
    }
}
 
Best Answer chosen by anuj huria
anuj huriaanuj huria
hi shashikant,
i found my mistake, i was clossing command button tag before param tag
thnks for your concern

All Answers

Shashikant SharmaShashikant Sharma
Hi Anuj,

Change this 
 
<apex:commandButton value="remove" action="{!remove}" reRender="eee"/>
                          <apex:param name="index" value="{!rowNumber}" assignTo="{!rowNumber}"/>


to 
<apex:commandLink value="Remove" style="color:red" action="{!remove}" rerender="eee" immediate="true" >
             <apex:param value="{!rowNumber}" name="rowToRemove" assignTo="{!rowToRemove}"/>
         </apex:commandLink>



You could see exact same example here : http://sfdcsrini.blogspot.com/2014/12/adding-and-deleting-rows-dynamically-in.html

And a same issue being resolved here: https://developer.salesforce.com/forums/?id=906F000000091JfIAI

Let me knwo if you still face issues.

Thanks

Shashikant

anuj huriaanuj huria
hi Shashikant Sharma,

Still getting the Exception

System.NullPointerException: Argument cannot be null.
Error is in expression '{!remove}' in page task6: Class.t6.remove: line 26, column 1
Class.t6.remove: line 26, column 1

 
anuj huriaanuj huria
and the blogspot link u have given uses wrapper class and helpeer class..i dnt want to use these...

Thanks
Shashikant SharmaShashikant Sharma
What is the statement which is returning System.NullPointerException: Argument cannot at line 26 ?

The blog that i shared is for reference, you could define your structure as your needs.

Thanks
Shashikant
anuj huriaanuj huria
hi shashikant,
i found my mistake, i was clossing command button tag before param tag
thnks for your concern
This was selected as the best answer
Shashikant SharmaShashikant Sharma
Great you could resolve it.

Please mark this post as solved so other coudl be benifitted from it.

Thanks
Shashikant