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
Edgars Everts 5Edgars Everts 5 

How to inspect lightning:datatable getParam( <parameter> )?

Hi,

Story about lightning:datatable 

some component lines:
attributes:
<aura:attribute name="data" type="Object"/>
<aura:attribute name="rowSource" type="Object"/>

Table definition:
<lightning:datatable
                                aura:id="linesTable"
                                keyField="Id"
                                columns="{! v.columns }"
                                data="{! v.data }"
                                errors="{! v.errors }"
                                draftValues="{! v.draftValues }"
                                selectedRows="{! v.selectedRows }"
                                showRowNumberColumn="true"
                                onrowaction="{! c.openModel }"
                                onrowselection="{! c.countRows }"
                                onsave="{! c.handleSaveEdition }"
                                 />

and modal window ( to simulate dropdown list )
with picklist values:
<lightning:select aura:id="selectedStage" label="" name="selectStage" onchange="{!c.handleStageSelected}">
                                    <option value=""> </option>


on Closing modal window with save
<lightning:button variant="brand" 
                                              label="Save"
                                              title="Save"
                                              onclick="{!c.saveSegment}"/>


controller lines:
   openModel: function(component, event, helper) {
        component.set("v.isOpen", true);
        console.log("action: openModal => row: " + event.getParam('row'));
        component.set("v.rowSource", event.getParam('row'));        
    },

console output:
action: openModal => row: [object Object]
 
saveSegment: function(component, event, helper) { 
        // and set set the "isOpen" attribute to "False for close the model Box.
        component.set("v.isOpen", false);
        var vStageName = component.get("v.selectedStage");
        var cmRow = component.get("v.rowSource");
        console.log("action: saveSegment => row: " + cmRow);
        var cmRows = component.get("v.data");
        console.log("action: saveSegment => rows: " + cmRows);
        /** This method has the same algorithm as Array.prototype.indexOf(). TypedArray is one of the typed array types here. */
        var cmRowPosition = cmRows.indexOf(cmRow);
        console.log("action: saveSegment => rowIndex: " + cmRowPosition);

        cmRows[cmRowPosition].StageName = vStageName;
        component.set("v.draftValues", cmRows);
       
   },

Console output:
action: openModal => row: [object Object]
opportunityDataTableTest.js:44 action: saveSegment => row: [object Object]
opportunityDataTableTest.js:46 action: saveSegment => rows: [object Object],[object Object],[object Object],[object Object],[object Object]
opportunityDataTableTest.js:50 action: saveSegment => rowIndex: -1
Scenario:
Load Opportunitties with lightning datatable
OnRowaction - do open Modal window, here store current Row in component variable.
On Modal window picklist value change, store picklist value in component variable
On Modal window close by Save , get Component Row and Component Table, and find the Position of Row in Table
Then idea is set Row.fieldName = to Modal Window picklist value 
And on Save Table, should Update Opportunities accordingly.

Have to admit that this "solution" works in developer edition, but have challenge to transfer code to "work-sandbox".
Copy paste 1:1, but in "work-sanbox" it doesn't work.

And page fails with the following:
This page has an error. You might just need to refresh it. Action failed: c:opportunityDataTableTest$controller$saveSegment [Cannot set property 'StageName' of undefined] Failing descriptor: {c:opportunityDataTableTest$controller$saveSegment}

Question:?
In code examples on RowActions there is variable row, which is get by event.getParam(<param>).
e.g. 
component.set("v.rowSource", event.getParam('row'));

Honestly not clear where this 'row' parameter comes from as per lightning:datatable there is no such parameter. Any ideas?

How to ensure what parameters can be used in event.getParam( ) function?

Seems that Object position cannot be found by "row" , and indexOf returns -1. / but in developer org, the "same" code works/

Any ideas how to debug this besides console.log?
Is it possible to compare the data types of row [Object] and v.data[index][Object]?

Looking forward for your advice!
Thank you in advance.

Kind regards,
​​​​​​​Edgars
Best Answer chosen by Edgars Everts 5
Edgars Everts 5Edgars Everts 5
So far did not figure out what went wrong in work sandbox, but have "found a work-around".
Thanks goes to Aleksejs J. who did porpose to use any other method to calculate rowIndex value.
//var rowIndex = rows.indexOf(row);
/** find the index by any other way... */
   var rowIndex = -1;
   for ( let i = 0; i < rows.length ; i++ ){
       if (row["Id"] === rows[i]["Id"]) {
           rowIndex = i;
           break;
       }
   };

That's all the magic. Not sure if this is according to best practice, but it works.
Enjoy IT.

 

All Answers

Edgars Everts 5Edgars Everts 5
quick follow-up:
and here is the console output from developer edition:
action: openModal => row: [object Object]
expensesDataTable.js:43 action: openModal => rows: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
expensesDataTable.js:44 action: openModal => rowIndex: 3

Where custom object Expense__c is used instead of Opportunities ...
and component has the same attribute definition ( type = Object )
<aura:attribute name="data" type="Object"/>
<aura:attribute name="rowSource" type="Object"/>

 
Edgars Everts 5Edgars Everts 5
Ok, this question is answered by console.log help
"Honestly not clear where this 'row' parameter comes from as per lightning:datatable there is no such parameter. Any ideas?
How to ensure what parameters can be used in event.getParam( ) function?"
 
var peventDetails = event.getParams();
console.log("action: openModal => eventDetails object: " + JSON.stringify(peventDetails));

console output: 
action: openModal => eventDetails object: {"row":{

So datatable:lightning onrowaction event has at least "row" Parameter. Enjoy It.
Edgars Everts 5Edgars Everts 5
So far did not figure out what went wrong in work sandbox, but have "found a work-around".
Thanks goes to Aleksejs J. who did porpose to use any other method to calculate rowIndex value.
//var rowIndex = rows.indexOf(row);
/** find the index by any other way... */
   var rowIndex = -1;
   for ( let i = 0; i < rows.length ; i++ ){
       if (row["Id"] === rows[i]["Id"]) {
           rowIndex = i;
           break;
       }
   };

That's all the magic. Not sure if this is according to best practice, but it works.
Enjoy IT.

 
This was selected as the best answer