Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
farama
/
icd0007
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
eda58ae9
authored
May 11, 2020
by
farama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
9a6e4bb5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
151 additions
and
0 deletions
Lab10/script.js
Lab10/script.js
0 → 100644
View file @
eda58ae9
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment