Title : Html5 local storage example - a small tutorial.
User can enter the contact details , store the contact details in a table , can modify them, can retrive them and can remove them when he wanted.
Html5 comes with a new feature local storage that can add an item with key value pair and can retrive,modify,delete them when he wants.
Here is the code for the above example.
User can enter the contact details , store the contact details in a table , can modify them, can retrive them and can remove them when he wanted.
Html5 comes with a new feature local storage that can add an item with key value pair and can retrive,modify,delete them when he wants.
Here is the code for the above example.
<!doctype html>
<
html>
<
head>
</
head>
<
body>
index: window.localStorage.getItem(
"Contacts:index"),
$table: document.getElementById(
"contacts-table"),
$form: document.getElementById(
"contacts-form"),
$button_save: document.getElementById(
"contacts-op-save"),
$button_discard: document.getElementById(
"contacts-op-discard"),
init:
function() {
window.localStorage.setItem(
"Contacts:index", Contacts.index = 1);
}
Contacts.$form.reset();
Contacts.$button_discard.addEventListener(
"click", function(event) {
Contacts.$form.reset();
Contacts.$form.id_entry.value = 0;
},
true);
Contacts.$form.addEventListener(
"submit", function(event) {
id: parseInt(
this.id_entry.value),
first_name:
this.first_name.value,
last_name:
this.last_name.value,
email:
this.email.value
};
Contacts.storeAdd(entry);
Contacts.tableAdd(entry);
}
Contacts.storeEdit(entry);
Contacts.tableEdit(entry);
}
event.preventDefault();
},
true);
key = window.localStorage.key(i);
contacts_list.push(JSON.parse(window.localStorage.getItem(key)));
}
}
contacts_list
.sort(
function(a, b) {
})
.forEach(Contacts.tableAdd);
}
}
Contacts.$table.addEventListener(
"click", function(event) {
Contacts.$form.first_name.value = entry.first_name;
Contacts.$form.last_name.value = entry.last_name;
Contacts.$form.email.value = entry.email;
Contacts.$form.id_entry.value = entry.id;
}
Contacts.storeRemove(entry);
Contacts.tableRemove(entry);
}
}
event.preventDefault();
}
},
true);
},
storeAdd:
function(entry) {
entry.id = Contacts.index;
window.localStorage.setItem(
"Contacts:index", ++Contacts.index);
window.localStorage.setItem(
"Contacts:"+ entry.id, JSON.stringify(entry));
},
storeEdit:
function(entry) {
window.localStorage.setItem(
"Contacts:"+ entry.id, JSON.stringify(entry));
},
storeRemove:
function(entry) {
window.localStorage.removeItem(
"Contacts:"+ entry.id);
},
tableAdd:
function(entry) {
$td = document.createElement(
"td");
$td.appendChild(document.createTextNode(entry[key]));
$tr.appendChild($td);
}
}
$td = document.createElement(
"td");
$td.innerHTML =
'<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
$tr.appendChild($td);
$tr.setAttribute(
"id", "entry-"+ entry.id);
Contacts.$table.appendChild($tr);
},
tableEdit:
function(entry) {
$tr.innerHTML =
"";
$td = document.createElement(
"td");
$td.appendChild(document.createTextNode(entry[key]));
$tr.appendChild($td);
}
}
$td = document.createElement(
"td");
$td.innerHTML =
'<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
$tr.appendChild($td);
},
tableRemove:
function(entry) {
Contacts.$table.removeChild(document.getElementById(
"entry-"+ entry.id));
}
};
Contacts.init();
</
body></html>
I willl explain the line by line code with in this week so, please follow this blog.
very nice example ... :)
ReplyDeleteIt's Working browser. But in my emulator I can't enter text box values.
ReplyDelete