Home Salesforce Extend lightning-datatable with file upload element in Lightning Web Component

Extend lightning-datatable with file upload element in Lightning Web Component

by Dhanik Lal Sahni

Recently we got a requirement to show list of account for getting status of uploaded document. If document is already uploaded  then list will show ‘No action required’ otherwise it will show file upload control.

As per requirement we have to show list or table of records so we can extend standard lightning-datatable with conditional file upload control. Let us how we can extend lightning-datatable with file upload control.

There is another blog https://salesforcecodex.com/2019/10/show-image-in-lightning-web-component-data-table/ which show how to add image control in lightning-datatable.

We can use similar concept to add file upload control in lightning data table.

Steps for extending  lightning-datatable

  1. Create component which show lightning-file-upload control
  2. Extend lightning-datatable with above component
  3. Use extended lightning-datatable to show requirement

1. Create component which show lightning-file-upload control

create LWC component which will show conditional lightning file upload. This condition is defined as @api property and its value is passed from lightning-datatable record. Basically we need to show file upload control for account which has not uploaded document. So we need current record’s document status.

2. Extend lightning-datatable with above component

Let us extend lightning-datatable so that it will add file upload control in one of column.  For extending any control we can use below code

FileUploadDataTable extends LightningDatatable

See comment in below code where we have created custom type from LightningDatatable.

 static customTypes = {   //it show that we are creating custom type
        fileUpload: {  // type of custom element
            template: documentUploadRender, // Which html will render
            typeAttributes: ['acceptedFileFormats','fileUploaded']  // attribute of custom type
        }
    };

3. Use extended lightning-datatable to show requirement

We have created new type by extending lightning-datatable. Let us see how we can use that component. For showing list in data table we have fetched account list.

public class AccountController{
    @AuraEnabled
    public static List getPendingAccounts() {
        return [SELECT Id,Name, AccountNumber,IsDocumentComplete__c from Account];
    }
}

We have custom element fileUpload in lightning-datatable.  We can add attribute which we have created in above step. We have to set value in fileUploaded attribute of custom type. we can set value like fileUploaded:{fieldName: ‘IsDocumentComplete__c’} in column list.

{ label: 'Documents Upload', type: 'fileUpload', fieldName: 'Id', typeAttributes: { acceptedFileFormats: '.jpg,.jpeg,.pdf,.png',fileUploaded:{fieldName: 'IsDocumentComplete__c'} } }

Let us show these records in new data table.

Demo Page:

Reference:

https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example

http://salesforcecodex.com/2019/10/show-image-in-lightning-web-component-data-table/

Related Posts:

Generic DataTable in Lightning Web Component

Single Row Selection in Lightning Datatable

Update:

Folder Structure for these components:

SalesforceCodex file upload folder structure

You may also like

19 comments

Shubham June 21, 2020 - 2:06 pm

When we are using multiple columns in this data-table, the CSS is not auto handled. Header column size varies with content of the column. Can you please suggest how to control the CSS for the same.

Reply
Vignesh July 5, 2020 - 2:23 pm

HI,
This issue coming while deploying file upload component.
force-app\main\default\lwc\fileUploadDataTable\fileUploadDataTable.js LWC1011: Failed to resolve import “./documentUploadRender.html” from “fileUploadDataTable.js”. Please add “documentUploadRender.html” file to the component folder.

Reply
Dhanik Lal Sahni July 5, 2020 - 9:53 pm

Hello Vignesh,
You have to add documentUploadRender.html in fileUploadDataTable lwc component folder.

Please try this. It will work.

Thank You,
Dhanik

Reply
Dhanik Lal Sahni July 5, 2020 - 10:11 pm

I have also updated blog to show folder structure of all components.

Thank You,
Dhanik

Reply
Nathan July 29, 2020 - 12:50 am

Hi Vignesh,

This LWC1101 error happened to me and I ended up on this blog post. There seems to be an issue if the template filename (in your case ./documentUploadRender.html’) has more than one capital letter in camelcase. I ended up renaming my template to use all lowercase and it finally worked, for example ./progresstemplate.html – hope this helps. I spent almost an hour trying to call it progressBarTemplate.html. I will note that I think this will work with one uppercase letter, but not two. I couldn’t find any documentation on this issue so I wanted to add a comment on the top result.

Reply
Dhanik Lal Sahni July 31, 2020 - 9:43 pm

Thank You Nathan for sharing info.

Regards
Dhanik

Reply
Venkatesh Ravichandran September 5, 2020 - 12:23 pm

Hi
I have extended LWC datatable with a custom picklist column. It’s designed more like the above code. Only problem is that it is value update is uni direction. value changed in the picklist can be updated to the parent wrapper component’s this.dataRows using event. But if I change the this.dataRows in parent wrapper it’s not reflected in the extended picklist component even after doing this “this.dataRows = […this.dataRows]”.

Reply
Dhanik Lal Sahni September 5, 2020 - 1:07 pm

Hello Venkatesh,

Is dataRows variable is track property? Try using that. If it is not working, Let us connect to resolve your issue.

Thank You,
Dhanik

Reply
Tamanna Chettiyar June 30, 2022 - 5:55 pm

Hi, was your issue resolved, I am also facing similiar issue. Can you help me to know how the issue was resolved?
Thanks

Reply
Daniel August 26, 2021 - 1:20 am

I have a JSON and in this JSON I have a field that brings information in base64, I receive this value in a LWC, I need to transform this base64 value into an image according to the line it is in the datatable, can you help me with that?

Reply
Dhanik Lal Sahni August 30, 2021 - 10:38 pm

Hello Daniel,
Please check this link for converting base64 to image https://stackoverflow.com/questions/21227078/convert-base64-to-image-in-javascript-jquery

I have also used same in another blogs View Files in Salesforce Lightning Community Portal and Capture Screen using Lightning Component

Thank You,
Dhanik

Reply
VIVEK March 3, 2022 - 9:03 pm

is there a way to do sorting on this ?

Reply
Dhanik Lal Sahni March 4, 2022 - 7:39 pm

Yes Vivek, It can be done. You can see references from other blog Generic DataTable in Lightning Web Component

Thank You,
Dhanik

Reply
Sai Mohan December 2, 2022 - 5:31 pm

how to filter to display only parent related child records with this function

Reply
Dhanik Lal Sahni December 7, 2022 - 8:08 pm

Hello Sai,

You can try filtering in your apex or js.

Thank You,
Dhanik

Reply
Data Table in Screen Flow - SalesforceCodex June 12, 2023 - 9:23 am

[…] Extend lightning-datatable with file upload element in Lightning Web Component […]

Reply
Krish September 10, 2023 - 11:56 am

Hi In this method is it possible to Change The Icons Dynamically Based on Rows Value

Reply
Dhanik Lal Sahni September 30, 2023 - 9:43 pm

Yes Krish. It can be done.

Thank You,
Dhanik

Reply

Leave a Comment