Delete data records in an Oracle JET virtual DOM app
Introduction
This tutorial shows you how to use your Oracle JavaScript Extension Toolkit (Oracle JET) virtual DOM app to delete an existing data record and submit the change to a REST service.
Objectives
In this tutorial, you will learn how to delete an existing data record and submit the change to a REST service.
Prerequisites
- A development environment set up to create Oracle JET virtual DOM apps that includes an installation of Node.js
- Completion of the previous tutorial in this learning path, Update Data Records in an Oracle JET Virtual DOM App
- The completed app JET-Virtual-DOM-app-final.zip optionally downloaded
Task 1: Create a Delete Button
-
Navigate to the
JET-Virtual-DOM-app/src/components/ActivityItemdirectory and open theItemActionsContainer.tsxfile in an editor. -
In the
Propstype alias, define an additionaldeleteproperty.type Props = { create: () => void; edit: () => void; itemSelected: Partial<Item>; delete: () => void; }; -
Find the
<oj-button id="updateButton"element and add anoj-buttonelement below it. Set theonojActionattribute to{props.delete}and thedisabledattribute tohideActions.return ( <div> <oj-button id="createButton" onojAction={props.create}>Create</oj-button> <oj-button id="updateButton" disabled={hideActions} onojAction={props.edit}>Update</oj-button> <oj-button id="deleteButton" disabled={hideActions} onojAction={props.delete}>Delete</oj-button> </div> -
Save the
ItemActionsContainer.tsxfile.Your code should look similar to this
final-ItemActionsContainer.tsx.txtfile.
Task 2: Handle Deleting Records
Use the Fetch API and the HTTP DELETE method to delete a record from the REST service. Use the mutate method of the RESTDataProvider to update your RESTDataProvider instance.
-
Navigate to the
JET-Virtual-DOM-app/src/components/ActivityItemdirectory and open theActivityItemContainer.tsxfile in an editor. -
Within the
ActivityItemContainerfunction, below your previous code that updates records, create a method calleddeleteItem.This method requests confirmation from the user to delete an item and, on successful confirmation, creates a
DELETErequest that it sends to the REST service. If theDELETEoperation is a success, the method then invokes aRESTDataProvidermutateevent to notify data provider consumers that an item has been deleted.const deleteItem = async () => { const really = confirm("Do you really want to delete this item?"); if (really) { // Create and send request to REST service to delete item const request = new Request(`${restServerURLItems}${itemData.id}`, { method: "DELETE", }); const response = await fetch(request); // Call mutate method to notify dataprovider consumers that an // item has been removed if (response.status === 200) { const removedRowKey = itemData.id; const removedRowMetaData = { key: removedRowKey }; props.data?.mutate({ remove: { data: [itemData.id], keys: new Set([removedRowKey]), metadata: [removedRowMetaData], }, }); } else { alert( "Delete failed with status " + response.status + " : " + response.statusText ); } console.log("Deleted item."); } else { console.log("OK, we canceled that delete."); } }; -
In the
returnstatement, update theItemActionsContainerelement with adeleteattribute.<div id="container"> <h3>Activity Items</h3> <ItemActionsContainer edit={openEditDialog} delete={deleteItem} itemSelected={activityItemValue} create={openCreateDialog} /> . . . </div> -
Save the
ActivityItemContainer.tsxfile.Your code should look similar to this
final-delete-ActivityItemContainer.tsx.txtfile.
Task 3: Run the Virtual DOM App and Delete a Record
- In the terminal window, run your virtual DOM app.
npx ojet serve -
In the app, click the Baseball activity, and then click the SureFire Ball (Set of 4) item.
-
Click the Delete button. A confirmation window opens.
-
Click OK.
The section refreshes, and the item has been deleted.
-
Close the browser window or tab that displays your running virtual DOM app.
- In the terminal window, press Ctrl+C, and if prompted, enter
yto exit the Oracle JET tooling batch job.
Task 4: (Optional) Run a Virtual DOM App from a Restored App
If you want to run the completed Oracle JET virtual DOM app from the supplied code, you can restore the app from the downloaded archive file. To work with a “stripped and zipped” Oracle JET virtual DOM app, you must restore project dependencies, including Oracle JET tooling and the required libraries and modules, within the extracted app.
- Download the
JET-Virtual-DOM-app-final.zipfile and extract the contents of the completed app to theJET-Virtual-DOM-app-finaldirectory. - In the terminal window, navigate to the
JET-Virtual-DOM-app-finaldirectory and restore the Oracle JET virtual DOM app.npm install - Wait for confirmation.
Success: Restore completeThe app is ready to run.
- Run the virtual DOM app and test in the browser.
npx ojet serve -
Close the browser window or tab that displays your running virtual DOM app.
- In the terminal window, press Ctrl+C, and if prompted, enter
yto exit the Oracle JET tooling batch job.
Next Step
This tutorial concludes the module CRUD Operations Using a REST Service in this learning path on building virtual DOM web apps.
- Fetch Data from the REST API in Oracle JET
- Create a Form to Create Data Records in an Oracle JET Virtual DOM App
- Update Data Records in an Oracle JET Virtual DOM App
- Delete Data Records in an Oracle JET Virtual DOM App
You can proceed to the next tutorial in the learning path, Set up a Test Environment in Oracle JET, in the module End-to-End Testing in Oracle JET.
You can also return to the virtual DOM learning path’s main page to access all the modules on building virtual DOM apps, as well as other learning paths.
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Delete data records in an Oracle JET virtual DOM app
F70630-06