Skip to content

Instantly share code, notes, and snippets.

@geekyakshay
Created June 29, 2020 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geekyakshay/241cbb7d4fad3b2842b232d1bd991c13 to your computer and use it in GitHub Desktop.
Save geekyakshay/241cbb7d4fad3b2842b232d1bd991c13 to your computer and use it in GitHub Desktop.
Custom Icons or Image as column in lightning-datatable LWC
.image
{
height: 1.5rem;
width: auto;
}
<template>
<template if:true={isImage}>
<p class="slds-truncate slds-p-horizontal_x-small">
<img src={getUrlValue} class="image" alt={getUrlValue}/>
</p>
</template>
<template if:true={isIcon}>
<span class="slds-truncate slds-p-horizontal_x-small">
<lightning-icon icon-name={getIconValue} alternative-text={getIconValue} size="small"></lightning-icon>
</span>
</template>
</template>
import { LightningElement, api } from 'lwc';
const validNameRe = /^([a-zA-Z]+):([a-zA-Z]\w*)$/;
const validIconCategory = ['standard','utility','custom','action','doctype'];
export default class CustomIconOutput extends LightningElement {
@api value = '';
get isIcon(){
return validNameRe.test(this.value.trim().toLowerCase()) && validIconCategory.includes(validNameRe.exec(this.value.trim())[1].toLowerCase());
}
get getIconValue(){
return this.value.toLowerCase();
}
get isImage(){
return !this.isIcon;
}
get getUrlValue(){
return this.value;
}
}
@protyushc
Copy link

Hi Akshay,
whenever i use any extended datatable, my header and columns values get misaligned. any idea how to resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment