The Lightning Web Component(LWC) framework is very popular because it provides better performance to your applications, it adheres to web standards, it is easy to learn, and so much more. Salesforce provides out-of-the-box components, called base LWC. In this post, I will go deep into the lightning-datatable base component.
This component is great for displaying data in a table format, but it can do SO MUCH MORE. It has great features like inline editing, infinite scrolling, column level actions, and row level actions. You can also create your custom data type if the component does't support your use case. The cool thing about creating the custom data type is that you get to keep all the lightning-datatable functionality and adding your own on top of it.
In this blog I will go through a scenario I have implemented in the past. This time, I will implement the solution using the lightning-datatable component instead of using lightning-layout and lightning-record-edit-form.
Create three fields in the Contact object:
Set up development tools
Create the components
lightning-datatable base component, it saves me from having to build functionality, like the Contact form and the table layout.lightning-card displaying a lightning-datatable, the c-error-panel component if there is an error, the button to add a new row, and the c-modal component to verify the Contact's deletion.lightning-datatable attributes I am passing.handleCellChange every time any cell is updated via inline editing.'button-icon'. getRelatedContacts and upsertContacts in order to use them in the code.deleteRecord function from the uiRecordApi in line 4 to use it in the future. This function is provided by salesforce and many other useful ones. See more in this documentation.relatedContactsUtil.js. I created these functions in a separate file to keep the code organized. fieldName property set to the field's api name, and the editable property set to true.lightning-datatable component has functionality that allows you to add a button with any slds icon to perform row level actions. In this case, I am using utility:delete. Notice how I am giving it a name property, that is how I can identify that the user clicked on it and not in other button from the table.recordId property to store the record's Id. Notice the @api decorator, this makes the property public, and can be updated by a parent component.contacts property that will store the contacts in the table. The @track decorator is necessary because we need to component to observe the contacts's properties.tableError and the other one is error. The error property is related to general component errors, like error in server side calls. The tableError property stores the validation errors that will show in the lightning-datatable component.connectedCallback function in line 41 gets called when the component is inserted into the DOM. This function calls the refreshData function that will populate the contacts property.contacts property, since this is a new contact, it doesn't have an Id. The record Id is required in the lightning-datatable component, so, in this case, I created the getter function temporaryId that will return a temporary Id.handleSave function gets called when the user presses the save button on the lightning-datatable.validate function, and it it returns true, the function continues saving the contacts.modifyForInsert function which is imported from file relatedContactsUtil.js in line 5.upsertContacts function imperatively. I am importing the apex method in line 3. After the call succeeds, I am calling the refreshData function to refresh the table, and clear the draftValues in line 64.handleCellChange function gets called every time a field gets updated.handleAddNewRow function gets called when the user presses the "Add new row" button. The function adds a new contact to the table by pushing a JSON object with only the Id populated with a temporary Id.handleRowAction function gets executed when any row action happens. In this case, the row action is the delete button. When clicking on the delete button, this function stores the row selected in the rowToDelete property, and it opens a modal asking the user if they are sure about the deletion of the contact.handleYesDelete function gets called when the user approves the deletion of a contact in the modal. If the row to delete has the temporary keyword, it means that the contact wasn't inserted yet, so I only need to delete it from the table and from the table draft values(the list of contact records that have changed, this is a public property from the lightning-datatable component). If the row to delete has a real Id, I delete it from the database my calling the deleteContactDB function.This is a perfect example of how you can utilize base components in your custom applications. Here, I used lightning-datatable to create a quick contact creation table. I have accomplished ALL of the requirements( inline editing, adding and removing contacts, refreshing total value column) by only using one base component. In a previous post, I completed the same requirements, but using lighting-record-edit-form and lightning-layout, where I felt like I had more freedom, and leveraged the field validation functionality that the lighting-record-edit-form provides. The lightning-datatable is a very powerful component with a lot of functionality. I cannot wait to explore it more.