5

I need to refresh viewstate on a page to show the latest query results based on user entry, but it's not working.

I have an inputText on the page for the user to enter a filter value. When that happens, I re-query records and I rerender the pageBlockTable that displays the records.

I'm using onChange on the inputText and an actionFunction with Javascript.

When I enter a value in the input field, the method is called and the page is rerendered. However, the viewstate is not changing so the pageBlockTable still shows the original set of records.

Here are my code snippets:

<apex:page>
    <script>
        CCx.thispage = {};
        CCx.thispage.filterByName = function(evt) {
            var name = evt.value;

            // log a google analytics event

            // submit via actionfunction
            doFilterByName(name);
        };
    </script>

    <apex:form id="form">
        <div class="ccx-list-header">
            <div class="ccx-advocate-buttons" id="bulkListActions">
                <apex:inputText value="{!searchName}" onChange="CCx.thispage.filterByName(this); return false;" 
                        html-placeholder="Search for advocate" />
            </div>
        </div>
        <apex:actionFunction name="doFilterByName" action="{!filterByName}" 
                rerender="bulkListActions, advocateListTable" />


            <apex:pageBlock id="pageblock">
                <apex:pageBlockSection columns="1" collapsible="false" id="section">
                    <apex:pageBlockTable id="advocateListTable" value="{!advocates}" var="advocateListItem">
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:pageBlock>


public class MyController {
    public String currentDeskId;
    public String searchName { get; set; }
    public List<CCx_AdvocateServices.onboardingAdvocateDecorator> advocates { get; set; }

    public void filterByName() {
        system.debug('*** searchName: ' + searchName);
        if (string.isBlank(searchName)) return;
        advocates = CCx_AdvocateSelector.selectAdvocateDecoratorForDeskAndName(currentDeskId, searchName);
        system.debug('*** advocates: ' + advocates);
        searchName = null;
    }

    // Constructor
    // etc
}
2

I know this was asked a long time ago, but I think this deserves an answer.

The issue is that your function returns void. This will not refresh the ViewState. Instead, make it return PageReference and return null in the function body. I was able to recreate your code (using Accounts) and get it working.

This is what the working version looks like:

public PageReference filterByName() {
    if (string.isBlank(searchName)) return null;
    searchName = '%' + searchName + '%';
    accounts = [SELECT Name, Id From Account WHERE Name Like :searchName];
    searchName = null;
    return null;
}

(Modified for a simple Accounts query)

Also I noticed that you have not defined any columns in your pageBlockTable I'm guessing that's because you left that out for this example. If not, you'll have to add them.

Here is some another question on SO that has some similarities to yours:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.