Yes, Conditional Formatting can be done using cellAttributes for data table
For Example, If the requirement is to format/Show all the names of users in Red whose "Due Balance" is greater than 200 and other Usernames in Green the component would be
The working example can be viewed in playground
Code would be:
.HTML
<template>
<div style="height: 300px;">
<lightning-datatable
key-field="id"
data={data}
columns={columns}>
</lightning-datatable>
</div>
</template>
.JS
import {
LightningElement,
track
} from 'lwc';
const columns = [{
label: 'Label',
fieldName: 'name',
cellAttributes: {
class: {
fieldName: `format`
},
alignment: `left`
}
},
{
label: 'Phone',
fieldName: 'phone',
type: 'phone'
},
{
label: 'Balance',
fieldName: 'amount',
type: 'currency'
}
];
const recordMetadata = {
name: 'name',
email: 'email',
website: 'url',
amount: 'currency',
phone: 'phoneNumber',
closeAt: 'dateInFuture',
};
let amountOfRecords = 10;
export default class FormatTable extends LightningElement {
@track data = [];
@track columns = columns;
async connectedCallback() {
const data = await this.fetchDataHelper({
amountOfRecords
});
//Generate Dynamic Values
data.forEach(ele => {
ele.format = ele.amount > 200 ? 'slds-text-color_error' : 'slds-text-color_success';
});
this.data = data;
}
fetchDataHelper({ amountOfRecords }) {
const recordMetadata = {
name: "name",
email: "email",
website: "url",
amount: "currency",
phone: "phoneNumber",
closeAt: "dateInFuture"
};
return fetch("https://data-faker.herokuapp.com/collection", {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify({
amountOfRecords,
recordMetadata
})
}).then(response => response.json());
}
}
Similarly, additional styles can be included as desired and additional icons can also be included
Additionally, more styles can be found in another example in playground
to include additional styles platform-resource-loader can be used
You would need to patch in a field "isNotActive" in your data so you can use the opposing value. There's no way to do otherwise with buttons.
If you use the action column instead, you can conditionally determine what to display. This is actually in the documentation:
// Other column data here
{ type: 'action', typeAttributes: { rowActions: this.getRowActions } },
...
getRowActions(row, doneCallback) {
const actions = [];
if (row['isActive']) {
actions.push({
'label': 'Deactivate',
'iconName': 'utility:block_visitor',
'name': 'deactivate'
});
} else {
actions.push({
'label': 'Activate',
'iconName': 'utility:adduser',
'name': 'activate'
});
}
// simulate a trip to the server
setTimeout(() => {
doneCallback(actions);
}), 200);
}
You can do the callback immediately instead of simulating a callout, or simply return the value to show. This is the preferred way of doing this in Lightning.
Best Answer
Here's a solution using an attribute for the editable value
You can conditionally edit by doing the following in the LWC datatable:
In your table columns define editable as such
In your data include the controlling field like
You can manipulate the controlling field via JS or in your Apex logic.
Please note: You can debug this feature using the Chrome Developer Tools and enabling your Salesforce Sandbox user for Debug Mode. You can then open datatable.js to look under the hood at how this resolves. See the function isCellEditable.