Search code examples
salesforceapex-codevisualforce

How can I simulate a button click from a page controller?


I'm creating a Google-like application in Visualforce where the user enters a search term, clicks Search, and the results are displayed underneath the search box. As part of this, when the user clicks Search, an actionStatus is set up to display "Loading..." while the search is in progress:

<apex:commandButton value="Search" action="{!runQuery}" status="loading"/>

Now I'm trying to set up the landing page, which is just a page with an input field and a Search button, for the initial search. In Google style, the first search will take the user to the page where results are displayed with the search box moved up to the top.

The problem is that I don't know how to invoke the actionStatus at page load. It's easy enough when the user clicks a button, but when searching from the landing page, a new page is loaded and then the search is carried out. At the moment I just have the controller's constructor checking for parameters and, if any are found, calling runQuery() manually, but this simply delays the page load time and doesn't invoke the actionStatus.

Any ideas?


Solution

  • Adding an onLoad event call didn't work, but putting this at the very end of the page, just before the closing apex:page tag, appears to. I'm not entirely sure why, but it's clean and gets the job done.

    <script>
        runQuery();
    </script>