Home Salesforce Show Image in Lightning Web Component Data Table

Show Image in Lightning Web Component Data Table

by Dhanik Lal Sahni

Showing image in data table is one of major requirements in projects. In this post, we will see how we can show image in Lightning Web Component Data Table.  In last blog Integrate Salesforce Stackexchange using Lightning Web Component and Apex I have created community to show Salesforce StackExchange user list. Let us add profile picture of user in same community.

By default datatable does not support image data type. We have to create custom data type for image in Lightning Web component.

Steps for creating custom data type

  1. Custom Control Creation
  2. Extend Lightning Data Table
  3. Use extended data table in Component

1. Custom Control Creation

Create custom control for showing profile pics.  For this we need to create a lightning web component with below code. We are using img control to show profile pic and source for this image will be passed at runtime using @api parameter.

2. Extend Lightning Data Table

We have to extend lightning data table to show image in table row. Create another Lightning Web Component named ‘salesforceCodexDataTable’ .  Create imageTableControl.html in same folder to use above created image control to show profile pics. This file is used as custom type while extending data table.

Folder structure for custom components:

3. Use extended data table in Component

After image custom data type is created for data table. Let us use this in component to show profile pics of Salesforce Stack Exchange User on community portal.

Here we have used image type to show profile pic in line no 3. fieldName: profile_image is having profile image url which is passed to image control as source url.

Reference:  https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation#creating-custom-data-types

You may also like

27 comments

sayanta October 13, 2019 - 7:38 pm

awesome post

Reply
loka April 6, 2020 - 12:32 am

Can you please show the complete implementation?

Reply
Dhanik Lal Sahni April 6, 2020 - 6:41 pm

Hello,

It is complete implementation. Few steps are written in blog https://salesforcecodex.com/2019/10/integrate-salesforce-stackexchange-using-lightning-web-component-and-apex/ and rest in this blog. Please try using implementing using both blogs. Let me know, if you find any issue.

Thank You,
Dhanik

Reply
neha April 4, 2020 - 4:41 pm

hey
can you show the apex side as well and in which cmp you added that?

Reply
Dhanik Lal Sahni April 6, 2020 - 6:38 pm

Hello Neha,

Apex detail is shown in blog https://salesforcecodex.com/2019/10/integrate-salesforce-stackexchange-using-lightning-web-component-and-apex/ which is mentioned in this blog as well. Please check that blog.

Let me know, if it is not working.

Thank You,
Dhanik

Reply
Charles Yach May 12, 2020 - 1:09 am

Hello Dhanik,

In imageTableControl.html you have the url attribute set to value, yet it looks like value is never defined in the js. How does this work? I ask because I notice you are hardcoding the alt-text as “Image Not Found” and I would like to know how to pass an alt-text through as well.

Thank you

Reply
Dhanik Lal Sahni May 12, 2020 - 7:31 am

Hello Charles,
Url values is passed from data table records in showProfile.js. Please check https://salesforcecodex.com/2020/04/extend-lightning-datatable-with-file-upload-element-in-lightning-web-component/ for passing Alt-text through parameter. You can use typeAttributes for this purpose.

Thank You,
Dhanik
https://salesforcecodex.com/

Reply
Dimple October 13, 2022 - 12:54 pm

I am also getting “Image not found”.Can you please help me out?

Reply
Dhanik Lal Sahni October 15, 2022 - 9:42 pm

Hello Dimple,

If you still facing issues, ping me on Linkedin and we will resolve your issue.

Thank You,
Dhanik

Reply
Extend datatable in Lightning Web Component | SalesforceCodex July 5, 2020 - 10:11 pm

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

Reply
Shiran July 6, 2020 - 12:41 pm

Great post Dhanik!
In this case how would you propagate click in imageControl component to showProfile component? It is useful for one of the projects i’m doing right now

Reply
Shiran July 6, 2020 - 12:48 pm

Great post Dhanik!
In this case how would you dispatch an onclick event from imageControl to showProfile component?

Reply
Dhanik Lal Sahni August 20, 2021 - 9:15 pm

Thank You Shiran,

You can add events for this. Checkout post https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.events_handling for this.

Thank You,
Dhanik

Reply
Hassan April 14, 2021 - 5:04 am

Hi folks,
Awesome article. Can we use this solution for lighting tree grid.

Reply
Dhanik Lal Sahni April 17, 2021 - 10:19 pm

Yes Hassan, As per documentation lighting-tree-grid is not supporting custom data type so you will not able to do this. You can try your custom tree grid control.

Thank You,
Dhanik

Reply
Dhanik Lal Sahni July 3, 2021 - 1:46 pm

Thank You Abhilash.

Regards
Dhanik

Reply
vishnu July 23, 2021 - 4:34 pm

Hi Dhanik,

We are getting following error,
LWC1011: Failed to resolve import “./imageTableControl.html” from “salesforceCodexDataTable.js”. Please add “imageTableControl.html” file to the component folder.
please help us to resolve this issue.

Thanks,
Vishnu

Reply
Dhanik Lal Sahni August 20, 2021 - 9:11 pm

Hello Vishnu,
imageTableControl.html should be in same folder as salesforceCodexDataTable is. If not then add proper path in importing it in SalesforceCodexDataTable js.

Thank You,Dhanik

Reply
Mounika August 14, 2021 - 5:15 pm

Hey dhanik,
I’ve a similar scenario of displaying picklist field on editable lightning-datatable. When i used the above approach picklist values are not displaying in edit mode . Can you please help

Reply
Dhanik Lal Sahni August 20, 2021 - 9:07 pm

Hello Mounika,

If issue still exist, we can connect in a meeting to see issues.

Thank You,
Dhanik

Reply
LWC Datatable with custom data type not rendering as supose December 25, 2021 - 1:20 pm Reply
LWC Datatable with custom data type not rendering as supose – GrindSkills April 2, 2022 - 3:39 pm Reply
Kari SaiMohan March 7, 2023 - 4:25 pm

Hi Dhanik in the place of image it is showing image link where i want to change

Reply
Dhanik Lal Sahni March 12, 2023 - 10:39 am

Hello Kari,
You have to see if js is loading properly or not. If the issue is not resolved, ping me on LinkedIn.

Thank You,
Dhanik

Reply
Dhanik Lal Sahni April 29, 2024 - 1:23 pm Reply

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.