Tips for adding templates dynamically in Vue 3

I'm working on implementing a Vue 3 component that can dynamically insert rows into a table. This is what I have so far:

 <template>
    <table>
        <tbody>
            <tr v-for="(item, index) in tableRows" :key="item.id">
                <td>D{{item.id}}</td>
                <td><input type="text" v-model="item.Filename"></td>
                <td><input type="text" v-model="item.ImageTitle"></td>
                <td><input type="text" v-model="item.Caption"></td>
            </tr>
        </tbody>
    </table>
   <button @click.stop="insert_Row">add row +</button>
   
   <div v-for="item in tableRows" :key="item.id">
     {{item}}
   </div>  
</template>
    
<script>
export default {
  data() {
    return {
    tableRows: [
      {
        "id": 1,
        "Filename": "test",
        "ImageTitle": "test",
        "Caption": "test"
      }
    ]
    }
  },
  methods: {
    insert_Row() {
      this.tableRows.push(
        {
        "id": this.tableRows.length+1,
        "Filename": "",
        "ImageTitle": "",
        "Caption": ""
      }
      )
    }
  }
}
</script>

However, the current setup includes an initial row by default before clicking the add button. I want to make it so that the first row is also dynamically added when the button is clicked. Any suggestions on how I can achieve this?

Answer №1

It might be a bit of an oversimplification, but if you prefer not to display any table rows until the button is pressed, why not just delete the dummy data row from your tableRows array?

Replace this:

data() {
    return {
        tableRows: [
            {
               "id": 1,
               "Filename": "test",
               "ImageTitle": "test",
               "Caption": "test"
             }
        ]
    }
},

with this:

data() {
    return {
        tableRows: []
    }
},

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

HTML Image Mapping Up Close

Is there a way to magnify an HTML Image Map with zoom-in functionality similar to jQZoom? I attempted to modify the jQZoom code, but it resulted in losing the Image Map links and only linking to the enlarged image. Any suggestions or assistance would be ...

Issues encountered when parsing JSON data file

I'm facing an issue with reading a json file in JavaScript. I've tried the code below, but it generated an error: Uncaught SyntaxError: Unexpected token ':' <script> function loadJSON(callback) { var xobj = new X ...

using JavaScript to strip away content following .jpg

var lochash = "#http://www.thepresidiumschool.com/news_image/102%20NRD_7420.jpg&lg=1&slide=0"; I am looking to eliminate or modify the content that comes after .jpg. ...

Updating a property in an object within an Angular service and accessing it in a different controller

I am currently utilizing a service to transfer variables between two controllers. However, I am encountering difficulties in modifying the value of an object property. My goal is to update this value in the first controller and then access the new value in ...

What is the best way to send a JavaScript variable to PHP for storage in a WordPress database?

I am currently in the process of developing a star rating system within Wordpress and am looking to store the rating data in the Wordpress database. To achieve this, I have saved the star rating PHP code as a template within my Wordpress theme folder. Belo ...

Extracting the value of *data* from my HTML and displaying it in the console with Javascript

I'm struggling to figure out what's going wrong with this code. I've done some research online and it seems like I should just include the window.onload = function() at the beginning of my code. However, no matter what I try, the value alway ...

Is it necessary to use Hapi.js on the client side in order to establish a websocket connection using the Hapi.js protocol?

Currently, I am in the process of developing an API using Hapi and requiring WebSocket functionality. After some research, it appears that Nes is the preferred choice to integrate with Hapi for this purpose. Fortunately, Nes simplifies the process signific ...

What could be causing the Contact anchor element to not function properly?

I've been working on creating a dropdown menu for my API, but I'm having trouble getting the anchor links to function properly. I even attempted changing the "a" element to an onclick call with javascript:void(0) and added a function to open Gmai ...

Saving drag and drop positions in Angular to the database and troubleshooting screen resizing problems

Currently, I'm working on an application that involves dragging and dropping a DIV onto an image. My goal is to save the X and Y coordinates of the dropped DIV to the database so that when the user returns, the position will remain the same. The chal ...

Displaying the output of a PHP script in an AJAX request

I am trying to display feedback from certain checks, like checking if a file already exists, after making an ajax call to upload a file using my script. However, for some reason, I can't get the response to show up. Why is the response not appearing? ...

Issue with Jade template losing proper indentation

I have created a jade email template that includes a mixin file with the header mixin. This mixin should be present in all emails. Although it is included properly, the issue arises due to the nesting level within it (3 levels deep), causing anything place ...

What is the best way to retrieve the data response from an ajax request?

I am looking for help on how to preview the data received from my ajax request. I need to verify if the data is correct before proceeding. Below is the code snippet in question: <script type="text/javascript> var globalBase_Url = "{$ba ...

Using Java Script variables within Ruby code can be achieved by properly integrating the two languages through

Is there a way to incorporate a JavaScript variable into Ruby code? I attempted the following code, but it does not seem to be functioning properly. <script> var total = 100 <%@final_amount + = total%> </script> An error is being thr ...

What is the best method to ensure that none of the select option values are left empty?

I have a total of 5 select option drop down menus currently, but this number may increase in the future depending on requirements. The issue I'm facing is that when I select the last element, it returns true even though the other elements are empty. I ...

Exporting and importing modules in Angular 1 using Webpack 2

Seeking clarification on why the current implementation is not working as expected. It seems like a simple oversight, especially since it works fine without Webpack. I prefer to stick with the current implementation, where each component/controller/etc is ...

What is the best way to delay the rendering of my Vue component until a function call is finished?

After extensive research online, I have been unable to discover a specific solution to my issue. The problem is that I have a Vue component that relies on an init call and I need it to avoid rendering until the call is successfully completed. Once the ca ...

Navigating with Plone and AngularJS路径

Hello, I'm currently diving into AngularJS and experimenting with building an AngularJS based Plone addon. One challenge I'm facing relates to routing. Let's say I have a Plone URL, like http://localhost/blah/my_page When a user clicks on ...

Having difficulty rendering a child component within the main Component through iteration

I created a main component called App.js import "./styles.css"; import { Component } from "react"; import Item from "./components/Item"; class App extends Component { constructor(props) { super(props); this.state = ...

The process of organizing and arranging the content that appears on a webpage is in

Seeking a solution to achieve a similar effect like this example. Wanting the sections to transition nicely when clicked on. Should I use a Jquery plugin or implement it with CSS? ...

The AngularJS change event is not being activated

I am a beginner with angular js and I have implemented a bootstrap calendar in my application. However, I am facing an issue where the change event is not being triggered when the month changes, no matter where I place it within the code. Here is the snip ...