function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LUIGI EMANUEL TAORMINALUIGI EMANUEL TAORMINA 

How can I get the boolean value of my checkbox in lightning datatable?


I would like to know how to take the boolean value at the click of the lightning datatable checkbox in order to check to create a sort of product counter in mode that increases when the product is selected and descriptions when the product has been removed
LUIGI EMANUEL TAORMINALUIGI EMANUEL TAORMINA
Hi CharuDutt, maybe I have wrongly formulated my question, I will explain it better.
I have a Lightning: datatable which fetches the records of my products through an Apex class, when I generate my datatable, "checkboxes" appear that I need to select individual products to add them via a Lightning: button in a new one datatable. I created a new button that represents my cart. Inside the button there is an aura attribute "counter" which my intent is to increase or decrease it when selecting or removing the product in the cart. I would like to know which control to apply to make this automatism work.
User-added image
LUIGI EMANUEL TAORMINALUIGI EMANUEL TAORMINA
As you can see I created an Aura: attribute "counter" which I made an increment in the 'getSelectedProd' function in the controller, this thing I need as with each selection of the book this variable increases and I call it in a lightning: button. I would like to know what control to do on this Attribute when selecting and removing a book from the list. The variable should be incremented if a new book has been added to the cart or decremented if a book has been removed from the cart.
 
Component:
<!-- Create Datatable -->   
    <aura:attribute type="Product2__c[]" name="prodList"/>
    <aura:attribute type="Product2__c[]" name="prodList2"/>
    <aura:attribute name="control" type="Boolean" default='false'/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="Counter" type="Integer" default="0" />
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    <div class="container" style="height:250px">
        
        <lightning:datatable style="color:red ;font-size:14px;font-family: Lucida Console, Courier New, monospace;" 
                             aura:id="prodTable"  
                             data="{! v.prodList }"     
                             columns="{! v.mycolumns }"     
                             keyField="Id"
                             showRowNumberColumn="true" 
                             editable="true"
                             onrowselection="{! c.getSelectedProd }"
                             
                             />
    </div>
    <br /> <br />  <br/> 
    <lightning:button
                      label="{!v.Counter}"
                      title="Cart"
                      onclick="{! c.handleClick }"
                      iconName="utility:cart"
                      >
    </lightning:button>
    <br /> <br />
    <aura:if isTrue="{!v.control}">
        <div class="container" style="height:250px">
            <lightning:datatable style="color:red ;font-size:14px;font-family: Lucida Console, Courier New, monospace;"
                                 aura:id="prodTable"  
                                 data="{! v.prodList2 }"     
                                 columns="{! v.mycolumns }"     
                                 keyField="Id"    
                                 hideCheckboxColumn="true"  
                                 onrowselection="{! c.getSelectedProd }"/> 
        </div>
        <aura:set attribute="else">
            <h1 style="font-size:28px; text-align:center; color:red">Empty Cart</h1>
        </aura:set>
    </aura:if>



Controller:
getSelectedProd: function (component, event) {
            var selectedRows = event.getParam('selectedRows');
            component.set( "v.prodList2", selectedRows );
            var count = component.get('v.Counter');
            count = count+1;
            component.set('v.Counter', count);
            var list = component.get("v.prodList2");
            if(list.length==0){
            component.set("v.control",false);
            }
            else{
            console.log("Size = "+list.length);
            }
            },
            handleClick : function (component, event, helper) {
            component.set( "v.control", true );
            
            }


    
LUIGI EMANUEL TAORMINALUIGI EMANUEL TAORMINA

as you can see below there is a lightning: button that marks the numbers of the selected books, unfortunately I don't know how to decrease that number when removing the selected book



User-added image