Delete data records in an Oracle JET web app
Introduction
This tutorial shows you how to use your Oracle JavaScript Extension Toolkit (Oracle JET) web 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 web apps that includes an installation of Node.js
- Completion of the previous tutorial in this learning path, Update Data Records in Oracle JET
- The completed app jet_rest_crud_application_final.zip optionally downloaded
Task 1: Create a Delete Button in the View
-
Navigate to the
JET_Web_Application/src/ts/views
directory and open thedashboard.html
file in an editor. -
Find the
oj-button id="updateButton"
element and add anoj-button
element below it. Set theon-oj-action
attribute to"[[deleteItem]]"
and thedisabled
attribute to"[[!itemSelected()]]"
.<oj-bind-if test="[[activitySelected()]]"> . . . <oj-button id="createButton" on-oj-action="[[showCreateDialog]]">Create</oj-button> <!-- If itemSelected is set to false, disabled is true, and vice versa. --> <oj-button id="updateButton" disabled="[[!itemSelected()]]" on-oj-action="[[showEditDialog]]">Update</oj-button> <oj-button id="deleteButton" disabled="[[!itemSelected()]]" on-oj-action="[[deleteItem]]">Delete</oj-button> <oj-list-view id="itemsList" . . .
-
Save the
dashboard.html
file.Your code should look similar to this final-delete-dashboard-html.txt file.
Task 2: Handle Deleting Records in the ViewModel
Use the Fetch API and the HTTP DELETE
method to delete a record from the REST service. Use the mutate()
method of the RESTDataProvider
class to update your RESTDataProvider
instance.
-
Navigate to the
JET_Web_Application/src/ts/viewModels
directory and open thedashboard.ts
file in an editor. -
Within the
DashboardViewModel
class, below your previous code that updates records, create a method calleddeleteItem
.. . . public updateItemSubmit = async (event: ojButtonEventMap["ojAction"]) => { . . . }; public deleteItem = async (event: ojButtonEventMap["ojAction"]) => { };
-
Within the
deleteItem
method you created, declare acurrentRow
variable and use anif
statement to confirm that an item has been selected.public deleteItem = async (event: ojButtonEventMap["ojAction"]) => { const currentRow = this.selectedRow; if (currentRow != null) { } };
-
Add another
if
statement to check the user’s confirmation, then add code to send a request to delete the selected item to the REST service, and finally update theRESTDataProvider
instance.public deleteItem = async (event: ojButtonEventMap["ojAction"]) => { let itemID = this.firstSelectedItem().data.id; const currentRow = this.selectedRow; if (currentRow != null) { let really = confirm("Are you sure you want to delete this item?"); if (really) { // Create and send request to delete row on REST service const request = new Request( `${this.restServerURLItems}${itemID}`, { method: "DELETE" } ); const response = await fetch(request); // Create remove mutate event and call mutate method // to notify data-provider consumers that a row has been // removed if (response.status === 200) { const removedRowKey = itemID; const removedRowMetaData = { key: removedRowKey }; this.itemsDataProvider.mutate({ remove: { data: [itemID], keys: new Set([removedRowKey]), metadata: [removedRowMetaData], }, }); this.itemsDataProvider.refresh(); } else { alert("Delete failed with status " + response.status + " : " + response.statusText) } } } }; // End deleteItem
-
Save the
dashboard.ts
file.Your code should look similar to this final-delete-dashboard-ts.txt file.
Task 3: Run the Web App and Delete a Record
-
In the terminal window, run your web 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 web app.
-
In the terminal window, press Ctrl+C, and if prompted, enter
y
to exit the Oracle JET tooling batch job.
Task 4: (Optional) Run a Web App from a Restored App
If you want to run the completed Oracle JET web app from the supplied code, you can restore the app from the downloaded archive file. To work with a “stripped and zipped” Oracle JET web app, you must restore project dependencies, including Oracle JET tooling and the required libraries and modules, within the extracted app.
-
Download the
jet_rest_crud_application_final.zip
file and extract the contents of the completed app to thejet_rest_crud_application_final
folder. -
In the terminal window, navigate to the
jet_rest_crud_application_final
folder and restore the Oracle JET web app by installing the NPM packages that it requires.npm install
-
Wait for a confirmation message similar to the following.
added 284 packages in 59s
The app is ready to run.
-
Run the web app and test in the browser.
npx ojet serve
-
Close the browser window or tab that displays your running web app.
-
In the terminal window, press Ctrl+C, and if prompted, enter
y
to exit the Oracle JET tooling batch job.
Next Step
This concludes the learning path series that describes how to develop Oracle JET web apps that implement responsive design, master-detail patterns, and CRUD functionality. Two subsequent learning paths complement the series. One describes how to develop a web component using Oracle JET’s composite component architecture (CCA) that you can then consume in the CRUD app that you have developed. To proceed to the first tutorial in the learning path that describes how to develop a CCA-based web component, click here. The second learning path describes how to write and execute Selenium WebDriver test scripts that automate the create and delete functionality in the CRUD app using Oracle JET’s Component WebElements UI automation library. Learn more.
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 web app
F11596-10
September 2024