To update the record, add the following code in ‘editOrder’ method:
function editOrder() {
var ordno = document.getElementById('txtordno');
var id = ordno.value;
//S1: Open Database
var db = window.indexedDB.open("OrdersDB");
if (db) {
db.onsuccess = function (e) {
//S2: Set the transaction on the ObjectStore for read-write operation
var transaction = e.target.result.transaction("NewOrderStore", "readwrite");
var tbl = transaction.objectStore("NewOrderStore");
//S3: Use 'get' method to get the record details
var requestToUpdate = tbl.get(id).onsuccess = function (e) {
//S4: Assign the record to the 'Order object' of name 'orderToEdit'
orderToEdit = e.target.result;
//S5: Assign the New values
orderToEdit.PharmacistName =
document.getElementById('txtpharmacist').value;
orderToEdit.OrderDate = selectedDate;
orderToEdit.OrderMedicine =
document.getElementById('txtordermedicine').value;
orderToEdit.Quantity = document.getElementById('txtqty').value;
orderToEdit.UnitPrice = document.getElementById('txtunitprice').value;
orderToEdit.TotalPrice =
parseInt(document.getElementById('txtunitprice').value) *
parseInt(document.getElementById('txtqty').value);
//S6: Update the Record using Put method
var req = tbl.put(orderToEdit);
req.onsuccess = function (e) {
var res = document.getElementById("txtres");
res.value = "Update Completed Successfully" + e.target.result;
document.getElementById('txtprice').value = orderToEdit.TotalPrice;
};
req.onerror = function (e) {
var res = document.getElementById("txtres");
res.value = "Error Occured" + e.target.result;
};
};
};
loadOrder();
}
}
Add the following code into the ‘deleteOrder’ method. The method performs the following operations:
- Open the database
- Set the transaction for performing “readwrite” operations.
- Call the ‘delete’ method and pass the ‘id’ to it.
function deleteOrder() {
var ordno = document.getElementById('txtordno');
id = ordno.value;
//S1: Open the database
var db = window.indexedDB.open("OrdersDB");
if (db) {
db.onsuccess = function (e) {
//S2: Set the transaction to perform 'readwrite' operation
var transaction = e.target.result.transaction("NewOrderStore", "readwrite");
var objectStore = transaction.objectStore("NewOrderStore");
//S3: Delete the record using 'delete' method.
var delRequest = objectStore.delete(id);
};
}
loadOrder();
neworder();
}