You need to sign in to do that
Don't have an account?
Jessica Tejeiro
lightning:datatable Unable to pass selectedrows value to server side controller
Hi! I am working on a project to load account information per criteria in a data table and allow users to select which they want to create a new record for.
However I am unable to pass the rows selected by the user to the controller. the value is returning as undefined or null. Please help!
Here is my code:
COMPONENT:
<aura:component controller = "JMA_SearchforAllChildrenAccounts_Ctr" implements="lightning:availableForFlowActions,force:hasRecordId,lightning:availableForFlowScreens,force:lightningQuickActionWithoutHeader" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="recordId" type="String" access="global"/>
<aura:attribute name="AccountList" type="Account[]"/>
<aura:attribute name="childrenAccount" type="Account[]" access="global" />
<aura:attribute name="data" type="Object" />
<aura:attribute name="SelectedAccount" type="Account[]" access = "global"/>
<aura:attribute name="columns" type="List"/>
<div class="slds-page-header">
<div class="slds-page-header__row">
<div class="slds-page-header__name-title">
<h1>
<span class="slds-page-header__title slds-truncate" title="Rohde Corp - 80,000 Widgets">Select Accounts for Deal Proposal</span>
</h1>
</div>
</div>
<p class="slds-page-header__name-meta"> Selected Account</p>
</div>
<lightning:datatable
data="{!v.data}"
columns="{!v.columns}"
keyField="id"
selectedRows="{! v.selectedRows }"
maxRowSelection="{! v.maxRowSelection }"
onrowselection="{! c.updateSelectedText }" />
<br/>
<center>
<lightning:button variant="success" label="Create Deal Proposal" title="Create Deal Proposal" onclick="{!c.handleSelect}"/>
</center>
</aura:component>
CONTROLLER:
({
doInit : function(component, event, helper) {
var recordId = component.get('v.recordId');
var action = component.get("c.PullChildrenAccounts");
action.setParams({
'recordId': recordId,
'strObjectName' : 'Account',
'strFieldSetName' : 'JMA_Deal_Proposal_Lightning_Component'
});
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
component.set("v.data", response.getReturnValue().lstDataTableData);
component.set("v.columns", response.getReturnValue().lstDataTableColumns);
}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");
}
}else{
console.log('Something went wrong, Please check with your admin');
}
});
$A.enqueueAction(action);
},
updateSelectedText : function(component, event, helper){
var selectedRows = event.getParam('selectedRows');
console.log('selectedRows'+selectedRows);
component.set("v.selectedRowsCount" ,selectedRows.length );
var rowCount = component.get("v.selectedRowsCount");
console.log('rowCount'+rowCount);
let obj =[] ;
for (var i = 0; i < selectedRows.length; i++){
obj.push({Name:selectedRows[i].Name});
}
component.set("v.selectedRowsDetails" ,JSON.stringify(obj) );
component.set("v.selectedRowsList" ,event.getParam('selectedRows') );
console.log('obj: '+obj);
},
handleSelect: function (component, event, helper) {
var arr = component.get('v.data');
var obj = component.get("v.selectedRowsList");
var rowCount = component.get("v.selectedRowsCount");
console.log('rowCount'+rowCount);
console.log('obj '+JSON.stringify(obj) );
var updateAction = component.get("c.SelectedAccounts");
updateAction.setParams({ AccountList: obj});
updateAction.setCallback(this, function(a) {
$A.get('e.force:refreshView').fire();
});
$A.enqueueAction(updateAction);
},
})
However I am unable to pass the rows selected by the user to the controller. the value is returning as undefined or null. Please help!
Here is my code:
COMPONENT:
<aura:component controller = "JMA_SearchforAllChildrenAccounts_Ctr" implements="lightning:availableForFlowActions,force:hasRecordId,lightning:availableForFlowScreens,force:lightningQuickActionWithoutHeader" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="recordId" type="String" access="global"/>
<aura:attribute name="AccountList" type="Account[]"/>
<aura:attribute name="childrenAccount" type="Account[]" access="global" />
<aura:attribute name="data" type="Object" />
<aura:attribute name="SelectedAccount" type="Account[]" access = "global"/>
<aura:attribute name="columns" type="List"/>
<div class="slds-page-header">
<div class="slds-page-header__row">
<div class="slds-page-header__name-title">
<h1>
<span class="slds-page-header__title slds-truncate" title="Rohde Corp - 80,000 Widgets">Select Accounts for Deal Proposal</span>
</h1>
</div>
</div>
<p class="slds-page-header__name-meta"> Selected Account</p>
</div>
<lightning:datatable
data="{!v.data}"
columns="{!v.columns}"
keyField="id"
selectedRows="{! v.selectedRows }"
maxRowSelection="{! v.maxRowSelection }"
onrowselection="{! c.updateSelectedText }" />
<br/>
<center>
<lightning:button variant="success" label="Create Deal Proposal" title="Create Deal Proposal" onclick="{!c.handleSelect}"/>
</center>
</aura:component>
CONTROLLER:
({
doInit : function(component, event, helper) {
var recordId = component.get('v.recordId');
var action = component.get("c.PullChildrenAccounts");
action.setParams({
'recordId': recordId,
'strObjectName' : 'Account',
'strFieldSetName' : 'JMA_Deal_Proposal_Lightning_Component'
});
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
component.set("v.data", response.getReturnValue().lstDataTableData);
component.set("v.columns", response.getReturnValue().lstDataTableColumns);
}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");
}
}else{
console.log('Something went wrong, Please check with your admin');
}
});
$A.enqueueAction(action);
},
updateSelectedText : function(component, event, helper){
var selectedRows = event.getParam('selectedRows');
console.log('selectedRows'+selectedRows);
component.set("v.selectedRowsCount" ,selectedRows.length );
var rowCount = component.get("v.selectedRowsCount");
console.log('rowCount'+rowCount);
let obj =[] ;
for (var i = 0; i < selectedRows.length; i++){
obj.push({Name:selectedRows[i].Name});
}
component.set("v.selectedRowsDetails" ,JSON.stringify(obj) );
component.set("v.selectedRowsList" ,event.getParam('selectedRows') );
console.log('obj: '+obj);
},
handleSelect: function (component, event, helper) {
var arr = component.get('v.data');
var obj = component.get("v.selectedRowsList");
var rowCount = component.get("v.selectedRowsCount");
console.log('rowCount'+rowCount);
console.log('obj '+JSON.stringify(obj) );
var updateAction = component.get("c.SelectedAccounts");
updateAction.setParams({ AccountList: obj});
updateAction.setCallback(this, function(a) {
$A.get('e.force:refreshView').fire();
});
$A.enqueueAction(updateAction);
},
})
The attribute selectedRowsList itself is not defined ( nor selectedRowsDetails ).
<aura:attribute name="selectedRowsDetails" type="String"/>
<aura:attribute name="selectedRowsList " type="List"/>