Creating Booklist App Using JavaScript and Web Local Storage

Language

In this article, we will create a simple book web application using HTML, JavaScript, and Web Browser's Local Storage. This will help you to understand and learn how to create a web application and store data on the local storage. The application that we will create can enlist the book information in the table dynamically. The program is very simple, the user can add book details and can view the details stored in the list table. It can also delete a list of books if he/she wants to delete it.

So, let's get started.

Getting Started

In this tutorial, I will be using Bootstrap and FontAwesome. Just simply click the said library names to download.

Creating the Interface

Th following script is the code for our index page that shows the book form and book list. Copy/Paste the code below and save the file as index.html

.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <link rel="stylesheet" href="assets/css/bootstrap.min.css" />
  6. <link rel="stylesheet" href="assets/fontawesome/css/all.css">
  7. <title>Booklist App</title>
  8. </head>
  9.  
  10. <div class="container mt-4">
  11.  
  12. <h1 class="display-4 text-center">
  13. <i class="fa fa-book-open text-primary"></i>Book<span class="text-primary">List</span> App
  14. </h1>
  15.  
  16.  
  17. <form id="book-form">
  18. <div class="form-group">
  19. <label for="title">Title</label>
  20. <input type="text" id="title" class="form-control" />
  21. </div>
  22.  
  23. <div class="form-group">
  24. <label for="author">Author</label>
  25. <input type="text" id="author" class="form-control" />
  26. </div>
  27.  
  28. <div class="form-group">
  29. <label for="isbn">ISBN#</label>
  30. <input type="text" id="isbn" class="form-control" />
  31. </div>
  32.  
  33. <input type="submit" value="Submit" class="btn btn-primary btn-block" />
  34. </form>
  35.  
  36. <table class="table table-striped mt-5">
  37. <tr>
  38. <th>Title</th>
  39. <th>Author</th>
  40. <th>ISBN#</th>
  41. <th>Action</th>
  42. </tr>
  43. </thead>
  44.  
  45. <tbody id="book-list">
  46.  
  47. </tbody>
  48. </table>
  49. </div>
  50.  
  51. <script src="app.js"></script>
  52. </body>
  53. </html>

Creating the JavaScript

The following script below contains the codes for functionalies for our book application. This contains the create, display, and delete books functions. Copy/Paste the code below and save the file as app.js

  1. class Book {
  2. constructor(title,author,isbn){
  3. this.title = title;
  4. this.author = author;
  5. this.isbn = isbn;
  6. }
  7. }
  8.  
  9.  
  10. class UI {
  11.  
  12.  
  13. static displayBooks(){
  14. let books = Store.getBooks();
  15.  
  16. books.forEach((book) => UI.addBookToList(book));
  17.  
  18.  
  19. }
  20.  
  21. static addBookToList(book){
  22. const list = document.querySelector('#book-list');
  23.  
  24. const row = document.createElement('tr');
  25.  
  26. row.innerHTML = `
  27. <td>${book.title}</td>
  28. <td>${book.author}</td>
  29. <td>${book.isbn}</td>
  30. <td><a href="#" class="btn btn-danger btn-sm delete">X</a></td>
  31. `;
  32.  
  33. list.appendChild(row);
  34. }
  35.  
  36.  
  37. static clearFields(){
  38. document.querySelector('#title').value = '';
  39. document.querySelector('#author').value = '';
  40. document.querySelector('#isbn').value = '';
  41. }
  42.  
  43.  
  44. static deleteBook(el){
  45. if(el.classList.contains('delete')){
  46. el.parentElement.parentElement.remove();
  47. }
  48. }
  49.  
  50.  
  51. static showAlerts(message,className){
  52.  
  53. const div = document.createElement('div');
  54. div.className = `alert alert-${className}`;
  55.  
  56. div.appendChild(document.createTextNode(message));
  57.  
  58. const container = document.querySelector('.container');
  59. const form = document.querySelector('#book-form');
  60. container.insertBefore(div,form);
  61.  
  62.  
  63. setTimeout(() => document.querySelector('.alert').remove(), 3000);
  64. }
  65. }
  66.  
  67.  
  68. class Store {
  69. static getBooks() {
  70. let books;
  71. if(localStorage.getItem('books') === null) {
  72. books = [];
  73. } else {
  74. books = JSON.parse(localStorage.getItem('books'));
  75. }
  76.  
  77. return books;
  78. }
  79.  
  80. static saveBook(book){
  81. let books;
  82. books = Store.getBooks();
  83. books.push(book);
  84. localStorage.setItem('books',JSON.stringify(books));
  85. }
  86.  
  87. static removeBook(isbn){
  88. const books = Store.getBooks();
  89.  
  90. books.forEach((book, index) => {
  91. if(book.isbn === isbn) {
  92. books.splice(index, 1);
  93. }
  94. });
  95.  
  96. localStorage.setItem('books', JSON.stringify(books));
  97. }
  98. }
  99.  
  100.  
  101. document.addEventListener('DOMContentLoaded',UI.displayBooks);
  102.  
  103.  
  104. document.querySelector('#book-form').addEventListener('submit',(e) => {
  105.  
  106.  
  107. e.preventDefault();
  108.  
  109.  
  110. const title = document.querySelector('#title').value;
  111. const author = document.querySelector('#author').value;
  112. const isbn = document.querySelector('#isbn').value;
  113.  
  114. if(title === '' || author === '' || isbn === ''){
  115. UI.showAlerts('Please fill in all details...','danger');
  116. } else {
  117.  
  118. const book = new Book(title,author,isbn);
  119.  
  120.  
  121. UI.addBookToList(book);
  122.  
  123.  
  124. Store.saveBook(book);
  125.  
  126.  
  127. UI.showAlerts('Book Added','success');
  128.  
  129.  
  130. UI.clearFields();
  131. }
  132.  
  133. });
  134.  
  135.  
  136. document.querySelector('#book-list').addEventListener('click',(e) => {
  137. UI.deleteBook(e.target);
  138.  
  139. Store.removeBook(e.target.parentElement.previousElementSibling.textContent);
  140.  
  141. UI.showAlerts('Book Removed','success');
  142. });

DEMO

That's it! You can now test the application we created by browsing the index.html in a browser. i.e. C:/Booklist%20App/index.html. If Ever you may encounter an error on your end, just try to recheck the code given above and compare it with your work maybe you miss something. You may also download the free working source code that I created for this tutorial.

I hope that this system can help you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding:)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byGarniza (not verified)on Wed, 05/06/2020 - 20:17

very good

Add new comment