Unable to store the user's input information in the database

I've been attempting to incorporate an "add" button feature that saves/adds/inserts data into my table. I have inputted some text in the form of tags (input type), but unfortunately, it doesn't seem to be functioning properly. Despite conducting a Google search, I haven't been able to find a solution for what I'm attempting to achieve.

Below is the code snippet from my Vue.js file:

<template>
  <div>
    <b-table striped hover :items="items" id="table">      
    </b-table>
    No Product: <input type="text" id="no_product" v-model="items.product_no" /><br>
    Product Name: <input type="text" id="nama_product" v-model="items.product_name" /><br>
    Product Price: <input type="text" id="harga_product" v-model="items.product_price" /><br>
    Product Quantity: <input type="text" id="qtt_product" v-model="items.product_qty" /><br>
    Line Total: <input type="text" id="line_totall" v-model="items.line_total" /><br>

  <br><br>
    <b-button @click="addRow">
    <i class=""></i>Add Row
    </b-button>
</div>

</template>

And here is the script associated with it:

<script>
export default {
  data(){
    return {
      items: [{
            product_no: '1',
            product_name: 'box',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
        }, {
          product_no: '1',
            product_name: 'box',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
        }]
    }

  },
  methods: {
   addRow() {
    var noprod = document.getElementById('no_product').value;
    var nprod = document.getElementById('nama_product').value;
    var hprod = document.getElementById('harga_product').value;
    var qtyprod = document.getElementById('qtt_product').value;
    var lineprod = document.getElementById('line_totall').value;

    var table = document.getElementsByTagName('b-table')[0];

    var newRow = table.insertRow(table.rows.length);

    var cel1 = newRow.insertCell(0);
    var cel2 = newRow.insertCell(1);
    var cel3 = newRow.insertCell(2);
    var cel2 = newRow.insertCell(3);
    var cel3 = newRow.insertCell(4);

    cel1.product_no = noprod;
    cel2.product_name = nprod;
    cel3.product_price = hprod;
    cel4.product_qty = qtyprod;
    cel5.line_total = lineprod;
    console.log(print)

      },
    }
}

</script>

Answer №1

If you want to implement this using the vue system without vanilla JavaScript, you can follow this approach:

const app = new Vue({
  el: '#app',
  data: {
    items: [
      {
        product_no: '1',
        product_name: 'box',
        product_price: '10000',
        product_qty: '5',
        line_total: 0
      }, 
      {
        product_no: '1',
        product_name: 'box',
        product_price: '10000',
        product_qty: '5',
        line_total: 0
      }
    ],
    newItem: {
        product_no: '',
        product_name: '',
        product_price: '',
        product_qty: '',
        line_total: null
    }
  },
  methods: {
    addRow () {
      this.items.push(this.newItem)
      this.cleanForm()
    },
    cleanForm () {
      this.newItem = {
        product_no: '',
        product_name: '',
        product_price: '',
        product_qty: '',
        line_total: null
      }
    }
  }
})
<!-- Include necessary Bootstrap and BootstrapVue CSS files -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" />

<!-- Include Vue and BootstrapVue scripts -->
<script src="//unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>

<div id="app">
  <b-table striped hover :items="items">
  </b-table>
  Product Number : <input type="text" v-model="newItem.product_no" /><br>
  Product Name : <input type="text" v-model="newItem.product_name" /><br>
  Product Price : <input type="text" v-model="newItem.product_price" /><br>
  Product Quantity  : <input type="text" v-model="newItem.product_qty" /><br>
  Line Total : <input type="number" v-model="newItem.line_total" /><br>
  <br><br>
  <b-button @click="addRow">
    <i class=""></i>Add Row
  </b-button>
</div>

Answer №2

Consider this as a useful solution.

