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
Karl Houseknecht 9Karl Houseknecht 9 

lightning:datatable text column will not display currency or boolean fields as text

I am using a lightning:datatable to display the results of a dynamic SOQL query.  The columns are generated at runtime from those specified in the query, and default to type: "text".   Boolean or currency fields do not display unless you specifically set the type appropriately.  This is a problem because it is difficult in many cases to do a describe and get type information beforehand, particularly when querying fields from related objects.

Any insights?

 
Karl Houseknecht 9Karl Houseknecht 9
I created a workaround, but it's not ideal.  I'm hacking the returned data using JSON.stringify and regular expressions with replace.  I had to do this anyway to upperCase all of the property names to do case insensitive assignment for the columns.  Here is code:
 
var data = response.getReturnValue();

//convert the data's properties to uppercase - matches a property like "<property>":
var dataString = JSON.stringify(data).replace(/\W\w+\W\:/g, function(v) { return v.toUpperCase(); });

//non-text data won't show up in the datatable's text column.
//So surround any non-text data with quotes in the source. 
//The regex captures ":<data> so strip off the leading quote and colon and put them back with quotes around the //data
dataString = dataString.replace(/\u0022\:\w+/g, function(v) {v = v.replace('"', ""); v = v.replace(":", ""); return '":"' + v + '"'; });
                
var upperData = JSON.parse(dataString); //then assign to the data attribute of the datatable

It's ugly but it works.  Shouldn't have to do this.  Hopefully there's a fix in a later release.
Razia Khan 7Razia Khan 7
@Karl Houseknecht 9:
I was able to use currency as field.
You can use data type as 'currency' and it will work for you.Let me know if you still face issue here.Please mark it as best answer if its helpful.User-added image
 
Karl Houseknecht 9Karl Houseknecht 9
Yes, you can assign the datatype of the column as currency, that's the whole point of my original post.  Read it carefully again to understand the problem.
Manjunatha V PManjunatha V P
From the returned soql results, before assigning it to the data of the datatable, get any element in the array of returned soql results, get the property names of the object in an array. While iterating through the property names get the type of each property and assign it to the coulmn type. Please see the below code

var names= Object.getOwnPropertyNames(returnedResults[0]);
var options = [];
for(var item in names){
var individualPropertyNameObject = {};// Format to be fed to columns of datatable
individualPropertyNameObject.label = returnedResults[0][names[item]];
individualPropertyNameObject.fieldName = names[item];
console.log('type of ' + typeof returnedResults[0][names[item]]);
individualPropertyNameObject.type = typeof returnedResults[0][names[item]] ;
options.push(individualPropertyNameObject);
}
component.set('v.columns',options);
Well there are issues with this method, sometimes it can't detect date as date, but it doesn't return null, anyway it treats them as number and displays. You don't get empty columns