You need to sign in to do that
Don't have an account?
Mokshada
BMI calculator using aura component
Hi, I was trying to implement Bmi calculator using aura component but it is not working when i click on button it is showing zero after adding alerts in js i got to know that I am getting value as 0 of height and weight, how to make this code work can anyone help?
apex:
public class BMICalculatorController {
@AuraEnabled
public static Decimal calculateBMI(Decimal height, Decimal weight) {
Decimal heightInMeters = height / 100;
Decimal bmi = weight / (heightInMeters * heightInMeters);
System.debug(bmi);
return bmi;
}
}
component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="BMICalculatorController">
<aura:attribute name="height" type="Decimal" default="0"/>
<aura:attribute name="weight" type="Decimal" default="0"/>
<aura:attribute name="bmiResult" type="Decimal" default="0"/>
<div>
<label>Height (in CM):</label>
<input type="number" aura:id="heightInput" value="{!v.height}"/>
</div>
<div>
<label>Weight (in KG):</label>
<input type="number" aura:id="weightInput" value="{!v.weight}"/>
</div>
<div>
<button onclick="{!c.calculateBMI}">Calculate BMI</button>
</div>
<div>
<h2>BMI Result: {!v.bmiResult}</h2>
</div>
</aura:component>
Controller:
({
calculateBMI: function(component, event, helper) {
var height = component.get("v.height");
var weight = component.get("v.weight");
alert(component.get("v.height"));
alert(component.get("v.weight"));
//if (height > 0 && weight > 0) {
var action = component.get("c.calculateBMI");
action.setParams({
height: height,
weight: weight
});
action.setCallback(this, function(response) {
var state = response.getState();
//alert(response.getState())
alert(state)
if (state === "SUCCESS") {
var bmiResult = response.getReturnValue();
component.set("v.bmiResult", bmiResult);
} else {
alert("Error");
}
});
$A.enqueueAction(action);
}
})
Thanks,
apex:
public class BMICalculatorController {
@AuraEnabled
public static Decimal calculateBMI(Decimal height, Decimal weight) {
Decimal heightInMeters = height / 100;
Decimal bmi = weight / (heightInMeters * heightInMeters);
System.debug(bmi);
return bmi;
}
}
component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="BMICalculatorController">
<aura:attribute name="height" type="Decimal" default="0"/>
<aura:attribute name="weight" type="Decimal" default="0"/>
<aura:attribute name="bmiResult" type="Decimal" default="0"/>
<div>
<label>Height (in CM):</label>
<input type="number" aura:id="heightInput" value="{!v.height}"/>
</div>
<div>
<label>Weight (in KG):</label>
<input type="number" aura:id="weightInput" value="{!v.weight}"/>
</div>
<div>
<button onclick="{!c.calculateBMI}">Calculate BMI</button>
</div>
<div>
<h2>BMI Result: {!v.bmiResult}</h2>
</div>
</aura:component>
Controller:
({
calculateBMI: function(component, event, helper) {
var height = component.get("v.height");
var weight = component.get("v.weight");
alert(component.get("v.height"));
alert(component.get("v.weight"));
//if (height > 0 && weight > 0) {
var action = component.get("c.calculateBMI");
action.setParams({
height: height,
weight: weight
});
action.setCallback(this, function(response) {
var state = response.getState();
//alert(response.getState())
alert(state)
if (state === "SUCCESS") {
var bmiResult = response.getReturnValue();
component.set("v.bmiResult", bmiResult);
} else {
alert("Error");
}
});
$A.enqueueAction(action);
}
})
Thanks,
Mokshada,
Check below code it will work:-
Component:-
Controller:-
Apex:-
hope this will help you, please mark this as best answer.
Thanks