new Vue({
  el: '#app',
  data: {
    items: [{
            product_no: '1',
            product_name: 'kotak',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
      }, {
          product_no: '1',
            product_name: 'kotak',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
     }],
     counter: 3
  },
  methods:{
    addTableRow: function () { 
       var obj = {
        product_no: this.counter,
        product_name: this.counter,
        product_price: this.counter,
        product_qty: this.counter,
        line_total: this.counter
      };
      this.items.push(obj); 
      this.counter++;
   }
  } 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
 <table>
   <tr><th>Table</th></tr>
   <tr v-for="item in items">
     <td>{{ item.product_no }}</td>
     <td>{{ item.product_name }}</td>
     <td>{{ item.product_price }}</td>
     <td>{{ item.product_qty }}</td>
     <td>{{ item.line_total }}</td>
   </tr>
 </table>
 <button @click='addTableRow()'>Add New Row</button>
</div>

Answer №3

Insert Temporary Data

Whenever a new row is added, the data is taken to the items array and temporary data is set

<template>
  <div>
    <b-table striped hover :items="items" id="table">

    </b-table>
    Product Number : <input type="text" id="no_product" v-model="temp.product_no" /><br>
    Product Name : <input type="text" id="nama_product" v-model="temp.product_name" /><br>
    Product Price : <input type="text" id="harga_product" v-model="temp.product_price" /><br>
    Product Quantity : <input type="text" id="qtt_product" v-model="temp.product_qty" /><br>
    Line Total : <input type="text" id="line_totall" v-model="temp.line_total" /><br>

  <br><br>
    <b-button @click="addRow">
    <i class=""></i>Add Row
    </b-button>
</div>

</template>

Below is the corresponding script

<script>
export default {
  data(){
    return {
      temp: {
            product_no: '',
            product_name: '',
            product_price: '',
            product_qty: '',
            line_total: 0,
        },
      items: [{
            product_no: '1',
            product_name: 'box',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
        }, {
            product_no: '1',
            product_name: 'box',
            product_price: '10000',
            product_qty: '5',
            line_total: 0,
        }]
    }

  },
  methods: {
   addRow() {
    var table = document.getElementsByTagName('b-table')[0];

    var newRow = table.insertRow(table.rows.length);

    var cel1 = newRow.insertCell(0);
    var cel2 = newRow.insertCell(1);
    var cel3 = newRow.insertCell(2);
    var cel4 = newRow.insertCell(3);
    var cel5 = newRow.insertCell(4);

    cel1.product_no = this.temp.product_no;
    cel2.product_name = this.temp.product_name;
    cel3.product_price = this.temp.product_price;
    cel4.product_qty = this.temp.product_qty;
    cel5.line_total = this.temp.line_total;
    console.log(print);

    this.temp = {
            product_no: '',
            product_name: '',
            product_price: '',
            product_qty: '',
            line_total: this.items.length,
        }

      },
    }
}

</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When incorporating reduce into your code, remember to expect an undefined

Imagine you have an array like this: const novels = [ { id: 1, name: 'The Da Vinci Code', genre: 'Mystery', author: { name: 'Dan Brown', birthYear: 1964, }, releaseYear: 2003, }, { ...

The art of slipping away from a character in JavaScript while adding HTML using jQuery

I am trying to add multiple HTML canvas elements to an existing HTML canvas. <div> <canvas id="BackGroundImage" width="800" height="300" style="position: absolute; z-index: 0;"></canvas> <canvas id="canvas" width= ...

Tips on causing a forEach loop to pause for a regex substitution to occur

I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to ...

Removing a CSS class using JQuery

Within my website layout, I have a div that is dynamically included using PHP. This div is inserted in two different locations, each inside its own parent div. <div id="parent_div"> <div id="contact_details_div" class="contact_details_div sam ...

Vetur is experiencing issues with template functionality, such as not properly checking data and methods

I have incorporated the vetur extension into my Visual Studio Code for a Vue project using TypeScript. I recently discovered that vetur has the capability to perform type checks within the template tag for props, methods, and even variables. However, in ...

Transferring data securely via URLs

I need advice on securing a JavaScript function that passes values into URLs in order to navigate to another page. What precautions should I implement to prevent manipulation of this process? This is the code snippet I am currently using: window.location ...

Can you explain the functionality of the "update:model-value" directive in Vue and how it operates?

I searched extensively to find an example that would help me understand the code below, but unfortunately, I was unsuccessful. If you could provide a link where I can learn more or demonstrate an example with both parent and child components, I would gre ...

What could be causing PHP to fail to send form scores to the PHP MyAdmin database?

I recently ventured into web development and took on a college project website where I built a form in HTML containing various radio button questions. To handle the validation of answers against the correct ones, I utilized JavaScript. With some assistance ...

Challenges with rendering items in a FlatList component in React Native

I'm currently working on transferring data from a data.json file to video.js <Image source={{ uri: video.snippet.thumbnails.medium.url }} style={{ height: 200 }} /> and I want this file to be rendered in a flatlist <FlatList showsHorizo ...

Causing a click event to occur results in crashing the browser

Take a look at this link: example on jsfiddle Why does the click event keep triggering multiple times when a div is clicked, eventually causing the browser to crash? What could be causing this issue and how can it be resolved? The actual div contains a l ...

How can I retrieve a data field with a colon in its key using Cytoscape.js?

After diligently going through the official documentation and following the steps in the tutorial, I have successfully managed to access a node's data field and use it for labeling, as long as the key is a simple string. However, my data will contain ...

What is your preferred datepicker for Vue.js 2?

Can anyone recommend a Vue.js 2.0 date picker component that meets my specific needs? I'm in search of one with the following requirements: Ability to select a range - start date and end date. Easily customizable using CSS. Capability to trigger a m ...

Error: discord-webhooks causing SyntaxError due to an unexpected identifier in JavaScript code

I am currently working on a javascript project to set up a webhook for Discord. The URL has been removed for privacy reasons. const DiscordWebhook = require("discord-webhooks"); let myWebhook = new DiscordWebhook("removedtopostonstackexchange") myWebhook. ...

Utilizing jQuery to efficiently chunk JSON data through AJAX calls

Is there a way to efficiently handle the retrieval of large amounts of JSON data through an ajax request? I am looking for a jQuery or JavaScript function that can assist with "chunking" the data. For example, I would like the ability to continuously rece ...

The JavaScript require() function appears to be malfunctioning

Why am I encountering an error when trying to import a valid node_module? <script > var Twit = require('twit'); </script> Error: Uncaught ReferenceError: require is not defined I am puzzled as to why the require() function wor ...

A step-by-step guide on harnessing the power of RPG.J

Attempting to utilize the RPG.js library as it appears to be quite powerful. I checked out the official tutorial but am struggling to understand certain aspects, particularly State 4: "Initialize the canvas in your JS file." Any guidance on which one to ...

Troubleshoot: Issue with Multilevel Dropdown Menu Functionality

Check out the menu at this link: The issue lies with the dropdown functionality, which is a result of WordPress's auto-generated code. Css: .menu-tophorizontalmenu-container { margin: 18px auto 21px; overflow: hidden; width: 1005px; ...

What is the process for generating a GET request for selected checkboxes and subsequently showcasing the data on an HTML webpage?

Currently working on an app project and need assistance with implementing a feature. I have successfully set up a POST method for the checkboxes that are checked, but I am unsure how to retrieve this data and display it on my HTML page using a GET method ...

Retrieving an Element that Triggered an HTTP GET Request in Node.js with Express

In my Express front-end development project, I have a table with rows containing links that trigger GET requests. These requests are sent to the back-end, which is created using node.js, in order to retrieve files corresponding to each row. All links follo ...

Tips for programmatically choosing images from a Google Photos collection?

Currently, I am attempting to use code in the console to automatically choose a photo from a Google Photos Album while browsing. I've tried this method so far: const photo = document.getElementsByClassName('p137Zd')[0].parentElement photo. ...