Lightning Component: Show check boxes for a records list and display selected record

Lets take an example to show check boxes for a records list and display selected record in an alert box.

To show check boxes for a list view, we will first display dummy data of 5 contacts in our component.

Let’s create an apex class to query 5 random contacts for our component:

public class contactcontroller {
       public static List<Contact>  getcontacts() {
         return [SELECT Id, name FROM Contact limit 5];

Next we will display the above contacts in our component:

<aura:component controller="contactcontroller" implements="force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes" access="global">
<!-- List to store contacts data -->
<aura:attribute name="Contacts" type="List" />
<!-- Initialization method calls controller to get data -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
 <div class="slds-col"></div>
<!--tabular data -->
<table class="slds-table slds-table_bordered slds-table_striped slds-table_cell-buffer slds-table_fixed-layout">
<tr class="slds-text-heading_label">
<th scope="col">
<div class="slds-truncate" title="Name">Name</div>
 <!-- Use the Apex model and controller to fetch server side data -->
<aura:iteration items="{!v.Contacts}" var="Contact">
<th scope="row">
<div class="slds-truncate" title="{!Contact.Name}">
<!-- Contact name is linked to the Salesforce record page -->
<a href="{!'/lightning/r/Contact/'+ Contact.Id + '/view'}" target="_blank">{!Contact.Name}</a></div>

And the JS controller:

    doInit: function(component, event, helper){
helper.getcontacts(component, event, helper);

This is how my component looks like:

Lightning Component: Show check boxes for a records list and display selected record

To display check boxes, we will add the below line after line 11 in our component.

<th> <ui:inputCheckbox aura:id="selectAll" change="{!c.SelectAllCheck}"/>   </th>

This line should be added just below “<tr class=”slds-text-heading_label”>”. Also add the SelectAllCheck function to the controller. When we change the header check box, the above function changes the check boxes of all the rows.

Add the next line after line 23 in the component:

<ui:inputCheckbox aura:id="checkContacts" value="" text="{!Contact.Name}"/>

Once the component is saved, it will look like below screen shot:

Lightning Component: Show check boxes for a records list and display selected record

Add code for “SelectAllCheck” in the JS controller:

            var getID = component.get("v.Contacts");
        var checkvalue = component.find("selectAll").get("v.value");   
        var checkContacts=component.find("checkContacts"); 
           var len=checkContacts.length;
        if(checkvalue == true){
               if(len == 1){
        for (var i = 0; i<len; i++){
                if(len == 1){
        for (var i = 0; i<len; i++){

After saving the controller, reload the component. You should now be able to select/un-select all rows by clicking on the header checkbox.

Next add a button to the component:

<lightning:button variant="neutral" 
                                          label="Show Alert"
                                          title="Show Alert"

Add this just after the table tag ends. Also add the function for ShowAlert in the controller:

        var checkContacts=component.find("checkContacts");
       var ln=checkContacts.length

        for(var i=0;i<ln;i++)
             var v= checkContacts[i].get("v.value");
           var context=context.concat(checkContacts[i].get("v.text"),' ');

Now when you select any row(s), and click on the Show Alert button, the selected contact names will be shown in the alert window.

We can further use the above to take actions on selected rows such as send email or start an approval process.

Also read:

Checkbox group can also be used:

Leave a Reply

Your email address will not be published.

Scroll to top