2

I have a page on which I'm adding rows to pageblocktable but not saving them.

<apex:commandButton value="addrows" action="{!add}"/>

<apex:pageBlockTable var="a" value="{!statuslist}" >
<apex:column headerValue="Action">
               <apex:commandLink value="Delete" action="{!deleteAction}">
</apex:column>
  <apex:column headerValue="status" value="{!a.status_c}">

</apex:pageBlocktable>

Method to add rows

public void add()
{
   controls__c cnt = new controls__c();
   cnt.status = selectedvalue; (value selected by user)
   statuslist.add(cnt);
}

I want to be able to hide those particular rows on which user clicks delete. I don't have the id for the record as it is not saved. Any help is much appreciated.

1

1 Answer 1

4

Instead of using the ID of the object, you can use its index in the collection. Here that index is generated in the Visualforce (where it is called "num" but sent to the server using the name "delId"):

<apex:commandButton value="addrows" action="{!add}"/>
<apex:variable name="num" value="{!0}"/>
<apex:pageBlockTable var="a" value="{!statuslist}" >
<apex:column headerValue="Action">
<apex:commandLink value="Delete" action="{!deleteAction}">
     <apex:param name="delId" value="{!num}"/>
</apex:commandLink>
</apex:column>
  <apex:column headerValue="status" value="{!a.status_c}">
<apex:variable name="num" value="{!num+1}"/>
</apex:pageBlocktable>

with the value used in the Apex delete method:

public pagereference deleteAction(){
  String str = ApexPages.currentPage().getParameters().get('delId');
  if(str!=null){
    integer index = Integer.valueOf(str);
    statuslist.remove(index);
  }
  return null;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .