Created
June 29, 2020 15:15
-
-
Save geekyakshay/241cbb7d4fad3b2842b232d1bd991c13 to your computer and use it in GitHub Desktop.
Custom Icons or Image as column in lightning-datatable LWC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.image | |
{ | |
height: 1.5rem; | |
width: auto; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Akshay,
whenever i use any extended datatable, my header and columns values get misaligned. any idea how to resolve the issue.