Deleting a particular record in the displayed table
The first thing that would flash in our mind when we are supposed to delete a particular record is the record Id itself. So, let's make sure we are some how able to pass the records id and store/receive it in the apex class in order to do the honours.
Next, we fetch that particular record from the required object and use a simple DML to delete.
Apex Class
public with sharing class DeleteTestingNew {
public String rId{get;set;}
DataLoadTest__c delDlt = new DataLoadTest__c();
public PageReference doDelete() {
delDlt=[select Id from DataLoadTest__c where id =:rId];
delete delDlt;
//re-directing to a new page, please create this page "newdelete"
pagereference ref=new pagereference('/apex/newdelete');
ref.setredirect(true);
return ref;
}
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
public List<DataLoadTest__c> getRecords() {
lstdlt =[select id,name,city__c,country__c,phone__c from DataloadTest__c];
return lstdlt;
}
}
Visualforce Page
<apex:page controller="DeleteTestingNew" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Action">
<apex:commandlink value="Delete" action="{!doDelete}">
<apex:param name="rId" value="{!r.Id}"
assignTo="{!rId}"/>
</apex:commandlink>
</apex:column>
<apex:column headerValue="Name">
{!r.name}
</apex:column>
<apex:column headerValue="city">
{!r.City__c}
</apex:column>
<apex:column headerValue="phone">
{!r.Phone__c}
</apex:column>
<apex:column headerValue="country">
{!r.Country__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
We need your support,Please share/Like us on Facebook ⛅
Deleting a particular record in the displayed table
Reviewed by dasfrogpractice
on
04:53
Rating:
No comments: