[SalesForce] Rerendering on Delete button is not working

I have delete button on page which deletes selected record from page.
The moment I click on button, Record gets deleted but page is not getting refreshed automatically.
To see the changes I need to refresh the page mauanlly.

Below is my Visualforce Page,

<apex:page Controller="testPageExt" >
    <apex:stylesheet value="/sCSS/25.0/sprites/1342034628000/Theme3/default/gc/versioning.css" />
    <apex:stylesheet value="/sCSS/25.0/sprites/1342034628000/Theme3/default/gc/extended.css" />
        <script>
            window.onload = function(){
            document.getElementsByClassName('headerSearchContainer')[0].style.display = 'none';
            };
        </script>
        <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;
                        }
                    }
                }
        </script>
    <apex:form >
        <apex:pageBlock title="Supplier List" helpTitle="supplier" helpUrl="/apex/testPage" >
        <apex:pageBlockButtons >
            <apex:commandButton action="{!deleteSelectedSupplier}" value="Delete Supplier" reRender="supplier"/>
        </apex:pageBlockButtons>
            <apex:pageBlockSection showHeader="true" title="List of All Supplier" columns="2" id="PBSection">
                <apex:pageBlockTable value="{!supWrapList}" var="s" id="supplier">
                    <apex:column >
                        <apex:facet name="header"><apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputID')"/></apex:facet>
                        <apex:inputCheckbox value="{!s.isSelected}" id="inputID"/>         
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Supplier Name</apex:facet>
                        <apex:commandLink action="{!quoteaction}" value="{!s.supObj.name}" reRender="quote">
                            <apex:param name="sParam" value="{!s.supObj.id}" assignTo="{!supplierValue}"/>
                        </apex:commandLink>
                    </apex:column>     
                </apex:pageBlockTable>
                <apex:pageBlockTable var="r" value="{!quoteList}" id="quote">
                    <apex:column headerValue="Quote Name" value="{!r.name}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

*******Controller***********

public with sharing class testPageExt {

public List<Quote__c> quoteList {get;set;}
public List<Supplier__c> supllierList {get;set;}
public String supplierValue {get;set;}
public List<wrapSupplier> supWrapList {get;set;}
public List<Supplier__c> selectedSupplier {get;set;}

public testPageExt() {
    supWrapList = new List<wrapSupplier>();
    supllierList =[Select id, Name from supplier__c];
    system.debug('*********'+supllierList );
    quoteList = null;
    for(supplier__c sObj:supllierList){
        supWrapList.add(new wrapSupplier(sObj));
    }
}

    public pagereference deleteSelectedSupplier(){
        selectedSupplier = new List<Supplier__c>();
        for(wrapSupplier wObj: supWrapList){
            if(wObj.isSelected == True){
                selectedSupplier.add(wObj.supObj);
            }
        }
        Delete selectedSupplier;
        //getSupllieraction();
        return new PageReference ('/apex/testPage');
    }

    public List<Supplier__c> getSupllieraction() {
        supllierList =[Select id, Name from supplier__c];
        return supllierList ;
    }

    public void quoteaction() {
        quoteList = [Select id, name ,RFQ_Supplier__c from Quote__c where RFQ_Supplier__c =:supplierValue ];
    }

    public class wrapSupplier {

        public Supplier__c supObj {get;set;}
        public Boolean isSelected {get;set;}

        public wrapSupplier(supplier__c sObj){

            this.supObj = sObj;
            this.isSelected = False;
        }
    } 
}

Best Answer

The reason why you are not getting the updated list is because you haven't queried the list after handling the delete. So in your server, the list is not refreshed.

Try updating your code into something like this:

public testPageExt() {
    initializeData();
}

public void initializeData()
{
    supWrapList = new List<wrapSupplier>();
    supllierList =[Select id, Name from supplier__c];
    system.debug('*********'+supllierList );
    quoteList = null;
    for(supplier__c sObj:supllierList){
        supWrapList.add(new wrapSupplier(sObj));
    }
}

    public void deleteSelectedSupplier(){
        selectedSupplier = new List<Supplier__c>();
        for(wrapSupplier wObj: supWrapList){
            if(wObj.isSelected == True){
                selectedSupplier.add(wObj.supObj);
            }
        }
        Delete selectedSupplier;
        initializeData();
    }
Related Topic