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
Ritesh__Ritesh__ 

Removing None Option in PickList visualforce apex

i have created an object with name PS_Detail__c .in this custom object i created a custom field of picklist type.in this picklist i defined two values Dollar ,Unit. when i go to P/S Detail Tab for creating a new PS_Detail__c object then i get None also as a value in picklist field .i want only Dollar and Unit as Picklist values .some one please help how to remove this None from Picklist

CaptainObviousCaptainObvious

Since your picklist field is on a Visualforce page, you can use some jQuery magic...

 

Give your field a unique id.

 

<apex:inputField value="{!My_Picklist__c}" id="myPicklist" />

 

Then, remove the option with the blank value as follows:

 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('[id*=myPicklist] option[value=""]').remove();
    }); 
</script>

 

sandeep@Salesforcesandeep@Salesforce

 We can not remove None option directly becaus this inputfield ( pickiist Type). We need to make our custom pick list .

sandeep@Salesforcesandeep@Salesforce

Here is a way to create custom pick list 

<!-- Page: -->
                        
<apex:page controller="sampleCon"> <apex:form> <apex:selectList value="{!countries}" multiselect="true"> <apex:selectOptions value="{!items}"/> </apex:selectList><p/> <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel> <p>You have selected:</p> <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page> /*** Controller: ***/
    public class sampleCon { String[] countries = new String[]{}; public PageReference test() { return null; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('US','US')); options.add(new SelectOption('CANADA','Canada')); options.add(new SelectOption('MEXICO','Mexico')); return options; } public String[] getCountries() { return countries; } public void setCountries(String[] countries) { this.countries = countries; } }
Ryan DRyan D
The problem with this is that it works on document.ready but once you pick a value and then if you drop the picklist down again, 'none' shows up again. I am trying to figure out how to handle this. I tried the following but it didn't quite work: $('[id*=myPicklist]').change(function(){
$('[id*=myPicklist] option[value=""]').remove();
});
Anita BhanarkarAnita Bhanarkar

Please refer below link. It worked for me!

http://mrjavascript.blogspot.in/2013/04/blog-post.html