function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kon DeleKon Dele 

How to pass search values from component(InputText) to controller to VF page

I'm trying to add an article search feature to a custom portal page. I have a Search Component, a Controller, and a VF page for this functionality.

The issue is when I enter an InputText in the search component, the text is not passed on to the controller and VF page, and no results are returned.

If I enter text directly in the search box on the VF page, there's success. But the requirement is for the search to be on the component so that it can be iterated on various VF pages. I've posted the code below:

The Component:
<apex:component controller="vfKeywordSearchController">
  <apex:attribute name="theSearchResults" type="String" description="Id of the section rerender."     assignTo="{!searchstring}"/>
    <apex:form >
     <apex:inputText value="{!searchstring}" id="theSearchstring" maxlength="100" size="70"/> &nbsp;
    <apex:commandButton value="Go" id="submitButton" style="width:30" reRender="" action="{!onClick}"/>
   </apex:form>
    </apex:component>
The Controller:

public with sharing class vfKeywordSearchController {

    //Page Size
     private Static Final Integer PAGE_NUMBER = 10;

     //Search String used in ArticleList tag
       public String searchstring { get; set; }

          public PageReference onClick(){
           return new PageReference('/apex/Global_Search_Test?searchParameter='+searchstring);
      }

      public vfKeywordSearchController() {
   String qryString = 'SELECT Id, title, UrlName, LastPublishedDate,LastModifiedById FROM     KnowledgeArticleVersion WHERE (PublishStatus = \'online\' and Language = \'en_US\')';
       List<KnowledgeArticleVersion> articleList= Database.query(qryString);
     maxSize = articleList.size() ;
     }

  //Keeps track of current page & max size of article list
   Integer currentPage = 1;
    Integer maxSize = 1;

   // Returns whether we need to see previous button or not
      public boolean getPrevRequired() {
    return currentPage > 1;
      }

      // Returns whether we need to see next button or not
      public boolean getNextRequired() {
      return currentPage * PAGE_NUMBER < maxSize;
     }

     //Returns current page number 
    public Decimal getCurrentPageNumber() {
     return this.currentPage;
      }

     //action for next click
      public PageReference next() {
      if(maxSize > this.currentPage * PAGE_NUMBER) {
       this.currentPage = this.currentPage + 1;
        }
      return null;
     }    

      //action for previous click
      public PageReference previous() {        
       if(this.currentPage > 1)
        this.currentPage = this.currentPage - 1;
       return null;
       }
       }
The VF Page:

<apex:page id="mypage" controller="vfKeywordSearchController">
         <apex:composition template="LATemplate">
          <c:LASearchComponent theSearchResults="Global_Search_Test:theSearchResults"/>
              <apex:define name="body">
             <apex:form >
             <apex:inputText value="{!searchstring}" id="theSearchstring" maxlength="100" size="70"/> &nbsp;
                 <apex:commandButton value="Go" id="submitButton" style="width:30" reRender=""  action="{!onClick}"/>
                 <apex:panelGroup id="theSearchResults" >
               <apex:panelGrid width="100%">
                 <table width="99%">
           <tr>
             <th width="33%">Title</th>
            <th width="33%">Article Type</th>
            <th width="33%">Summary</th>
            </tr>
            </table>
         <knowledge:articleList articleVar="article" pageNumber="{!currentPageNumber}" Keyword=" {!searchstring}" hasMoreVar="false" pageSize="10">
             <table  width="99%">
             <tr>
             <td width="33%">
              <apex:outputLink target="_blank" value="{!URLFOR($Action.KnowledgeArticle.View, article.id,['popup' = 'true'])}">{!article.title}</apex:outputLink>
                 </td>
              <td width="33%"><apex:outputText >{!article.articleTypeLabel}</apex:outputText></td>
              <td width="33%"><apex:outputText >{!article.abstract}</apex:outputText></td>
                </tr>
           </table>
            </knowledge:articleList>
            </apex:panelGrid> 
          <apex:panelGrid columns="2">
          <apex:commandLink action="{!previous}" value="Previous" style="{!IF(prevRequired = true,'display:block','display:none')}" reRender="theSearchResults"/> 
             <apex:commandLink action="{!next}" value="Next"  style="{!IF(nextRequired = true,'display:block','display:none')}" reRender="theSearchResults"/>  
             </apex:panelGrid>
             </apex:panelGroup>
        </apex:form>
           </apex:define>
    </apex:composition>
        </apex:page>
I appreciate any pointers.


Sumitkumar_ShingaviSumitkumar_Shingavi
I think you are trying to use "" controller in VF component as well as VF page which is not good. Everytime you use an apex class with a VF page or component; it creates/maintains it's own state of variables:

So push all your article search code in VF component code and don't give anything as controller="vfKeywordSearchController" in your VF page.

PS: if this answers your question then hit Like and mark it as solution!