2

I am building an aura lightning:datatable.

For flexibility I am specifying each column definition in a custom metadata type record so that when a column needs to be added or removed, have its title changed or position changed etc, it can be done by amending custom metadata rather than requiring development and deployment.

It works well, except where the attribute value is an object, like typeAttributes, which I need to set for url columns.

If I retrieve the typeAttributes value from a custom metadata type field it has no effect on the display.

enter image description here

If I write the definition directly in my helper then I get the result that I want.

    const columnsTemp = [
        {label: 'First Name', fieldName: 'localPolicyId', type: 'text', sortable: true},
        {label: 'Website', fieldName: 'customer', type: 'url', sortable: false, typeAttributes: { label: { fieldName: 'customer' }, target: '_blank', tooltip: 'Click to visit website' }}
    ]

So it seems that retrieving the typeAttributes value as text doesn't work.

Any suggestions on how to specify the typeAttributes value in the custom metadata type record? The typeAttributes object itself contains an object for the label property so need to be able to handle that too.

1
  • 1
    I get the impression you are wanting the custom metadata instance to hold a JSON representation of an object, with the various properties like "label", "target" etc. Surely all you need to do is pass this as a string to the component and have the component use JSON.parse to convert it to the required object to add to your array of columns?.See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Phil W Feb 7 '20 at 15:13
1

Boom! Thanks @phil-w.

It works with this in my controller:

column.typeAttributes = JSON.parse(item.TypeAttributes__c);

and this is the value in my custom metadata type field:

{ "label": { "fieldName": "customer" }, "target": "_blank", "tooltip": "Click to visit website" }

Note to other readers - on the docs that Phil helpfully provided a link to, it states

"JSON.parse() does not allow single quotes"

I have verified that this is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.