You need to sign in to do that
Don't have an account?
Robert Gallahue
Issues with keyField in Lightning DataTable
Having an issue getting the Record Ids of the selected record in Lightning Datatable.
Here is my controller
and here is my setRecordId function
Var selectedRows returns the correct selected row as an object within an array but i can't seem to find the correct syntax to access that records ID for some reason. Let me know if any additional information is needed here.
appreciate the help
Here is my controller
<aura:component implements="lightning:availableForFlowScreens" access="global"> <!-- attributes --> <aura:attribute name="dataArr" type="String[]"/> <aura:attribute name="data" type="Object"/> <aura:attribute name="columnsStr" type="String"/> <aura:attribute name="columns" type="List"/> <aura:attribute name="maxRowSelection" type="Integer" default="1"/> <aura:attribute name="numOfRowsSelected" type="Integer" default="0"/> <aura:attribute name="key" type="String" default="Id"/> <aura:attribute name="recordId" type="String" /> <aura:attribute name="recordIds" type="String" /> <!-- handlers--> <aura:handler name="init" value="{!this }" action="{! c.doInit }"/> <div style="height: 300px"> <lightning:datatable keyField="{!v.key}" data="{! v.data }" columns="{! v.columns }" maxRowSelection="{! v.maxRowSelection }" onrowselection="{! c.setRecordId }" /> </div> </aura:component>
and here is my setRecordId function
setRecordId : function(component, event, helper){ var selectedRows = event.getParam('selectedRows'); var key = component.get('v.key'); var recIds = ''; console.log(selectedRows); if(selectedRows){ if(selectedRows.length === 1){ console.log(selectedRows.id) console.log(selectedRows[key]) console.log(selectedRows[0][key]) component.set('v.recordId', selectedRows[0][key]); } else{ for(let i = 0; i < selectedRows.length; i++){ recIds += selectedRows[i][key] + ','; } component.set('v.recordIds', recIds); component.set('v.numOfRowsSelected', selectedRows.length); } } },
Var selectedRows returns the correct selected row as an object within an array but i can't seem to find the correct syntax to access that records ID for some reason. Let me know if any additional information is needed here.
appreciate the help
All Answers
setRecordId : function(component, event, helper){
var selectedRows = event.getParam('selectedRows');
var key = component.get('v.key');
var recIds = '';
console.log(selectedRows);
if(selectedRows){
if(selectedRows.length === 1){
console.log(selectedRows[0].Id)
component.set('v.recordId', selectedRows[i].Id);
}
else{
for(let i = 0; i < selectedRows.length; i++){
recIds += selectedRows[i].Id + ',';
}
component.set('v.recordIds', recIds);
component.set('v.numOfRowsSelected', selectedRows.length);
}
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C