Crud Operations Using Vue.Js: a Basic Example

by Luigi Nori Date: 23-05-2020 javascript vue.js vue crud


In this tutorial, we show you how to create CRUD application using vue js. here is very basic and simple example of vue.js crud app. using this vuejs crud (create read update delete) you can easily implement with php mysql or also in laravel or codeigniter framework. So it is a very simple example tutorial for beginner.

So just follow bellow two files and get very amazing and simple CRUD app with vue.js. we will create following two files for making simple crud app.

1 - index.html
2 - index.js

Now you can see bellow both files code:

index.html
<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>Basic CRUD Operations Using Vue.js Example</title>
    <link rel="stylesheet prefetch" href="bootstrap.min.css">
    <link rel="stylesheet prefetch" href="bootstrap-theme.min.css">
</head>

<body>
<div class="container">
  <header class="page-header">
    <div class="branding">
      <img src="https://vuejs.org/images/logo.png" alt="Logo" title="Home page" class="logo"/>
      <h1>Basic CRUD Operations Using Vue.js Example - HDTuto.com</h1>
    </div>
  </header>
  <main id="app"></main>
</div>

<template id="invoice-list">
  <section>
  <div class="actions">
    <router-link class="btn btn-default" :to="{path: '/invoice-add'}">
      <span class="glyphicon glyphicon-plus"></span>
      Add invoice
    </router-link>
  </div>
  <div class="filters row">
    <div class="form-group col-sm-3">
      <label for="search-element">invoice name</label>
      <input v-model="searchKey" class="form-control" id="search-element" requred/>
    </div>
  </div>
  <table class="table">
    <thead>
    <tr>
      <th>Name</th>
      <th>Description</th>
      <th>Price</th>
      <th class="col-sm-2">Actions</th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="invoice in filteredinvoices">
      <td>
        <router-link :to="{name: 'invoice', params: {invoice_id: invoice.id}}">{{ invoice.name }}</router-link>
      </td>
      <td>{{ invoice.description }}</td>
      <td>
        {{ invoice.price }}
        <span class="glyphicon glyphicon-euro" aria-hidden="true"></span>
      </td>
      <td>
        <router-link class="btn btn-warning btn-xs" :to="{name: 'invoice-edit', params: {invoice_id: invoice.id}}">Edit</router-link>
        <router-link class="btn btn-danger btn-xs" :to="{name: 'invoice-delete', params: {invoice_id: invoice.id}}">Delete</router-link>
      </td>
    </tr>
    </tbody>
  </table>
  </section>
</template>

<template id="invoice-add">
    <section>
  <h2>Add new invoice</h2>
  <form v-on:submit="createinvoice">
    <div class="form-group">
      <label for="add-name">Name</label>
      <input class="form-control" id="add-name" v-model="invoice.name" required/>
    </div>
    <div class="form-group">
      <label for="add-description">Description</label>
      <textarea class="form-control" id="add-description" rows="10" v-model="invoice.description"></textarea>
    </div>
    <div class="form-group">
      <label for="add-price">Price, <span class="glyphicon glyphicon-euro"></span></label>
      <input type="number" class="form-control" id="add-price" v-model="invoice.price"/>
    </div>
    <button type="submit" class="btn btn-primary">Create</button>
     <router-link class="btn btn-default" :to="{path: '/'}">Cancel</router-link>
  </form>
</section>
</template>

<template id="invoice">
    <section>
  <h2>{{ invoice.name }}</h2>
  <b>Description: </b>
  <div>{{ invoice.description }}</div>
  <b>Price:</b>
  <div>{{ invoice.price }}<span class="glyphicon glyphicon-euro"></span></div>
  <br/>
  <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
  <router-link to="'/'">Back to invoice list</router-link>
</section>
</template>

<template id="invoice-edit">
    <section>
  <h2>Edit invoice</h2>
  <form v-on:submit="updateinvoice">
    <div class="form-group">
      <label for="edit-name">Name</label>
      <input class="form-control" id="edit-name" v-model="invoice.name" required/>
    </div>
    <div class="form-group">
      <label for="edit-description">Description</label>
      <textarea class="form-control" id="edit-description" rows="3" v-model="invoice.description"></textarea>
    </div>
    <div class="form-group">
      <label for="edit-price">Price, <span class="glyphicon glyphicon-euro"></span></label>
      <input type="number" class="form-control" id="edit-price" v-model="invoice.price"/>
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
    <router-link to="'/'" class="btn btn-default">Cancel</router-link>
  </form>
</section>
</template>

<template id="invoice-delete">
    <section>
  <h2>Delete invoice {{ invoice.name }}</h2>
  <form v-on:submit="deleteinvoice">
    <p>The action cannot be undone.</p>
    <button type="submit" class="btn btn-danger">Delete</button>
    <router-link to="'/'" class="btn btn-default">Cancel</router-link>
  </form>
</section>
</template>

<script src='vue.min.js'></script>
<script src='vue-router.min.js'></script>
<script src="index.js"></script>

</body>
</html>

index.js

