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
Nisha Rao PatnanaNisha Rao Patnana 

how to add 'required' validation to a datatable checkbox

I an new to Datatable. I have managed to populate data and display the table with checkbox ,now I want the user to select atlest one of the checkbox if he/she doesn't then throw an alert saying the user has to select atlest one checkbox. How do I add the required selection to the datatable code
Akshay Dhiman 63Akshay Dhiman 63
Hi Nisham,
Below is the code to add validation:-

Apex Class

public class LightningDataTableController {
        @AuraEnabled
    public static List<Account> fetch(){
        List<Account> acc = [SELECT Name, Phone, Rating FROM Account LIMIT 20];
        return acc;
    }
}


Aura Component

<aura:component controller="LightningDataTableController">
    <aura:attribute name="mydata" type="Object"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="showNewTable" type="boolean" default="false" />
    <aura:attribute name="checkbox" type="boolean" default="false" />
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <lightning:datatable data="{! v.mydata }"
                         onrowselection="{! c.handleRowAction }"
                         columns="{! v.mycolumns }"
                         keyField="Id"/>
    <lightning:button variant="brand" label="CHECKING" title="Brand action" onclick="{! c.handleClick }" />
    <aura:if isTrue="{!v.showNewTable}">
        <c:TeamMemberMSDatatable />//Your Code Here which you perform when button is  click and show what you want.
    </aura:if>
</aura:component>

Controller:-
({
    doInit : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Account Name', fieldName: 'Name', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', type: 'phone'},
            {label: 'Rating', fieldName: 'Rating', type: 'text'}
        ]);
        
        var action = component.get("c.fetch");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log(response.getReturnValue());
                component.set("v.mydata", response.getReturnValue());
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    handleClick : function(component,event,helper){
        if(component.get("v.checkbox")==false){
        alert('You cannot checked the checkbox');
        }
        else{
            component.set("v.showNewTable",true);
        }
    },
     handleRowAction : function(component, event, helper){
        var selRows = event.getParam('selectedRows');
         component.set("v.checkbox",selRows);
         console.log('selRows',selRows);
    },
})


Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
Nisha Rao PatnanaNisha Rao Patnana
Hi Akshay,

The code still dosent work i mean when i add the aura component and controller code in the devloper section and try debugging my flow the flow data dosent load atall and keeps loading not sure where I am going  wrong?

Have provided you the snipped of the added code
Akshay Dhiman 63Akshay Dhiman 63
Hi Nisha,

Make sure you have the accounts in your org and internet connectivity is good also I have double-checked this code and its working fine for me.

If any error is throwing then please share the screenshot as well.

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
Nisha Rao PatnanaNisha Rao Patnana
Hi,

Its no internet issue cause the page loads fine when I remove the code. There is no error as such It just doesn't load

Thanks,
Nisha
Akshay Dhiman 63Akshay Dhiman 63
Hi Nisha,

Can you please check the console on line number 5 which you gave the screenshot as I also highlighted the line in your screenshot?

Also please share the screenshot of your console where the response is returning.

User-added image

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay