1

How can i save the value of selected option in lightning component. Here is my code:

Component:

<aura:attribute name="pollQuestionObj" type="LghtCompNS__Poll_Question__c" default="{'sobjectType': 'LghtCompNS__Poll_Question__c'}"/>

<aura:attribute name="visibleTo" type="String[]" default="Admin,Everyone"/>
<ui:inputSelect aura:id="visTo" class="slds-input" label="Visible to after vote" change="{!c.visibleToUser}" >
            <aura:iteration items="{!v.visibleTo}" var="vsbl"> 
                 <aura:if isTrue="{!index ==0}" >
                    <ui:inputSelectOption text="{!vsbl}" value="true"/>
                    <aura:set attribute="else">
                        <ui:inputSelectOption text="{!vsbl}" />
                    </aura:set>
                </aura:if>
            </aura:iteration>
        </ui:inputSelect> 

Controller:

visibleToUser : function(component, event, helper) {
     var visibleToItem = component.get("v.pollOptions");
     helper.visibleToUser(component, visibleToItem, function(a){
        console.log('visibleTo Item Selected');            
    });
}

Helper:

visibleToUser: function(component, event, helper){
   var resultCmp = component.find("visTo").get("v.value");       
   alert(resultCmp);


}
  //Add Polling Questions.
addPollQuestions : function(component,pollQuest,callback) {
    var action = component.get("c.addPollQuestion"); // name on the apex class
    var questOptions = component.find("options").get("v.value");


    var pollQuestion = component.find("question");
    var questValue = pollQuestion.get("v.value");

    var optionVal = component.find();        

    //Get visible to value.
    var visibleVal = this.visibleToUser();

    //Check if question is entered.
    if(questValue == undefined)
    {
        alert('Please enter question');
         /*var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Required!",
                    "message": "Please enter question."
                });                    
         toastEvent.fire();*/
         return false;
    }     


    var optionValList = [];

    for (i = 1; i <= questOptions; i++) {
        var optionVal = $('#option' + i).val()
        if( optionVal != "" &&  optionVal != undefined)
        {
            optionVal = optionVal; 
            optionValList.push(optionVal);                
        }
        else{
            alert("Please enter all number of options as selected");
            /*var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
                "title": "Required!",
                "message": "Please enter all number of options as selected."
            });                    
            toastEvent.fire();*/
            return false;
        }

    } 
    //alert('Option Value List: ' + optionValList);
    /*if(optionValList == undefined && optionValList.length < 2)
    {
        alert("Please enter atleast 2 options");
        return false;
    }*/


    action.setParams({"pollQuest": pollQuest,"optionValList":optionValList});    
    //action.setParams({"pollQuest": pollQuest});
    /*if(callback)
    {
        action.setCallback(this, callback);
    }*/
    if(callback)
    {
        // Create a callback that is executed after
        // the server-side action returns
        action.setCallback(this, function(response) {
            //Get response state.
            var state = response.getState();

            //Check if response is success.
            if (state === "SUCCESS") { 
                alert("Poll Question saved successfully");
                /*var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "Poll Question saved successfully."
                });                    
                toastEvent.fire();*/

            }
            else if (state === "INCOMPLETE") {    
               alert("INCOMPLETE");    
            }    
            //Check for error.
            else if (state === "ERROR") {    
                var errors = response.getError();    
                if (errors) {    
                    if (errors[0] && errors[0].message) { 
                        alert("Error message: " +errors[0].message);
                        /*var toastEvent = $A.get("e.force:showToast");
                        toastEvent.setParams({
                            "title": "Error!",
                            "message": errors[0].message
                        });                    
                        toastEvent.fire();*/
                    }
                } else {                        
                    alert("Unknown error");                     
                }    
            }
        }); 

    }
    $A.enqueueAction(action);
}

0

You must log in to answer this question.