Establishing an animated starting point for a template variable through Vue JS

Using a content management system, I am designing a front-end form that allows theatre directors to input multiple showtimes. There is a button they can click to add another input field if needed (side note: I am still figuring out how to implement a 'remove last' button using Vue, but that's for another time).

I have successfully implemented the addition of new fields. However, when a director returns to edit the showtimes they created, I need the Vue counter to start at the number of existing fields. For example, if they already defined 3 showtimes (0,1,2), the first dynamically placed field with Vue should begin at 3.

Alternatively, I am considering generating an array of existing data in Vue when the page loads, but I am unsure of where to begin with this approach.

Below is my HTML and Vue JS:

new Vue({
  el: '#app',

  data: {
    timeslots: [
      {
        count: 0
      }
    ],
    count: 0
  },

  methods: {
    addAnother: function(){
      this.timeslots.push({
        count: ++this.count
      });
    }
  }
});
<div id="app">
  {{ performances }}
    <input name="performances[{{ zero_index }}][showtime]" value="{{ showtime }}">
  {{ /performances }}

  <template v-for="slot in timeslots">
    <input name="performances[@{{ slot.count }}][showtime]" value="{{ now }}">
  </template>
  
  <div>
    <button @click.prevent="addAnother">Add another</button>
  </div>
</div>

Any guidance on how to proceed would be greatly appreciated!

Answer №1

I managed to solve the problem on my own in a quicker time than expected by utilizing the second method I had speculated about in my initial post. Instead of using an arbitrary integer like 'count', I used $index. The only downside is that this method necessitates the JavaScript code to be directly inserted into my template, as opposed to being stored in a separate file. However, this arrangement suits me just fine.

<div id="oven">
  <template v-for="biscuit in oven">
    <div>
      <input type="text" name="performances[@{{ $index }}][showtime]" value="@{{ biscuit.showtime }}">
    </div>
  </template>
  <span class="add small black btn" @click="addAnother"><i class="fa fa-plus-circle"></i> Add another</span>
</div>

<script>
new Vue({
  el: '#oven',

  data: {
    oven: [
      {{ performances }}
      {
        showtime: "{{ showtime }}"
      },
      {{ /performances }}
    ]
  },

  methods: {
    addAnother: function(){
      this.oven.push({
        showtime: '{{ now modify_date="+2 months" format="Y-m-d" }} 7:30 PM'
      });
    }
  }
});
</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

Error: selenium web driver java cannot locate tinyMCE

driver.switchTo().frame("tinymce_iframe"); String script="var editor=tinyMCE.get('tinymce_textarea');"; JavascriptExecutor js=(JavascriptExecutor) driver; js.executeScript(script); I'm encountering a WebDriverException while try ...

Issue with jQuery script detected on website

On my website, I have implemented two controls that are bound to jQuery. One control is a button used to copy the value of a textbox to the clipboard, while the other is a textbox for entering dates. The issue arises when debugging the web application as ...

Can images be accessed and displayed on an HTML page by opening a directory and reading file data using Javascript?

Is it feasible in JavaScript to access a directory, read an image file, and display it in HTML? While it may not be possible to directly open files in a directory using JS, I am looking for a way to achieve the following: I have an XML file that includes ...

MongoDB's conditional aggregation function allows users to manipulate and aggregate data based

My mongodb contains data like this: { "_id": "a", "reply": "<", "criterion": "story" }, { "_id": "b", "reply": "<", "criterion": "story" }, { "_id": "c", "reply": ">", "criterion": "story" } What I need is the following result: ...

What steps can be taken to disable the send button once it has been used and then automatically show gratitude?

My website features a contact form, but I noticed that when users press the send button, there is no confirmation message indicating whether the message was successfully sent. This could potentially lead to multiple spam emails being sent to me. How can I ...

Significant lag in *ngIf caused by the http.get request

<mat-spinner *ngIf="loading === true" class="tableLoading"></mat-spinner> <div *ngIf="loading === false"> ... </div> It's puzzling to me that the spinner takes significantly longer to ...

Manually validate inputs in Angular

I've encountered an issue with the bootstrap datepicker. When I click on the date icon, the date and time automatically populate in the input field. However, the input remains invalid because I haven't directly interacted with it. Has anyone else ...

Presenting data fetched from Firebase database in an HTML table format

I recently started working with Firebase and I'm attempting to fetch static data from my Firebase Database to showcase in a tabular format on an HTML page. Below is the structure of the HTML code I've implemented: <section> <table> ...

How come ng-click isn't triggering in the components' template?

Embarking on a new Angular project has presented me with some challenges in understanding the basics. One issue I'm facing is getting ng-click events to work properly within my component model. Despite following various solutions from Stack Overflow, ...

ng-select will solely output the term 'collection'

i am having an issue with a ng-select in my contact form. everything is being received correctly except for the value of the ng-select. Instead of getting the selected option from the ng-select, the system just returns the word "array". Below is the port ...

Encountering 404 Error in Production with NextJS Dynamic Routes

I'm currently working on a next.js project that includes a dynamic routes page. Rather than fetching projects, I simply import data from a local JSON file. While this setup works well during development, I encounter a 404 error for non-existent pages ...

Issues encountered when trying to set JqxCombo values from an ajax response within a Vue.js environment

I've been experimenting with integrating JqxWidgets into Vue.js. The concept is to have multiple ComboBox elements in a form and simply call the ComboBox template while providing an ajax call to get and set data for that specific combobox. So far, th ...

Laravel Mix causing issues with Vue loading when extract() is used

I currently have a simple webpack.mix.js and resources/js/app.js setup in my Laravel project. Here is the code from webpack.mix.js: const mix = require('laravel-mix'); mix.js('resources/js/app.js', 'public/js') .sass(&ap ...

Using ExpressJS and Jade to submit a form using the POST method and redirecting to a specified path

I am exploring node, express and jade for the first time and working on a small application that requires users to enter their name and password in a form. The app then redirects them to a path based on their username. Here is the code snippet to achieve ...

Tips for displaying a script file from a remote server on your PHP page using AJAX

I'm currently working on executing a script from a remote server and displaying the output on an HTML page using AJAX for user visibility. I managed to do this successfully on my local server with up.sh and down.sh scripts. However, when attempting th ...

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

Pull in content or data from a separate page by leveraging AJAX

Hello, I'm just starting out in programming and I could really use some help with this issue. index.html <button id="press">Click here</button> <div id="show_image"> </div> <br> <div id="appended_area"></div&g ...

"Troubleshooting a minor jQuery problem - update on current URL and refresh the page

I have developed a jQuery script that is designed to search through a webpage for specific text and then perform an action based on the findings. My query is straightforward. If I want to apply this script to a current website, such as www.google.com, how ...

Enhance Your Scene with Three.js Object Layering

Currently, I am showcasing three objects on the screen that can be selected by a click (I am only changing their color upon clicking at the moment). My goal is to have the selected object come to the forefront of the camera when left-clicked (including rot ...

Change the selection box to a checkbox

I am struggling to retrieve the value of a selection box inside jQuery. Specifically, I need to access the selection box value within a function that triggers on a checkbox change event: This is what I have so far: $('input').on('change& ...