Custom Lightning Web Component (LWC) that dynamically disables inline editing based on the status field of each record.
This project demonstrates how to create a Lightning Web Component (LWC) that displays a data table with conditional inline editing based on the status of each record. Specifically, inline editing is disabled for rows where the status is "processed".
force-app │ └───main └───default └───lwc │ └───myDataTable │ │ myDataTable.html │ │ myDataTable.js │ │ myDataTable.js-meta.xml │ │ myDataTable.css │ └───customCellRenderer │ │ customCellRenderer.html │ │ customCellRenderer.js │ │ customCellRenderer.js-meta.xml └───classes │ MyApexController.cls │ MyApexController.cls-meta.xml
This component is responsible for displaying the data table and handling the logic to disable inline editing based on the status field.
- myDataTable.html: Defines the structure of the data table.
- myDataTable.js: Contains the logic to fetch data and set the inline editing dynamically.
- myDataTable.js-meta.xml: Metadata configuration for the Lightning Web Component.
- myDataTable.css: Custom CSS to handle any styles, including hiding certain elements.
A custom cell renderer used to conditionally render the editable input fields.
- customCellRenderer.html: Template for rendering editable or non-editable cells.
- customCellRenderer.js: Logic to determine if a cell should be editable.
- customCellRenderer.js-meta.xml: Metadata configuration for the custom cell renderer component.
The Apex controller fetches the records to be displayed in the data table.
- MyApexController.cls: Contains the Apex method to fetch records from Salesforce.
- MyApexController.cls-meta.xml: Metadata configuration for the Apex class.
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Authorize your Salesforce org and provide the alias
my-org
:sfdx force:auth:web:login -a my-org
-
Deploy the Apex controller to your Salesforce org:
sfdx force:source:deploy -p force-app/main/default/classes
-
Deploy the Lightning Web Component to your Salesforce org:
sfdx force:source:deploy -p force-app/main/default/lwc
-
Assign the LWC to a Lightning page:
- Navigate to the Lightning App Builder in Salesforce.
- Select or create a Lightning page (e.g., App Page, Record Page, Home Page).
- Drag and drop the
myDataTable
component onto the page. - Save and activate the page.
Once deployed and added to a Lightning page, the data table will display records with conditional inline editing. Rows where the status is "processed" will not be editable.
- Modify Columns: Update the
COLUMNS
constant inmyDataTable.js
to include or exclude fields as needed. - Fetch Different Records: Update the SOQL query in
MyApexController.cls
to fetch different records or fields based on your requirements.
Feel free to fork this repository and make any improvements or customizations. Pull requests are welcome!
This project is licensed under the MIT License