[SalesForce] Reset search results and clear the search text using a button click

I want to clear the inputtext used to search, and clear all search results using a commandbutton. I tried the following on the 'Reset' button I added but it only clears the inputtext and the search results are not clearing.

<apex:pageBlockSectionItem >
                    <apex:outputPanel layout="block" Rendered="true">
                    <apex:inputText value="{!ReportSearchString}" />
                    <apex:commandButton value="Search" action="{!searchReports}" reRender="ReportSearchResults, "/> &nbsp;&nbsp; 
                    <apex:commandButton value="Back" action="{!BackToCreateFeedPage}"/>&nbsp;
                    <apex:commandButton value="Reset" onclick="this.form.reset();return false;"/>
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>
                            </apex:pageBlockSection>
                        <apex:pageBlockSection id="ReportSearchResults">
                <apex:outputText value="{!warningMessage}" rendered="{!!showTable}" />
                <apex:pageBlockTable value="{!reportList}" var="item" rendered="{!showTable}">
                    <apex:column headerValue="Report Name" >
                        <apex:commandLink value="{!item.Name}" action="{!BackToCreateFeedPage}">
                            <apex:param value="{!item.Id}" name="SelectedReport" assignTo="{!selectedReportId}" />
                        </apex:commandLink>

This is my search function, it works fine. I need to clear the results of this search on clicking 'Reset' button.

 public void searchReports()
    {
        showTable = false;      
        reportList = new List<Report>(); 
        reportSearchResult = new Map<Id, Report>(); 
        selectedReport = new Report();
        if((ReportSearchString != null && ReportSearchString != '') && ReportSearchString.length() >= 3)
        {
            reportSearchResult = new Map<Id, Report>([Select Id, Name From Report Where Name Like :'%'+ReportSearchString+'%' limit 2000]);

            warningMessage = 'No Result Found';
        }
        else
        {
            warningMessage = 'Search text should have atleast 3 characters';
        }
    }

Best Answer

Page: have a id on first outputpanel and pageblocktable and rerender them after resetting the search string and search results.

<apex:commandButton value="Reset" action="{!reset}" rerender="OutPutPanelId PageBlockTableId"/>

Controller:

public void reset(){
   ReportSearchString = '';
   if(reportList != null){
       reportList.clear();
   }
}
Related Topic