Skip to content

Instantly share code, notes, and snippets.

@loktevich
Created May 7, 2020 16:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loktevich/044d9411985c03a314765dbe05328eb3 to your computer and use it in GitHub Desktop.
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)
<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>
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>"
};
}
<template>
</template>
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
}
}
}
<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
Copy link

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?

@PetrKosicka
Copy link

NatalyaMurphyCS is right, it should be opposite way of wrapText false / true in richTextColumnType.html to provide correct functionalita when wrapText is enabled

@xormentor
Copy link

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.

@xormentor
Copy link

Sorry I answered my own question, I increased wrap-text-max-line to an amount that will solve the issue.

@HarishK501
Copy link

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