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
Service Cloud 60Service Cloud 60 

To get Record id from datatable in lightning component.

Hi, 
Can anyone tell me how to get record Id when i click on the checkbox of datatable in lightning component ?

TIA
Best Answer chosen by Service Cloud 60
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You can use onrowselection event handler. Try like this:

Component:
<lightning:datatable data="{! v.mydata }" 
                         columns="{! v.mycolumns }" 
                         keyField="Id" 
                         onrowselection="{! c.handleRowAction }"/>

Controller:
handleRowAction : function(component, event, helper){
        var selRows = event.getParam('selectedRows');
        console.log('selRows -> ' + JSON.stringify(selRows));
        var selectedRowsIds = [];
        for(var i=0;i<selRows.length;i++){
            selectedRowsIds.push(selRows[i].Id);  
            console.log('selectedRowsIds -> ' + selectedRowsIds);
        }   
    },

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You can use onrowselection event handler. Try like this:

Component:
<lightning:datatable data="{! v.mydata }" 
                         columns="{! v.mycolumns }" 
                         keyField="Id" 
                         onrowselection="{! c.handleRowAction }"/>

Controller:
handleRowAction : function(component, event, helper){
        var selRows = event.getParam('selectedRows');
        console.log('selRows -> ' + JSON.stringify(selRows));
        var selectedRowsIds = [];
        for(var i=0;i<selRows.length;i++){
            selectedRowsIds.push(selRows[i].Id);  
            console.log('selectedRowsIds -> ' + selectedRowsIds);
        }   
    },

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Service Cloud 60Service Cloud 60
Thanks Khan Anas