Today i will create small crud application using ES6 and HTML5 storage iam using MVC approach in this small application.It is very easy to use HTML5 LocalStorage store data in browser.
Demo
1) Index.js is a controller where our Model and View logic combines
2) BookView.js is wheres is our all view related stuff goes.
3) BookModel.js is where our all logic goes.
4) common.js where all commin elements or function goes.
5) for unique id i have used A Unique Hexatridecimal ID generator
index.js
import { clearResults, clearFormValues} from './views/BookView';
import { Book, Store} from './models/BookModel';
import { clearLoader, elements, UI, checkDataLocalStorare, renderLoader} from './views/common';
var uniqid = require('uniqid');
const book = new Book();
const ui = new UI();
const store = new Store();
document.addEventListener('DOMContentLoaded', store.diplayBooks());
//Add new Book(submit)
elements.submit.addEventListener('click', e => {
if (elements.title.value === '' || elements.author.value === '') {
ui.showAlert('All fields are required.', 'danger');
elements.title.focus();
} else {
addBook();
ui.showAlert('Book Added successfully.', 'success');
}
e.preventDefault();
});
//Add new Book
const addBook = () => {
const title = elements.title.value;
const author = elements.author.value;
const book = new Book(title, author, uniqid());
const store = new Store();
store.addBooks(book);
clearFormValues();
clearResults();
store.diplayBooks();
}
//Update Book (submit)
elements.update.addEventListener('click', e => {
if (elements.title.value === '' || elements.author.value === '') {
ui.showAlert('All fields are required.', 'danger');
elements.title.focus();
} else {
updateBook();
}
e.preventDefault();
});
//Add new Book localstorage
const updateBook = () => {
const updateId = elements.update.getAttribute('data-id');
store.updateBooks(updateId);
clearFormValues();
clearResults();
store.diplayBooks();
elements.title.focus();
ui.showAlert('Book update successfully.', 'success');
}
//Delete Book from localstorage
elements.booklist.addEventListener('click', e => {
if (e.target.dataset.type === 'delete') {
store.deleteBook(e.target.id);
renderLoader(elements.booklist);
store.diplayBooks();
clearLoader();
ui.showAlert('Book deleted successfully.', 'success');
checkDataLocalStorare();
e.stopPropagation();
}
});
//Edit Book from localstorage
elements.booklist.addEventListener('click', e => {
if (e.target.dataset.type === 'edit') {
elements.update.setAttribute('data-id', e.target.id)
store.getEditBook(e.target.id);
elements.cancel.classList.remove('hide');
elements.submit.classList.add('hide');
elements.update.classList.remove('hide');
e.stopPropagation();
}
});
//Show hide button from DOM
elements.cancel.addEventListener('click', e => {
elements.cancel.classList.add('hide');
elements.submit.classList.remove('hide');
elements.update.classList.add('hide');
clearFormValues();
})
BookView.js
import { elements} from './common';
import { Store} from '../models/BookModel';
const store = new Store();
export const clearFormValues = () => {
elements.title.value = '';
elements.author.value = '';
}
export const clearResults = () => {
elements.booklist.innerHTML = '';
}
const renderResult = result => {
const markUP = `
<tr>
<td>${result.title}</td>
<td>${result.author}</td>
<td> X </td>
<td> Edit </td>
</tr>
`
elements.booklist.insertAdjacentHTML('beforeend', markUP)
};
export const dataLocalStore = (result) => {
result.forEach(renderResult);
};
const noResult = () => {
const noDatamarkUP = `
<tr>
<td>No record found.</td>
<td></td>
<td></td>
<td></td>
</tr>
`
elements.booklist.insertAdjacentHTML('beforeend', noDatamarkUP)
};
export const checkDataLocalStorare = () => {
var items = JSON.parse(window.localStorage.getItem('books'))
if (items === null || items.length === 0) {
noResult();
}
};
BookModel.js
import { elements, renderLoader, clearLoader} from '../views/common';
import { clearResults, dataLocalStore, checkDataLocalStorare} from '../views/BookView';
export class Book {
constructor(title, author, id) {
this.title = title;
this.author = author;
this.id = id;
}
}
export class Store {
getBooks() {
let books;
if (localStorage.getItem('books') === null) {
books = [];
} else {
books = JSON.parse(localStorage.getItem('books'));
}
return books;
}
addBooks(book) {
const books = this.getBooks();
books.push(book);
localStorage.setItem('books', JSON.stringify(books));
}
diplayBooks() {
elements.update.classList.add('hide');
const books = this.getBooks();
dataLocalStore(books);
elements.cancel.classList.add('hide');
checkDataLocalStorare();
}
deleteBook(id) {
const books = this.getBooks();
books.forEach(function(book, index) {
if (book.id === id) {
books.splice(index, 1);
}
})
clearResults();
localStorage.setItem('books', JSON.stringify(books));
}
updateBooks(id) {
const books = this.getBooks();
books.forEach(function(book) {
if (book.id === id) {
book.title = elements.title.value;
book.author = elements.author.value;
}
})
localStorage.setItem('books', JSON.stringify(books));
}
getEditBook(id) {
const books = this.getBooks();
books.forEach(function(book, index) {
if (book.id === id) {
elements.author.value = book.author;
elements.title.value = book.title;
}
})
}
}
common.js
export const elements = {
title: document.getElementById('title'),
author: document.getElementById('author'),
formid: document.getElementById('formid'),
booklist: document.getElementById('book-list'),
container: document.querySelector('.container'),
submit: document.querySelector('#submit'),
cancel: document.querySelector('#cancel'),
update: document.querySelector('#update')
};
export class UI {
showAlert(message, className) {
const alertMarkup = `
<div class="alert alert-${className}">
${message}
</div>`;
elements.container.insertAdjacentHTML('afterbegin', alertMarkup);
setTimeout(function() {
document.querySelector('.alert').remove();
}, 2000)
}
}
index.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<title>Small CRUD Application</title>
<style>
.hide{
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center">Small Crud App with ES6</h1>
<form id="formid">
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Author</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="author">
</div>
</div>
<div class="row">
<div class="btn-group mx-auto">
<input type="submit" value="Submit" id="submit" class="btn btn-primary">
<input type="submit" value="Update" data-id="" id="update" class="btn btn-primary">
<input type="button" value="Cancel" id="cancel" class="btn btn-primary">
</div></div>
</form>
<br>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Delete</th>
<th>Update</th>
</tr>
</thead>
<tbody id="book-list">
</tbody>
</table>
</div>
</body>
</html>
myggail b9c45beda1 https://coub.com/stories/2778413-ib-chemistry-periodicity-exam-questions-link
charos b9c45beda1 https://coub.com/stories/2735974-star-wars-hero-s-journey-worksheet-top
percpait b9c45beda1 https://coub.com/stories/2720365-hot-ilya-efimov-lp-electric-guitar-kontakt-torrent-download
holinev 8bfa699c61 https://wakelet.com/wake/nKWoJ6KouuO0eFMtuPzIW
tamest 79a0ff67a5 https://coub.com/stories/2735516-john-mask-boy-mp4-at-streamtape-com-geffar
berardi 79a0ff67a5 https://coub.com/stories/2784349-windows-xp-professional-64-bit-clean-virtualbox-image-serial-key-keygen-better
quilred ba0249fdb3 https://wakelet.com/wake/mk6tz8MoTnETVwwNOmX4w
ilefide 7383628160 https://wakelet.com/wake/yzBKTFCjWp-If8Eg1ARx-
padghail 7383628160 https://trello.com/c/VrKeSqgz/50-free-crack-adobe-illustrator-cc-2018-2300-64-bit-crackl
dilter 7383628160 https://wakelet.com/wake/a4umDxeJStMS9O_iNnLmi
redmbur fe98829e30 https://coub.com/stories/2958163-top-catia-v5r19-crack-win-64-41
laurnag fe98829e30 https://wakelet.com/wake/xjBBQePqoDIijcdFWxq8j
laurnag fe98829e30 https://wakelet.com/wake/xjBBQePqoDIijcdFWxq8j
vanphyl fe98829e30 https://trello.com/c/uDcUgXfd/59-street-hero-1984-download-book-churfio
osicath fe98829e30 https://trello.com/c/EGUK2nUq/44-tarak-mehta-ka-ulta-chasma-full-link-episodes-free-download
idabhet fe98829e30 https://coub.com/stories/3029057-waves-maxxaudio-3-drivers-for-dell-xps-l702x-rar
uggmal fe98829e30 https://wakelet.com/wake/J70szgzz2oBk7FAuq81KF
alonnyll fe98829e30 https://coub.com/stories/2980541-insanity-max-30-max-out-abs-m4v-torrent
blarayl fe98829e30 https://trello.com/c/qiP3SS5T/34-peugeot-sedre-2011-keygen-17-nevefre
wadltab fe98829e30 https://www.cloudschool.org/activities/ahFzfmNsb3Vkc2Nob29sLWFwcHI5CxIEVXNlchiAgID_5sjgCgwLEgZDb3Vyc2UYgICA_4CWmQsMCxIIQWN0aXZpdHkYgIDA0J7w-QgMogEQNTcyODg4NTg4Mjc0ODkyOA
moaring fe98829e30 https://trello.com/c/qOovux9C/89-twinmotion-2016-with-crack-linkrar
giaparn d868ddde6e https://coub.com/stories/3058471-raavanan-tamil-movie-with-english-subtitles-better-download-torrent
tarrima d868ddde6e https://coub.com/stories/2975058-new-contoh-karangan-pedaran-basa-sunda
harmyam d868ddde6e https://coub.com/stories/3027746-autocad-lt-2011-better-crack-file-only-64-bit
vitwish d868ddde6e https://coub.com/stories/3044707-canoco-for-windows-45-free-98-shanlayd
vyvalee d868ddde6e https://coub.com/stories/3084002-adobe-acrobat-xi-pro-11-0-31-final-crack-free-64-bit
garmfab d868ddde6e https://coub.com/stories/2942011-mercedes-citaro-g-c2-omsi-2-crac-link
lisnev d868ddde6e https://coub.com/stories/3077642-studio-one-2-product-key-crack-link
jesswamb d868ddde6e https://coub.com/stories/3133688-baixar-audiffex-pedals-v107-com-18
jemder d868ddde6e https://coub.com/stories/3120237-bde-installer-for-rad-studio-delphi-c-builder-10-3-rio-jaelper
marric d868ddde6e https://coub.com/stories/3030075-alice-madness-returns-skidrow-rar-password-high-quality
gervglyn d868ddde6e https://coub.com/stories/3076180-2011-focus-on-grammar-fourth-edition-level-5-answer-key-pdf
darrguil d868ddde6e https://coub.com/stories/3041735-crack-upd-bandicam-v3-4-2-1258-final-keygen-sh
xilalis d868ddde6e https://coub.com/stories/2934679-geneious-license-crackl-_top_
nikegayt b7f02f1a74 https://necnemodomanlist.wixsite.com/engahargai/post/learn-english-listening-premium-v4-4-1-cracked-android
wenfyll b7f02f1a74 https://resgebundnerhago.wixsite.com/ualildamcont/post/winrar-x64-64-bit-5-21-final-key-team-eat-alien-download-pc
ogilbry b7f02f1a74 https://tanja84.wixsite.com/exscorcaso/post/download-anti-deep-freeze-versi-7-30
ogilbry b7f02f1a74 https://tanja84.wixsite.com/exscorcaso/post/download-anti-deep-freeze-versi-7-30
wyneula c0c125f966 https://weiglosonabatat.wixsite.com/sendresili/post/activation-adobe-premiere-pro-cc-zip-torrent-windows-full
halutyn fb158acf10 https://johhkachansierebfa.wixsite.com/unclochisac/post/valentina-studio-pro-10-6
werdebb f4bc01c98b https://coub.com/stories/3330466-awem-studio-star-defender-4-crack-install
alyvan f4bc01c98b https://coub.com/stories/3297684-crack-quickbooks-enterprise-solutions-v10-2010-usa-sansha
dafnyseu f4bc01c98b https://coub.com/stories/3227244-adobe-acrobat-6-0-professional-serial-number-crack-top
xylogray f4bc01c98b https://coub.com/stories/3343541-pthc-private-pae-5-mother-daughter-4yo-torrent-yanaber
favofary f4bc01c98b https://coub.com/stories/3495275-prime-loops-xxl-hip-hop-drums-multiformat-rar-infanzia-bandiera-bring-presepe-zappulla-simpatic
garvsofe f4bc01c98b https://coub.com/stories/3521136-link-symantec-ghost-solution-suite-2-5-1-license-43