[SalesForce] lightning:dataTable in a multi-currency org

Trying to figure out how to handle records with multiple currency. The dataTable definitions are for the column. I do not see anywhere in the documentation where the currency code of an individual row can be specified dynamically based off the currency code field (either custom or standard)

the documentation for a formattedNumber component used by the dataTable states

"The locale set in the app's user preferences determines how numbers are formatted."

So if the row has a different currency it will be displayed with the wrong symbol AND the amount will be incorrect.

I.e. if the amount is CAD200.00 it is displayed to the user as $200.00

No code to show as I cannot see a way to do it currently

Has anyone been able to use the dataTable to display rows with differing currencies?

I tested in an org with multi-currency enabled and it did not show the correct symbol or currency despite the record being set to a different currency.

Would appreciate any links explaining? Maybe I am missing something small or a setting somewhere to allow the table to show differing currencies in the rows…

Here is what I came up with (Open to other ideas)

I am sure there are better ways but in a pinch….

Once I get the data back from the server I iterate over it. It contains an Amount field and a currency code field. Since I cannot seem to display the correct amount in the dataTable I am just going to display as an example USD222.00

Problem is that number format if 2222.00 shows as 2222. So I need to add back in the commas and decimals. 1.10 would show as 1.1 and 1234.20 would show as 1234.2 so

for(var x=0;x<data.length;x++){
    //Despite setting the min max precision, localization does not return any trailing zeros
    var formattedAsString =  $A.localizationService.formatNumber(data[x].ns__Amount__c,2,2);
    //get the current precision value adding in trailing zeros . If none set to .00
    var precisionValue = formattedAsString.indexOf('.') === -1 ? '.00' : formattedAsString.substring(formattedAsString.indexOf('.')).concat('00').substring(0,3);

    //Combine it all back together replacing the precision    
    formattedAsString = formattedAsString.substring(0,formattedAsString.indexOf('.') === -1 ? formattedAsString.length : formattedAsString.indexOf('.')).concat(precisionValue);

    //Set the value of the field to the text representation in local format (sans decimal if local is comma, oh well)    
    data[x].ns__Amount__c =    
       data[x].ns__Currency_Code__c.concat(formattedAsString);
}

Not pretty but the following is what shows

Original Value => After localization => final output

USD 1234.00 => 1,234 => USD1,234.00

CAD 234.10 => 234.1 => CAD 234.10

Despite using the min max precision in the localization it drops all precision if the value is a 0 🙁

Note I am not sending the data back to the server so populating a number field with text in this case will not have any ill effects. If I had to send it back to the server that would be a pain…

Best Answer

You can dynamically specify the currency code in this manner:

{
    label: "Amount", 
    fieldName: "Amount", 
    type: "currency", 
    typeAttributes: { currencyCode: { fieldName: 'CurrencyIsoCode' }}
}

I've put a basic example in an LWC playground but it works the same in Aura.