Hello Everyone, In this tutorial, I am going to provide a working example that explains how to delete checked data from the list during pagination. In this example, only those checked records are deleted whose list is in front of you.
After completing this tutorial, you’ll able to:
- Use of custom Iterator in apex class.
- How to delete checked record in pagination.
So, let’s begin,
In this example, we are using three apex classes.
Step 1: Login to your Salesforce Org. and open developer console.
Step 2: Navigate to File | New | Apex Class and create an apex controller called AccountInner and replace the following code.Step 1: Login to your Salesforce Org. and open developer console.
AccountInner.apxc
global Class AccountInner{ public boolean isSelected {get;set;} public Account acc {get;set;} public AccountInner(boolean isSelected, Account acc){ this.isSelected = isSelected; this.acc = acc; } }Step 3: Again navigate to File | New | Apex Class and create a custom iterable controller (Apex class) called CustomIterable and replace the following code.
CustomIterable.apxc
global with sharing class CustomIterable implements Iterator<list<AccountInner>>{ List<Account> accs {get; set;} list<AccountInner> accInnerList {get; set;} list<AccountInner> accInnerListRequested {get; set;} AccountInner accInnerObj; Integer i {get; set;} public Integer setPageSize {get; set;} public CustomIterable(string sQuery){ accs = Database.Query(sQuery); accInnerList = new list<AccountInner>(); accInnerListRequested = new list<AccountInner>(); for(Account a : accs) { accInnerObj = new AccountInner(false, a); accInnerList.add(accInnerObj); } setPageSize = 10; i = 0; } global boolean hasNext(){ if(i >= accInnerList.size()) { return false; } else { return true; } } global boolean hasPrevious(){ if(i <= setPageSize) { return false; } else { return true; } } global list<AccountInner> next(){ accInnerListRequested = new list<AccountInner>(); integer startNumber; integer size = accInnerList.size(); if(hasNext()){ if(size <= (i + setPageSize)){ startNumber = i; i = size; } else{ i = (i + setPageSize);startNumber = (i - setPageSize); } for(integer start = startNumber; start < i; start++){ accInnerListRequested.add(accInnerList[start]); } } return accInnerListRequested; } global list<AccountInner> previous(){ accInnerListRequested = new list<AccountInner>(); integer size = accInnerList.size(); if(i == size){ if(math.mod(size, setPageSize) > 0){ i = size - math.mod(size, setPageSize); } else{ i = (size - setPageSize); } } else{ i = (i - setPageSize); } for(integer start = (i - setPageSize); start < i; ++start){ accInnerListRequested.add(accInnerList[start]); } return accInnerListRequested; } }
Step 4: Again navigate to File | New | Apex Class and create an Apex controller called DelcheckedItemInPagination and replace the following code.
DelcheckedItemInPagination.apxc
public class DelcheckedItemInPagination { CustomIterable obj; public list<AccountInner> accInnerObj {get;set;} public boolean ischeck{get;set;} //Constructor public cls_getcheckbox_valuein_pagination() { getDate(); } public void getDate(){ string sQuery = 'SELECT Id, Name FROM Account order by name'; obj = new CustomIterable(sQuery); obj.setPageSize = 5; next(); } public Boolean hasNext { get { return obj.hasNext(); } set; } public Boolean hasPrevious { get { return obj.hasPrevious(); } set; } public void next() { accInnerObj = obj.next(); ischeck=false; } public void previous() { accInnerObj = obj.previous(); } public void deleteRecord(){ for(AccountInner m:accInnerObj){ if(m.isSelected==true){ delete m.acc; } } getDate(); } }
Step 5: Navigate to File | New | Visualforce Page and create a new Visualforce Page called DelcheckedItemInPaginationVF and replace the following markup.
DelcheckedItemInPaginationVF.vfp
<apex:page controller="DelcheckedItemInPagination"> <script type="text/javascript"> function selectAllCheckboxes(obj,receivedInputID){ var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++){ if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){ inputCheckBox[i].checked = obj.checked; } } } function changeValue(input, textid) { document.getElementById(textid).value += input.value; } </script> <apex:actionstatus id="ajax"> <apex:facet name="start"> <div class="waitingSearchDiv" id="el_loading" style="background-color: black; height:100%;opacity:0.65;width:100%;"> <div class="waitingHolder" style="top: 100px; width: 91px;"> <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." /> <span style="color:white;font-weight:bold;">Loading...</span> </div> </div> </apex:facet> </apex:actionstatus> <apex:form > <apex:sectionHeader title="Pagination"/> <apex:pageBlock id="pb"> <apex:commandButton value="Delete" action="{!deleteRecord}" reRender="pbSec" status="ajax"/> <apex:pageBlockSection id="pbSec"> <apex:pageBlockTable value="{!accInnerObj}" var="inner"> <apex:column title="Select" headerValue="Select"> <apex:facet name="header"> <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')" value="{!ischeck}"/> </apex:facet> <apex:inputCheckbox value="{!inner.isSelected}" id="inputId"></apex:inputCheckbox> </apex:column> <apex:column headerValue="Account"> <apex:outputText value="{!inner.acc.Name}" /> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> <br/> <apex:commandButton value="<<Previous" action="{!previous}" rendered="{!hasPrevious}"/> <apex:commandButton value="Next >>" action="{!next}" rendered="{!hasNext}"/> </apex:form> </apex:page>
Output:
See also:
- Print number in text format using Visualforce and Apex
- Show account list in popup window on checkbox checked
- Account Search using JavaScript Keyup Event in Salesforce
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
0 Comments
Post a Comment