1

in this download column I am returning URL so that the user can view a document and download it. but when it does not return url the cenda remains blank and the idea is to add a message that says not available.

I have reviewed the documentation and I do not find anything similar.

enter image description here

hmtl

      <lightning:datatable class="slds-table slds-table_bordered" keyField="id" data="{!v.data}"
                columns="{!v.columns}" hideCheckboxColumn="true" onrowaction=""
                defaultSortDirection="{!v.defaultSortDirection}" sortedDirection="{!v.sortDirection}"
                sortedBy="{!v.sortedBy}" onsort="{!c.handleSort}" resizeColumnDisabled="true" />

Controller JS

    component.set('v.columns', [
        { label: 'Nombre del Documento', fieldName: 'nombreDoc', type: 'text', sortable: 'true' },
        { label: 'Fecha', fieldName: 'fecha', type: 'date', sortable: 'true' },
        {
            label: '', fieldName: 'descargas', type: 'url', 'iconName': 'utility:pdf_ext', initialWidth: 150,
            typeAttributes: {
                label: 'Ver documento',
                target: '_self'
            }
        }
    ]);

    var action = component.get('c.getDocumentData');
    action.setCallback(this, $A.getCallback(function (response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var data = response.getReturnValue();
            if (data != null && data.length > 0) {
                console.log(data);
                component.set("v.data", data);
                component.set("v.cantData", true);
            }
        } else if (state === "ERROR") {
            this.msjError(response);
            component.set("v.isError", true);
        }
        component.set("v.loading", true);
    }));
    $A.enqueueAction(action);

1 Answer 1

1

You can do this in the data supplied to the datatable.

You can specify two fields that supply data to the URL - a label and an href field. Like so:

{
   label: '', fieldName: 'YOUR_LABEL_FIELD', type: 'url', 'iconName': 'utility:pdf_ext', initialWidth: 150,
   typeAttributes: {
     fieldName: 'YOUR_HREF_FIELD',
     label: 'Ver documento',
     target: '_self'
   }
}

In your getDocumentData method, you can then check the href field (represented by YOUR_HREF_FIELD). If it's empty, set the href field to # and the label field to Not Available`.

Something like this:

if (data != null && data.length > 0) {

    let parsedData = [];
    data.forEach( document=> {
      if (!document['YOUR_HREF_FIELD']){
        document['YOUR_HREF_FIELD'] = '#';
        document['YOUR_LABEL_FIELD'] = 'Not Available';
      }
      else {
        document['YOUR_LABEL_FIELD'] = 'Click to Download';
      }
      parsedData.push(document);
    });

    component.set("v.data", parsedData);
    component.set("v.cantData", true);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .