I am new to building a Lightning Web Component and I have what feels like a basic validation using the row's data for inline editing, but I have been unable to figure out. I have a simple table with a few columns which I am fetching via a simple Apex SOQL call. Here is the relevant code, which works fine minus the validation check.
Component
<lightning:datatable aura:id="reorderTable"
data="{! v.reorderList }"
columns="{! v.mycolumns }"
keyField="Id"
hideCheckboxColumn="true"
onsave="{!c.handleSave}" />
Column Setup
component.set('v.mycolumns', [
{label: 'Inventory Item', fieldName: 'ProductName', type: 'text'},
{label: 'Quantity Ordered', fieldName: 'Quantity__c', type: 'number'},
{label: 'Quantity Received', fieldName: 'QuantityReceived__c', type: 'number', editable: true},
{label: 'Notes', fieldName: 'Notes__c', type: 'text', wrapText: true, editable: true}
]);
Save Event Function
var updatedRecords = component.find( "reorderTable" ).get( "v.draftValues" );
for(var i = 0 ; i < updatedRecords.length;i++) {
var row = updatedRecords[i];
var notesError = false;
if(row.QuantityReceived__c < row.Quantity__c){
row.ShipmentStatus__c = 'Partial';
if(row.Notes__c == ''){
notesError = true;
}
}
else if (row.QuantityReceived__c > row.Quantity__c){
row.ShipmentStatus__c = 'Pending';
}
else{
row.ShipmentStatus__c = 'Closed';
}
}
So the issue is how do I get the value of an unedited column that should be used for comparison? In this case the field QuantityReceived__c is edited but the Quantity__c field is read only, but that isn't captured in the draftvalues. In a similar vein the Notes__c field is only required if the QuantityReceived__c value is less than the Quantity__c column so it may or may not be in the draftvalues. I realize I could do some of this in the Apex class that does the update, but I was hoping to find a way of not needing to do that.
Best Answer
The draftValues will always include the key-field (typically Id), so you can find the old values from the original data: