You need to sign in to do that
Don't have an account?
SFDC Forum 9
Lightning Data table onrowselection issue
I have created data table, as below. I am getting selectedRows is [object Object] in console when i select any row.
i supposed to get array of selected row.I guess i have done some mistake in declaration of attributes (columns, data).Please clarify asap.
Component:::
<aura:component controller="ContactTopTen" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="columns" type="List" />
<aura:attribute name="data" type="Object" />
<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<lightning:datatable aura:id="contactTable"
keyField="id"
data="{!v.data}"
columns="{!v.columns}"
hideCheckboxColumn="false"
onrowselection="{!c.doSelect}" />
</aura:component>
Controller:
({
init : function(component, event, helper) {
component.set('v.columns',[
{label: 'Account Name', fieldName: 'Name', type: 'text', sortable: true},
{label: 'Rating', fieldName: 'Rating', type: 'text', sortable: true},
]);
var action=component.get('c.getCon');
action.setCallback(this, function(response){
var state=response.getState();
if(state==='SUCCESS' || state==='DRAFT')
{
var responseValue=response.getReturnValue();
component.set('v.data', responseValue);
}
});
$A.enqueueAction(action,false);
},
doSelect:function(component, event, helper) {
alert('Success');
var selectedRows=event.getParam('selectedRows');
console.log('selectedRows is : ' +selectedRows);
}
})
Apex class:
public class ContactTopTen {
@AuraEnabled
public static List<Account> getCon(){
List<Account> con=[select Name,Id, Rating from Account Limit 5];
return con;
}
}
i supposed to get array of selected row.I guess i have done some mistake in declaration of attributes (columns, data).Please clarify asap.
Component:::
<aura:component controller="ContactTopTen" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="columns" type="List" />
<aura:attribute name="data" type="Object" />
<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<lightning:datatable aura:id="contactTable"
keyField="id"
data="{!v.data}"
columns="{!v.columns}"
hideCheckboxColumn="false"
onrowselection="{!c.doSelect}" />
</aura:component>
Controller:
({
init : function(component, event, helper) {
component.set('v.columns',[
{label: 'Account Name', fieldName: 'Name', type: 'text', sortable: true},
{label: 'Rating', fieldName: 'Rating', type: 'text', sortable: true},
]);
var action=component.get('c.getCon');
action.setCallback(this, function(response){
var state=response.getState();
if(state==='SUCCESS' || state==='DRAFT')
{
var responseValue=response.getReturnValue();
component.set('v.data', responseValue);
}
});
$A.enqueueAction(action,false);
},
doSelect:function(component, event, helper) {
alert('Success');
var selectedRows=event.getParam('selectedRows');
console.log('selectedRows is : ' +selectedRows);
}
})
Apex class:
public class ContactTopTen {
@AuraEnabled
public static List<Account> getCon(){
List<Account> con=[select Name,Id, Rating from Account Limit 5];
return con;
}
}
If this answer helps you, please mark it as accepted.
All Answers
Follow the below steps to get the values.
1)
2)
if this answers your question mark it as best solution
Regards,
Ram Chand Heerekar
I have created one component for this which do all work for you: Lightning DataTable: Lazy Loading with Inline Editing and Actions (https://newstechnologystuff.com/2019/01/01/lightning-datatable-lazy-loading-with-inline-editing-and-actions/)
If this answer helps you, please mark it as accepted.
Regards,
Tushar Sharma
This will throw a compile time Error you can't use that variable directly with using "+"
Now I will have to access couple of feild values from selectedRows and pass it to another apex method.How can I access them.
If this answer helps you, please mark it as accepted.
As soon as I click on any order in list, page should get refreshed and update order deatils in detiled section page.(I have controller for this as I have code for this entire process in classic vf page and rewriting it in lightning component)
All I need is, on row seletion, will have to pass Index of the row and two more field values to the apex method to get this process done.
But i am not able to retrieve those paaramenter values into my 'onrowselection' function.
however Ia am able to get all these in my 'onrowaction' js function.
rowActionfunction::
var action = event.getParam('action');
var row = event.getParam('row');
var rows = component.get('v.data');
var rowIndex = rows.indexOf(row);
console.log('Index rowsss is' +rowIndex);
console.log('Ins isss : ' +rows[rowIndex].order_ID);
The details I have consoled above , I need to get it under onrowselection function.please assist.
Thank you in advance