Javascript - Simple CRUD

In this tutorial we will create a Simple CRUD using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

This is the link for the bootstrap that has been used for the layout https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">Javascript - Simple CRUD</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-1"></div>
  17.                 <div class="col-md-10">
  18.                         <form action="javascript:void(0);" method="POST" class="form-inline" onsubmit="Add()">
  19.                                 <label>Name:</label>
  20.                                 <input type="text" id="name" class="form-control"/>
  21.                                 <button type="submit" class="btn btn-primary form-control"><span class="glyphicon glyphicon-plus"></span> Add</button>
  22.                         </form>
  23.                         <br />
  24.                         <div id="edit">
  25.                                 <form action="javascript:void(0);" method="POST" class="form-inline" id="update">
  26.                                         <label>Name:</label>
  27.                                         <input type="text" id="edit_name" class="form-control"/>
  28.                                         <button type="submit" class="btn btn-warning form-control"><span class="glyphicon glyphicon-update"></span> Update</button> <a class="btn btn-danger" onclick="Close()" aria-label="Close">&#10006;</a>
  29.                                 </form>
  30.                         </div>
  31.                         <br />
  32.                         <table class="table table-bordered">
  33.                                 <thead>
  34.                                         <tr>
  35.                                                 <th>Name</th>
  36.                                                 <th>Action</th>
  37.                                         </tr>
  38.                                 </thead>
  39.                                 <tbody id="name_list">
  40.                                
  41.                                 </tbody>
  42.                         </table>
  43.                 </div>
  44.         </div>
  45. </body>
  46. <script src="js/script.js"></script>
  47. </html>

Creating the Script

This code contains the script of the application. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var el = document.getElementById('name_list');
  2. var names = [];
  3.  
  4. ListAll();
  5. Close();
  6.  
  7. function ListAll() {
  8.         var data = '';
  9.         if (names.length > 0) {
  10.                 for (i = 0; i < names.length; i++) {
  11.                         data += '<tr>';
  12.                         data += '<td>' + names[i] + '</td>';
  13.                         data += '<td colspan="2"><center><button class="btn btn-warning" onclick="Edit(' + i + ')"><span class="glyphicon glyphicon-edit"></span> Edit</button> | <button class="btn btn-danger" onclick="Delete(' + i + ')"><span class="glyphicon glyphicon-trash"></span> Delete</button></center></td>';
  14.                         data += '</tr>';
  15.                 }
  16.         }
  17.        
  18.         el.innerHTML = data;
  19. };
  20.  
  21.  
  22. function Add() {
  23.     var el = document.getElementById('name')
  24.         var name = el.value;
  25.         if (name) {
  26.                 names.push(name.trim());
  27.                 el.value = '';
  28.                 ListAll();
  29.         }
  30.         ListAll();
  31. };
  32.  
  33. function Delete(item) {
  34.         names.splice(item, 1);
  35.         ListAll();
  36.        
  37. };
  38.  
  39. function Edit(item) {
  40.         var el = document.getElementById('edit_name');
  41.         el.value = names[item];
  42.         document.getElementById('edit').style.display = 'block';
  43.         self = this;
  44.        
  45.         document.getElementById('update').onsubmit = function() {
  46.         var name = el.value;
  47.                 if (name) {
  48.                         self.names.splice(item, 1, name.trim());
  49.                         self.ListAll();
  50.                         Close();
  51.                 }
  52.         }
  53. };
  54.  
  55.  
  56. function Close() {
  57.         document.getElementById('edit').style.display = 'none';
  58. }
There you have it we successfully created a Simple CRUD using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Comments

Allen

Add new comment