Created
May 7, 2020 16:40
-
-
Save loktevich/044d9411985c03a314765dbe05328eb3 to your computer and use it in GitHub Desktop.
LWC rich text column type for lightning-datatable (with standard text wrapping support)
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> | |
<!-- A custom component instead of lightning-datatable --> | |
<c-my-custom-datatable | |
key-field="textCol" | |
columns={columns} | |
data={tableData} | |
wrap-text-max-lines="3"> | |
</c-my-custom-datatable> | |
</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 } from "lwc"; | |
/** | |
* A component that uses new datatable | |
*/ | |
export default class cmpWithDatatable extends LightningElement { | |
columns = [ | |
// standard text column | |
{ label: "Text Column", fieldName: "textCol", wrapText: true }, | |
// custom richText column | |
{ label: "Rich Text", fieldName: "richTextCol", type: "richText", wrapText: true } | |
]; | |
tableData = { | |
textCol: "Text", | |
richTextCol: "<h3>Rich Text</h3>" | |
}; | |
} |
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> |
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 LightningDatatable from "lightning/datatable"; | |
import richTextColumnType from "./richTextColumnType.html"; | |
/** | |
* Custom component that extends LightningDatatable | |
* and adds a new column type | |
*/ | |
export default class MyCustomDatatable extends LightningDatatable { | |
static customTypes={ | |
// custom type definition | |
richText: { | |
template: richTextColumnType, | |
standardCellLayout: true | |
} | |
} | |
} |
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> | |
<!-- a template for cell. File in the myCustomDatatable directory --> | |
<template if:true={wrapText}> | |
<lightning-formatted-rich-text value={value} class="slds-truncate"> | |
</lightning-formatted-rich-text> | |
</template> | |
<template if:false={wrapText}> | |
<lightning-formatted-rich-text value={value}> | |
</lightning-formatted-rich-text> | |
</template> | |
</template> |
NatalyaMurphyCS is right, it should be opposite way of wrapText false / true in richTextColumnType.html to provide correct functionalita when wrapText is enabled
Is it possible to have wrap text and show the full content? It always truncates it with "..." when wraptext = true, I am trying to show everything to stop users digging into each record, either that or provide the full text in a hover if its truncated would also be fine.
Sorry I answered my own question, I increased wrap-text-max-line
to an amount that will solve the issue.
Use this code for the richTextColumnType.html
file, to clip the text in one line, even when the rich text field has multi-line text value
<template>
<!-- a template for cell. File in the myCustomDatatable directory -->
<template if:true={wrapText}>
<lightning-formatted-rich-text value={value}>
</lightning-formatted-rich-text>
</template>
<template if:false={wrapText}>
<lightning-formatted-rich-text value={value} class="slds-hyphenate slds-line-clamp" style="--lwc-lineClamp: 1">
</lightning-formatted-rich-text>
</template>
</template>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shouldn't the slds-truncate class be used when wrapText = FALSE? ie if we're clipping it, then we want to truncate text. Othewise we want to show the whole thing, right?