Commit eda58ae9 by farama

Upload New File

parent 9a6e4bb5
Showing with 151 additions and 0 deletions
const rowstable = document.getElementsByTagName("tr");
document.addEventListener('click', function(e) {
e = e || window.event;
var target = e.target || e.srcElement,
text = target.textContent || target.innerText;
for (var i = 0; i < rowstable.length; i++) {
rowstable[i].addEventListener('click', deleteRow);
}
}, false);
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
if(sessionStorage.getItem("username") == undefined)
{
do
{
var username = prompt("Enter your name please", "");
}
while(username == undefined || username =="");
sessionStorage.setItem("username", username);
document.cookie = ("username=" + username);
} else
{
if(sessionStorage.getItem("username") != undefined)
{
username = sessionStorage.getItem("username");
} else if(getCookie("username") != "")
{
username = getCookie("username");
}
}
var productslist = [];
var quantityval = [];
var usernameEl = document.getElementById("header-text");
usernameEl.innerHTML = username + "'s Shopping list";
function getInputValue()
{
if(JSON.parse(localStorage.getItem("shoppingListOf" + username)) !== null)
{
var shoppingListOld = JSON.parse(localStorage.getItem("shoppingListOf" + username))
productslist = shoppingListOld.productsList;
quantityval = shoppingListOld.quantityVal;
}
productslist.push(document.getElementById("product").value);
quantityval.push(document.getElementById("quantity").value);
var shoppingList =
{
userName : username,
productsList : productslist,
quantityVal : quantityval
};
localStorage.setItem("shoppingListOf" + username, JSON.stringify(shoppingList));
addRow();
}
function drawTable()
{
if(JSON.parse(localStorage.getItem("shoppingListOf" + username)) !== null)
{
var shoppingList = JSON.parse(localStorage.getItem("shoppingListOf" + username));
var table = document.getElementById("Table1");
var length = table.rows.length;
for (let i = ((shoppingList.productsList.length)-1); i > -1; i--)
{
var row = table.insertRow(length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = ((table.rows.length) - (table.rows.length-i))+1 + ".";
cell2.innerHTML = shoppingList.productsList[i];
cell3.innerHTML = shoppingList.quantityVal[i];
}
}
}
function deleteRow()
{
if(confirm("Do you really want to delete this item?"))
{
var x = this.rowIndex;
var table = document.getElementById("Table1");
table.deleteRow(x);
if(JSON.parse(localStorage.getItem("shoppingListOf" + username)) !== null)
{
var shoppingListOld = JSON.parse(localStorage.getItem("shoppingListOf" + username))
productslist = shoppingListOld.productsList;
quantityval = shoppingListOld.quantityVal;
}
productslist.splice(x-1, 1);
quantityval.splice(x-1, 1);
var shoppingList =
{
userName : username,
productsList : productslist,
quantityVal : quantityval
};
localStorage.setItem("shoppingListOf" + username, JSON.stringify(shoppingList));
}
}
function addRow()
{
var table = document.getElementById("Table1");
var length = table.rows.length;
var row = table.insertRow(length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = length + ".";
cell2.innerHTML = productslist[productslist.length - 1];
cell3.innerHTML = quantityval[quantityval.length - 1];
document.getElementById("product").value = "";
document.getElementById("quantity").value = "";
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment