[SalesForce] Filter Visualforce List Dynamically Using Picklist

I am trying to create a visualforce page that has a list of field values in a table that uses a picklist that on change will reRender the list by filtering by a specific field value. My problem is that, I can't seem to connect the picklist value with the field in the table that I want filtered. Can someone show me how I could fix this? Thanks!

Controller

    public with sharing class DynamicCasesExtension {

    public Case c { get; set; }
    public String objectName { get; set; }
    public List<String> objectFields { get; set; }
    public List<Case> SObjectListToShow { get; set; }

    public DynamicCasesExtension(){
        objectName = 'Case';
        SObjectListToShow = getSObjectList();
    }

    // Getting field list for the sObject
    public List<Case> getSObjectList(){
        objectFields = new List<String>();
        Map<String, Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        Schema.sObjectType sObjType = globalDescription.get(objectName);
        Schema.DescribeSObjectResult res = sObjType.getDescribe();

        Map<String , Schema.SObjectField> mapFieldList = res.fields.getMap();
        for(Schema.SObjectField field : mapFieldList.values())
        {
            Schema.DescribeFieldResult fieldResult = field.getDescribe();
            if(fieldResult.isAccessible() && (fieldResult.getName() == 'CaseNumber' || fieldResult.getName() == 'Assigned_To__c' || fieldResult.getName() == 'Subject' || fieldResult.getName() == 'Priority' || fieldResult.getName() == 'Status' || fieldResult.getName() == 'CreatedDate')) 
            {
                objectFields.add(fieldResult.getName());
            }
        }

        //Building Query with the fields
        Integer i = 0;
        String fieldsToFetch = '';
        Integer len = objectFields.size();

        for(String temp: objectFields){
            if(i==len-1){
                  fieldsToFetch = fieldsToFetch + temp;
            } else {
                  fieldsToFetch = fieldsToFetch + temp + ',';
            }
            i++;
        }

        ID MerchantTypeRecordType = [SELECT Id FROM RecordType WHERE sObjectType = 'Case' AND DeveloperName = 'Merchant_Support' LIMIT 1].id;
        String caseStatus = ApexPages.currentPage().getParameters().get('c.Status');

        String qryStr = 'Select ' + fieldsToFetch + ' From ' + objectName + ' WHERE (RecordTypeId = :MerchantTypeRecordType and Case.Status = ' + caseStatus + ') Limit ' + 20; 
        return Database.Query(qryStr);

    }

}

===============================================================================

Visualforce Page

<apex:page controller="DynamicCasesExtension">

<apex:form >
    <apex:pageMessages />

    <apex:pageBlock title="Filter Based on Case Status: ">
        <apex:outputText value="Filter Based on Case Status"></apex:outputText>
        <apex:actionRegion >
            <apex:selectList value="{!c.status}" multiselect="false" size="1" required="true">
                <apex:selectOption itemValue="None" itemLabel="-None-"/>
                <apex:selectOption itemValue="New" itemLabel="New" />
                <apex:selectOption itemValue="Closed" itemLabel="Closed"/>
                <apex:selectOption itemValue="In Progress" itemLabel="In Progress"/>
                <apex:actionSupport event="onchange" rerender="result"/>
            </apex:selectList>
        </apex:actionRegion>
    </apex:pageBlock>

    <apex:pageBlock >
        <apex:outputPanel>
            <apex:pageBlockTable value="{!sObjectList}" var="res" id="result">
                <apex:repeat value="{!objectFields}" var="field" >
                    <apex:column value="{!res[field]}" id="results"/>
                </apex:repeat>
                <apex:actionPoller rerender="results" interval="10" status="state"/>
                <apex:actionStatus id="state" startText="Refreshing" stopText=""/> 
            </apex:pageBlockTable>
        </apex:outputPanel>
    </apex:pageBlock>
</apex:form>

Best Answer

I have changed the below controller. I initially didn't see that you're not calling any method from the apex:actionSupport tag. I have changed the below code (Apex / VF) and added some comments.

public with sharing class DynamicCasesExtension {

    public Case c { get; set; }
    public String objectName { get; set; }
    public List<String> objectFields { get; set; }
    public List<Case> SObjectListToShow { get; set; }

    public DynamicCasesExtension(){
        objectName = 'Case';
        //set the status to the value you need. In your case I assume it is coming in as a parameter. This is to be set only for the first load. 
        c.Status = ApexPages.currentPage().getParameters().get('c.Status');
        //SObjectListToShow = getSObjectList();
        generateList();
    }

    // a new method which will be called everytime the value in the picklist is changed
    public void generateList() {
        SObjectListToShow = caseSObjectList();
    }

    // Getting field list for the sObject. Changed the name of the method.
    public List<Case> caseSObjectList(){
        .
        .
        .
        //Change it to c.Status so that it gets set to the value selected from the page.
        String caseStatus = c.Status;
        .
        .
        .
    }

}

VF Page:

<apex:actionSupport event="onchange" rerender="result" action="{!generateList}"/>
Related Topic