To create a Lightning checkbox in a Salesforce Lightning Aura Component, you would use the component. Here's a basic example of how you can create a Lightning checkbox
In this example:
We have an attribute isChecked of type Boolean to track the checked state of the checkbox.
We use the component with the type="checkbox" attribute to create the checkbox.
The label attribute specifies the label text for the checkbox.
The checked attribute is bound to the isChecked attribute using two-way data binding, ensuring that the checkbox state is synchronized with the isChecked attribute.
The onchange attribute specifies the controller action handleCheckboxChange that will be called when the checkbox value changes.
Now, let's define the controller action handleCheckboxChange in the corresponding JavaScript controller (controller.js) or helper (helper.js) file:
({
handleCheckboxChange: function(component, event, helper) {
// Retrieve the new checked state of the checkbox
var isChecked = event.getSource().get("v.checked");
// Update the isChecked attribute with the new value
component.set("v.isChecked", isChecked);
// You can perform additional actions based on the checkbox state change here
}
})
In the handleCheckboxChange controller action:
We retrieve the new checked state of the checkbox using event.getSource().get("v.checked").
We update the isChecked attribute with the new value using component.set("v.isChecked", isChecked). This will automatically update the checkbox's checked state due to two-way data binding.
This creates a basic Lightning checkbox in an Aura Component, and the handleCheckboxChange action allows you to handle any logic or actions based on the checkbox state change.