[SalesForce] Empty values in a picklist

Root-cause: From the controller alone, it adds empty values but it can be removed with javascript though the javascript doesn't work.

 VF PAGE

 <apex:column rendered="{!allowDiscount}" id="DiscountCol">
                                <apex:facet name="header">Discount %</apex:facet>
                                <apex:selectList id="Discount" size="1" multiselect="false" value="{!results.Discount}" rendered="{!AND(results.allowDiscountItem,allowDiscount,results.showDetails)}">
                                    <apex:selectOptions id="discOptions" rendered="{!allowDiscountFlag}" value="{!DiscountList}"/>
                                </apex:selectList>
</apex:column>


//javascript removing empty spaces 
<script>
                                    var entries = document.getElementById("{!$Component.j_id0:form:discDetail:discSection:discountTabTable:1:Disc}").value;
                                    for(var i = entries.length - 1; i>=0; i--){
                                        if(entries.discountList[i].text == ""){
                                           entries.remove(i);
                                        }
                                    }
</script>

CONTROLLER

 public List<SelectOption> getDiscountList() 
    {
        List<SelectOption> discountList = new List<SelectOption>();

        List<Product__c> familyDiscount = [SELECT Discount__c FROM Product__c where name =: business._Product__c];

        SelectOption sOption = new SelectOption('1', '<optgroup style="background:#FFFFFF;"></optgroup>');
        sOption.setEscapeItem(false);
        discountList.add(sOption);

        for(Product__c pf: familyDiscount)
        {
            if(pf.Discount__c != NULL)
            {
                string color;
                for(string discount: pf.Discount__c.split(','))
                {
                    if(integer.valueOf(discount) <= integer.valueOf('12')) 
                    {
                        color = 'green';
                    }
                    else{
                        color = 'red';
                    }
                    //system.debug('color:'+color);
                    SelectOption entry = new SelectOption(discount,'<option value="'+discount+'" style="color:'+color+';">' + discount+'%'+'</option>');        
                    entry.setEscapeItem(false);
                    system.debug(entry);
                    if(entry != null){
                        discountList.add(entry);
                    }
                }
            }
        }

        return discountList;
    }

Help me to get rid of this empty spaces.
enter image description here

What could be probably changes in the code to be made?

Best Answer

Try below,

public List<SelectOption> getDiscountList() 
    {
        List<SelectOption> discountList = new List<SelectOption>();

        List<Product__c> familyDiscount = [SELECT Discount__c FROM Product__c where name =: business._Product__c];

        SelectOption sOption = new SelectOption('1', '<optgroup style="background:#FFFFFF;"></optgroup>');
        sOption.setEscapeItem(false);
        discountList.add(sOption);

        for(Product__c pf: familyDiscount)
        {
            if(pf.Discount__c != NULL)
            {
                string color;
                for(string discount: pf.Discount__c.split(','))
                {
                    discount=discount.trim();
                    if(integer.valueOf(discount) <= integer.valueOf('12')) 
                    {
                        color = 'green';
                    }
                    else{
                        color = 'red';
                    }
                    //system.debug('color:'+color);
                    if(discount!=''){
                        SelectOption entry = new SelectOption(discount,'<option value="'+discount+'" style="color:'+color+';">' + discount+'%'+'</option>');        
                        entry.setEscapeItem(false);                        
                        discountList.add(entry);
                    }
                }
            }
        }

        return discountList;
    }
Related Topic