+ Start a Discussion
doudou 

Get the id of a row table and delete the row

Hello,
I have to create a button which allow to delete a row in a visualforce table.

Here is the part of the visualforce code :
<apex:form>
 <apex:repeat value="{!listClient}" var="lc">
  <div class="tr">
   <span class="cell">
   <apex:outputLink value="{!supprClient(lc.Id)}" styleClass="delete">
   <apex:param name ="rowId" value="{!supprClient(lc.Id)}"/>
   </apex:outputLink>
   </span>
   <span class="cell">{!lc.LastName}</span>
   <span class="cell">{!lc.FirstName}</span>
  </div>
 </apex:repeat>
</apex:form>

And the part of my controller:
public List<Contact> listClient { get; set; }

public ExtranetV2_CampagnesController() {

		listClient = new List<Contact>();
}

public void supprClient(){
String myId = System.currentPageReference().getParameters().get('rowId');	

		for(Contact c : listClient){
			c = [SELECT Id FROM Contact WHERE Id = :myId];
			delete(c);
		}	
}

I have to retrieve the id of the row I have to delete (when the link is clicked) and apply the supprClient() method to that id ?
But I don't know how to do that, my code doesn't work, and I get an error when I want to save my visualforce page : supprClient unknown...

Thank you 
 
Best Answer chosen by dou
Alexander TsitsuraAlexander Tsitsura
Hi dou,

For apply method you need use apex:commandLink instead of apex:outputLink

Use 
<apex:commandLink action="{!supprClient}" styleClass="delete">
   <apex:param name ="rowId" value="{!lc.Id}"/>
</apex:commandLink >

instead of 
<apex:outputLink value="{!supprClient(lc.Id)}" styleClass="delete">
   <apex:param name ="rowId" value="{!supprClient(lc.Id)}"/>
   </apex:outputLink>

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi dou,

For apply method you need use apex:commandLink instead of apex:outputLink

Use 
<apex:commandLink action="{!supprClient}" styleClass="delete">
   <apex:param name ="rowId" value="{!lc.Id}"/>
</apex:commandLink >

instead of 
<apex:outputLink value="{!supprClient(lc.Id)}" styleClass="delete">
   <apex:param name ="rowId" value="{!supprClient(lc.Id)}"/>
   </apex:outputLink>

Thanks,
Alex
This was selected as the best answer
Rupal KumarRupal Kumar
Try Like This------------>
<apex:page controller="DataTableEditRemoveController">
<apex:form id="form" >
<apex:pageBlock title="Accounts">
  <apex:pageMessages ></apex:pageMessages>
  <apex:pageBlockTable value="{!accs}" var="row">
     <apex:column >
       <apex:outputLink title="" value="/{!row.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
       <a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!row.Id}');" style="font-weight:bold">Del</a>
     </apex:column>
     <apex:column value="{!row.Name}"/>
     <apex:column value="{!row.BillingStreet}"/>
     <apex:column value="{!row.BillingCity}"/>
     <apex:column value="{!row.BillingPostalCode}"/>
     <apex:column value="{!row.BillingCountry}"/>
  </apex:pageBlockTable>
</apex:pageBlock>

<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="form" >
   <apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:form>
</apex:page>
Controller--
public class DataTableEditRemoveController {

   public List<Account> accs { get; set; }
  
   //used to get a hold of the account record selected for deletion
   public string SelectedAccountId { get; set; }
  
   public DataTableEditRemoveController() {
       //load account data into our DataTable
       LoadData();
   }
  
   private void LoadData() {
       accs = [Select id, name, BillingStreet, BillingCity, BillingPostalCode, BillingCountry from Account limit 20];
   }
  
   public void DeleteAccount()
   {
      // if for any reason we are missing the reference 
      if (SelectedAccountId == null) {
      
         return;
      }
     
      // find the account record within the collection
      Account tobeDeleted = null;
      for(Account a : accs)
       if (a.Id == SelectedAccountId) {
          tobeDeleted = a;
          break;
       }
      
      //if account record found delete it
      if (tobeDeleted != null) {
       Delete tobeDeleted;
      }
     
      //refresh the data
      LoadData();
   }    
  
  
}
Thanks
Rupal
 
doudou
Thank you for the answers, 
@Rupal Kumar the apex:actionFunction won't work since I have to use an apex:repeat.... 
@Alexander Tsitsura your solutions seems to work, but it seems that my function for deleting the rows don't work like I want : it delete the record from my database (I think) but I just want it to delete it from my visualforce page, not completlt, just I want not be able to see the row, but I d'ont want to delete the record... Do you have any ide about that ?
Thank you again !
 
Alexander TsitsuraAlexander Tsitsura
for implemet you requirement, you need delete item from list

see code below
public void supprClient(){
        String myId = System.currentPageReference().getParameters().get('rowId');	

		for(Integer i=0; i<listClient.size(); i++) {
            Contact c = listClient[i];
            if (c.Id == myId) {
              listClient.remove(i); // remove item only from list for avoid dispaly this item on vf page
			  // delete(c); - delete record from database
              break;
            }
		}	
}

Thanks,
Alex
doudou
Thank you a lot @Alexander Tsitsura ! I didn't know the "remove" function ! Thank you again it works perfectly and I better understand my mistakes :)