How to hide/remove Picklist Value in a Visual Force Page

I just want to extend @Praveen answer:

<apex:page standardController="Opportunity">
    <apex:form >
        <apex:pageBlock >
            <apex:inputField value="{!Opportunity.StageName}" />
            <apex:inputField value="{!Opportunity.StageName}" id="stageId"/>
            <script type="text/javascript">
            (function(){
                var e = document.querySelectorAll('[id$="stageId"]')[0];
                if (e && !e[0].value) {e.remove(0);}
            })();
            </script>
        </apex:pageBlock>
    </apex:form>
</apex:page>

We don't have to load jQuery library here.


This can be achieved by using the Schema.DescribeFieldResult Salesforce standard class to retrieve all the possible field label value pairs for a picklist field. The retrieved value pairs are then stored inside a collection of select option objects, which can be used as the basis of a select list. Below is a snapshot of the code from the apex controller, and also the Visualforce page.

VF Page:

<apex:page controller="NoNoneForPicklistController">

    Please select required status:
    <apex:form >
        <apex:selectList size="1" value="{!invoiceStatement.Status__c}">
            <apex:selectOptions value="{!statusOptions}"/>
        </apex:selectList>
    </apex:form>  

</apex:page>

Apex Class:

public class NoNoneForPicklistController{

    public Invoice_Statement__c invoiceStatement {get;set;}
    public List<SelectOption> statusOptions {get;set;}

    // Constructor called when page is accessed.
    public NoNoneForPicklistController() {

        invoiceStatement = new Invoice_Statement__c();        
        statusOptions = new List<SelectOption>();

        // Use DescribeFieldResult object to retrieve status field.
        Schema.DescribeFieldResult statusFieldDescription =                     Invoice_Statement__c.Status__c.getDescribe();

        // For each picklist value, create a new select option
        for (Schema.Picklistentry picklistEntry:                                statusFieldDescription.getPicklistValues()){

            statusOptions.add(new SelectOption(                                     pickListEntry.getValue(),pickListEntry.getLabel()));

            // obtain and assign default value
            if (picklistEntry.defaultValue){
                invoiceStatement.Status__c = pickListEntry.getValue();
            }  
        }     
    }
}

You can certainly do it in client side using javascript, rather than using Apex describe calls constructing your own select options. Using jquery, you can remove the first option (-None-) like this:

<apex:page controller="AccountController">
    <apex:includeScript value="https://code.jquery.com/jquery-1.11.1.min.js"/>
    <script>
        j$ = jQuery.noConflict();
        j$(document).ready( function () {
            j$('option[value=""]').remove();
        });

        <apex:form>
            <apex:inputField value="{!employee__c.ranking__c}" />
        </apex:form>
    </script>
</apex:page>

Tags:

Visualforce