var invoices = [
  {id: 1, name: 'Laravel', description: 'Provide Laravel Information.', price: 100},
  {id: 2, name: 'AngularJS', description: 'Provide AngularJS Information.', price: 100},
  {id: 3, name: 'PHP', description: 'Provide PHP Information.', price: 100}
];

function findinvoice (invoiceId) {
  return invoices[findinvoiceKey(invoiceId)];
};

function findinvoiceKey (invoiceId) {
  for (var key = 0; key < invoices.length; key++) {
    if (invoices[key].id == invoiceId) {
      return key;
    }
  }
};

var List = Vue.extend({
  template: '#invoice-list',
  data: function () {
    return {invoices: invoices, searchKey: ''};
  },
  computed : {
    filteredinvoices: function () {
    var self = this;
    console.log()
    return self.invoices.filter(function (invoice) {
      return invoice.name.indexOf(self.searchKey) !== -1
    })
  }
}
});

var invoice = Vue.extend({
  template: '#invoice',
  data: function () {
    return {invoice: findinvoice(this.$route.params.invoice_id)};
  }
});

var invoiceEdit = Vue.extend({
  template: '#invoice-edit',
  data: function () {
    return {invoice: findinvoice(this.$route.params.invoice_id)};
  },
  methods: {
    updateinvoice: function () {
      var invoice = this.invoice;
      invoices[findinvoiceKey(invoice.id)] = {
        id: invoice.id,
        name: invoice.name,
        description: invoice.description,
        price: invoice.price
      };
      router.push('/');
    }
  }
});

var invoiceDelete = Vue.extend({
  template: '#invoice-delete',
  data: function () {
    return {invoice: findinvoice(this.$route.params.invoice_id)};
  },
  methods: {
    deleteinvoice: function () {
      invoices.splice(findinvoiceKey(this.$route.params.invoice_id), 1);
      router.push('/');
    }
  }
});

var Addinvoice = Vue.extend({
  template: '#invoice-add',
  data: function () {
    return {invoice: {name: '', description: '', price: ''}
    }
  },
  methods: {
    createinvoice: function() {
      var invoice = this.invoice;
      invoices.push({
        id: Math.random().toString().split('.')[1],
        name: invoice.name,
        description: invoice.description,
        price: invoice.price
      });
      router.push('/');
    }
  }
});

var router = new VueRouter({
  routes: [{path: '/', component: List},
    {path: '/invoice/:invoice_id', component: invoice, name: 'invoice'},
    {path: '/invoice-add', component: Addinvoice},
    {path: '/invoice/:invoice_id/edit', component: invoiceEdit, name: 'invoice-edit'},
  {path:   '/invoice/:invoice_id/delete', component: invoiceDelete, name: 'invoice-delete'}
]});

new Vue({
  el: '#app',
  router: router,
  template: '<router-view></router-view>'
});

now you can run above example, comments about the script are really appreciated

 
by Luigi Nori Date: 23-05-2020 javascript vue.js vue crud hits : 13685  
 
Luigi Nori

Luigi Nori

He has been working on the Internet since 1994 (practically a mummy), specializing in Web technologies makes his customers happy by juggling large scale and high availability applications, php and js frameworks, web design, data exchange, security, e-commerce, database and server administration, ethical hacking. He happily lives with @salvietta150x40, in his (little) free time he tries to tame a little wild dwarf with a passion for stars.

 
 
 

Related Posts

Validating HTML forms using BULMA and vanilla JavaScript

Today we are going to write about contact forms and how to validate them using JavaScript. The contact form seems to be one of the top features of every basic home…

A FULFILLED PROMISE - Using the FETCH API to make AJAX calls

In this article we talked about what AJAX calls are and how to use them in a traditional way, by using the XMLHttpRequest (XHR) object. In short, thanks to AJAX…

How to use Parallax.js effect on your website

Today, we're going to write about the parallax effect, similar to parallax scrolling, and how to implement it to improve your landing page. In webdev, they say mobile first -…

How to make the website's dark mode persistent with Local Storage, CSS and JS

Recently we wrote about how to do a switchable alternative color mode or theme, a very useful and popular feature to websites. Today’s article is going to be about how…

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

JavaScript: Spread and Rest operators

In today’s article we are going to talk about one of the features of the ES6 version(ECMAScript 2015) of JavaScript which is Spread operator as well as Rest operator. These features…

Javascript: what are callbacks and how to use them.

Today we are going to learn about a concept that is widely used in javascript and that is used quite a lot by today's frameworks, libraries, especially NodeJS. This is…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The package managers npm and yarn: main differences

Npm and yarn are package managers that help to manage a project’s dependencies. A dependency is, as it sounds, something that a project depends on, a piece of code that…

The Javascript asign() method to merge and clone objects

In this article, we will be covering Object.assign()method in javascript in detail with examples. A javascript object is a collection of key-value pairs. Keys are also known as properties of object. Keys…

All the javascript functions and methods to manipulate arrays

This article will show that the prominent JavaScript array functions are .map(), .filter(), and .reduce(), and will then go through examples of instances in which .every() and .some() would save…

TypeScript: The evolution of JavaScript

When you're involved in the development of a large project, programming languages like JavaScript don't seem to be the best solution. Their lack of elements such as Language Aids has…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration