You need to sign in to do that
Don't have an account?
Shekhar Agarwal
Would like to create voting criteria using Lightning Component Hi, I am trying to create simple voting criteria in Lightning. If age>=18. Alert = Congrats!! You can vote. Show button = Submit your vote else--- alert = 'Sorry you can't vote'.
My question is in voting criteria, whenever your component is loaded whether you entered age value or not, attribute 'Eligible for voting' will give you default value' false'(i.e. Congrats! You can vote. OR 'Sorry! you can't ' . But I want 'Eligible for voting' value should only be displayed when we enter the age field. else don't show anything. How will be achieve this?
Below is the code:
Component Code:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="EligibleOrNot" type="boolean" default="true"></aura:attribute>
<ui:inputText label="First name" aura:id="fname" placeholder="First Name"></ui:inputText>
<ui:inputText label="Last name" aura:id="lname" placeholder="Last Name"></ui:inputText>
<ui:inputText label="Age" aura:id="age" placeholder="Enter Age"></ui:inputText>
<ui:button label="Check Eligibility" aura:id="checkeligibility" press="{!c.getEligibility}"></ui:button>
<aura:if isTrue="{!v.EligibleOrNot}">
Congrats! You can Vote.
<ui:button label="Give Vote"></ui:button>
<aura:set attribute="else">
Sorry! You can not vote.
<ui:button label="Cancel Vote"></ui:button>
</aura:set>
</aura:if>
</aura:component>
Controller Code:
({
getEligibility : function(component) {
var vAge = component.find("age").get("v.value");
if(vAge != null && vAge>=18){
component.set("v.EligibleOrNot",true);
}
if(vAge!=null && vAge<18){
component.set("v.EligibleOrNot",false);
}
}
})
Below is the code:
Component Code:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="EligibleOrNot" type="boolean" default="true"></aura:attribute>
<ui:inputText label="First name" aura:id="fname" placeholder="First Name"></ui:inputText>
<ui:inputText label="Last name" aura:id="lname" placeholder="Last Name"></ui:inputText>
<ui:inputText label="Age" aura:id="age" placeholder="Enter Age"></ui:inputText>
<ui:button label="Check Eligibility" aura:id="checkeligibility" press="{!c.getEligibility}"></ui:button>
<aura:if isTrue="{!v.EligibleOrNot}">
Congrats! You can Vote.
<ui:button label="Give Vote"></ui:button>
<aura:set attribute="else">
Sorry! You can not vote.
<ui:button label="Cancel Vote"></ui:button>
</aura:set>
</aura:if>
</aura:component>
Controller Code:
({
getEligibility : function(component) {
var vAge = component.find("age").get("v.value");
if(vAge != null && vAge>=18){
component.set("v.EligibleOrNot",true);
}
if(vAge!=null && vAge<18){
component.set("v.EligibleOrNot",false);
}
}
})
Please apply following code. i did some changes in your code.
I think that's the good approach.
let me know if it helps you and mark it as best answer.
Thank you
All Answers
Hi,
Please find the solution.
Please let me know it is working or not?
Please mark it as the Best Answer so that other people would take references from it.
Thank You
Please apply following code. i did some changes in your code.
I think that's the good approach.
let me know if it helps you and mark it as best answer.
Thank you
You can try like this
Create one attribute
<aura:attribute name="Age" type="Integer" />
Yes, I can. First of all, we are checking age if user not put age into field than first we will alert that 'Please enter your age' and then we will work furthure. From this attribute 'checkEligibleForVote' we are checking age. if age is null then after alert we will return. And EligibleOrNot attribute is checking user is under 18 or above.
Thank you