I'm facing a challenge with displaying data on a dynamically created table using VueJS

I'm having an issue with dynamically generating a table using VueJS. The problem arises when creating the <th> elements.

In order to set them up, I have a Vue component that is called by the addRow function. This component uses templates with values extracted from two input fields named "Please add service" and "Please enter price". These values are then added to an array in the data object and cleared dynamically to allow for more items to be added.

Upon logging the array, I noticed that the objects I input remain stored regardless of how many items are added. However, their textual content does not appear in the new table rows.

After some research, my only speculation is that the elements may not be reactive. Despite having getters and setters, there seems to be no change. Another thought is that this issue might be related to scope.

I would appreciate any further advice on resolving this issue.

For reference, here is the full code: https://codepen.io/MrYY/pen/PeJjZy

Below are the Vue instance and Vue component:

Vue.component('table-row', {
    template: '\<tr>\
    <th>\
      {{ this.name }}\
    </th>\
    <th>\
      {{ this.price }}\
    </th>\
    \</tr>\
  ',
  props: ['row']
 })

var app = new Vue({

    el: '#app',

    data: {
        companyName: "Company Name",
        repName: "Representative's name",
        phone: "+359-00-000-0000",
        newService: "Please add service",
        price: "Please enter price",
        services: [

        ]
    },

    methods: {

        addRow: function () {
            this.services.push({
                servicesName: this.newService,
                price: this.price
            });
            this.newService = "Please add service";
            this.price = "Please enter price";
        }
    }

})

Answer №1

To enhance your component, simply include the service as a prop.

<tr is="table-row" v-for="service in services" :service="service">

Vue.component('table-row', {
    template: '\<tr>\
    <th>\
      {{ service.servicesName }}\
    </th>\
    <th>\
      {{ service.price }}\
    </th>\
    \</tr>\
  ',
  props: ['service']
 })

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

Is it possible to continuously divide or multiply numbers by 2 without encountering any rounding errors when working with floating point numbers?

binary can only represent those numbers as a finite fraction where the denominator is a power of 2 Are calculations done in binary/floating point format still accurate, even after multiple additions or multiplications by 2 without any rounding errors? co ...

Obtain date and currency formatting preferences

How can I retrieve the user's preferences for date and currency formats using JavaScript? ...

What is the process for obtaining intersection set data from an array?

I'm trying to find the intersection set within an array only containing type 1 and 2. Can you help me with that? var arr = [ { id: 1, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, ...

Beware: The use of anonymous arrow functions in Next.js can disrupt Fast Refresh and lead to the loss of local component state

I am currently encountering a warning that is indicating an anonymous object in a configuration file, and even specifying a name for it does not resolve the warning. Below you will find the detailed warning message along with examples. Warning: Anonymous ...

When a button is clicked, load two separate pages into two distinct divs at the same time

$('#menuhome').click(function(){ $('#leftcolumncontainer').load('pages/homemenu.php'); }); the code above is used to load the home menu on the left side. Now, let's add the following: $('#menu ...

Press on a specific div to automatically close another div nearby

var app = angular.module('app', []); app.controller('RedCtrl', function($scope) { $scope.OpenRed = function() { $scope.userRed = !$scope.userRed; } $scope.HideRed = function() { $scope.userRed = false; } }); app.dire ...

Developing a custom logging middleware with morgan

I am trying to develop a middleware using morgan. I have attempted to export the middleware function and then require it in app.js. However, I am facing an issue where it is not working as expected. Middleware: const morgan = require('morgan&apos ...

How does React determine if a component is a class component or a functional component?

Within my React application, I have successfully developed both a class component and a functional component. However, I am curious about how React is able to distinguish between the two when calling them. Can you shed some light on this distinction? This ...

What could be causing my unit test to fail when trying to retrieve the text from a single-file component that was set using $el.innerText within the mounted hook?

Description of the Issue There is an editable div element that receives a default value of an empty string for its content property. Instead of using mustache syntax to display the content like this: {{ content }}, I set the innerText of the element using ...

Ways to close jQuery Tools Overlay with a click, regardless of its location

I have integrated the Overlay effect from jQuery Tools to my website, with the "Minimum Setup" option. However, I noticed that in order to close it, the user has to specifically target a small circle in the upper right corner which can affect usability. It ...

What is the method for executing a function enclosed within a variable?

As someone new to the world of Java, I have encountered a puzzling issue with some code related to a game. Specifically, there seems to be an obstacle when it comes to utilizing the navigator function. When I click on this function in the game, some sort o ...

Automatic closing of multile- vel dropdowns in Bootstrap is not enabled by default

I have successfully implemented bootstrap multilevel dropdowns. However, I am facing an issue where only one child is displayed at a time. <div class="container"> <div class="dropdown"> <button class="btn btn-default dropdown-to ...

Encountering an illegal invocation type error when clicking on the search icon within a Vue component

I keep encountering an "illegal invocation" error in the console whenever I try to click on the search icon. 'focus' called on an object that does not implement interface HTMLElement. This error seems to occur intermittently. <div class=" ...

Updating Object Properties in Vue.js 2.0 using Methods

I am facing an issue where I have an object with blank properties in my component's data. I want to update these properties using a method that is triggered by a click event on one of the listed elements. However, when I check the click event in the c ...

Switching icon when clicking on a Vue button

Is it possible to update the icon upon clicking a button in vue? Check out this code snippet: <v-btn flat icon color="white"> <v-icon>star_border</v-icon> </v-btn> Appreciate any guidance on this matter. ...

What is the behavior of a variable when it is assigned an object?

When using the post method, the application retrieves an object from the HTML form as shown below: code : app.post("/foo", (req, res)=> { const data = req.body; const itemId = req.body.id; console.log(data); console.log(itemId); }) ...

using post request axios to send parameters

I am looking to make a post request using axios with an object as the payload. employee:{ name: 'test', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d79687e794d6a606c6461236e6260">[email ...

Transferring variables from the $(document).ready(function(){}) to the $(window).load(function(){} allows for seamless and

Just think about if I successfully create percent_pass at the time of document.ready, how can I transfer that variable to window.load? $(document).ready(function() { jQuery(function() { var ques_count = $('.question-body').length; va ...

Button component in React JS fails to appear on iPhones

After building a website using reactjs, I encountered an issue where the landing page's begin button is not displaying correctly on iPhones. The website works fine on all other devices, but on iPhones, the button is barely visible - with only a faint ...

Storing additional data from extra text boxes into your database can be achieved using AJAX or PHP. Would you

A dynamic text box has been created using Jquery/JavaScript, allowing users to click on an "Add More" button to add additional input boxes for entering data. However, a challenge arises when attempting to store this user-generated data in a database. Assi ...