This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
123 lines (111 loc) · 3.62 KB
/
add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function createKey(newDay, newTime) {
return newDay + newTime.slice(0, 2) + newTime.slice(3)
}
function coladd() {
var table = document.getElementById("table");
let elements = document.getElementsByName('accessible-radio');
let len = elements.length;
let newDay = '';
for (let i = 0; i < len; i++) {
if (elements.item(i).checked) {
newDay = elements.item(i).value;
}
}
const newTime = document.getElementById('inputTime').value
const newName = document.getElementById('inputName').value
const newURL = document.getElementById('inputURL').value
if (document.getElementById('inputTime').value == "" || document.getElementById('inputName').value == "" || document.getElementById('inputURL').value == "") {
alert("Please Input All Values");
return false;
} else {
// 行を行末に追加
var row = table.insertRow(-1);
var cell1 = row.insertCell(-1);
var cell2 = row.insertCell(-1);
var cell3 = row.insertCell(-1);
// var cell4 = row.insertCell(-1);
var cell5 = row.insertCell(-1);
// セルの内容入力
cell1.innerHTML = newDay;
cell2.innerHTML = newTime;
cell3.innerHTML = newName;
// cell4.innerHTML = newURL;
cell5.innerHTML = '<input type="button" id="coladd" onclick="removeStorage(\'' + createKey(newDay, newTime) + '\')" value="delete">';
var array = [];
var obj = {
'Day': newDay,
'Time': newTime,
'Name': newName,
'URL': newURL
};
array.push(obj);
var setjson = JSON.stringify(obj);
localStorage.setItem(createKey(newDay, newTime), setjson);
document.getElementById('inputTime').value = ""
document.getElementById('inputName').value = ""
document.getElementById('inputURL').value = ""
return true;
}
}
function coldel(obj) {
var table = document.getElementById("table");
// 削除ボタンを押下された行を取得
tr = obj.parentNode.parentNode;
// trのインデックスを取得して行を削除する
tr.parentNode.deleteRow(tr.sectionRowIndex);
}
// ローカルストレージに保存
var saveStorage = function () {
var key = document.getElementById("key").value;
var value = document.getElementById("value").value;
if (key && value) {
localStorage.setItem(key, value);
}
key = "";
value = "";
viewStorage();
};
// 特定のキーと値を削除
var removeStorage = function (key) {
localStorage.removeItem(key);
key = "";
viewStorage();
};
// 全てのキーと値を削除
var clearStorage = function () {
localStorage.clear();
viewStorage();
};
// ローカルストレージのデータを表に出力
var viewStorage = function () {
var tb = document.getElementById("table")
// テーブルの初期化
while (tb.firstChild) {
tb.removeChild(tb.firstChild);
}
// テーブルの出力
for (var i = 0; i < localStorage.length; i++) {
var _key = localStorage.key(i);
var tr = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
// var td4 = document.createElement("td");
var td5 = document.createElement("td");
tb.appendChild(tr);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
// tr.appendChild(td4);
tr.appendChild(td5);
obj = JSON.parse(localStorage.getItem(_key))
td1.innerHTML = obj['Day'];
td2.innerHTML = obj['Time'];
td3.innerHTML = obj['Name'];
// td4.innerHTML = obj['URL'];
td5.innerHTML = '<input type="button" onclick="removeStorage(\'' + _key + '\')" value="delete">';
// console.log(localStorage)
}
};
// clearStorage()
viewStorage()