Unleashing the power of data transmission from a dynamic form comprised of multiple components

Currently, I am developing an application that allows users to create task lists. These task lists are retrieved from the database once created, enabling users to interact with them by selecting an appropriate response for each task.

The possible responses are 'Yes' or 'No'. When a user selects 'No', a dropdown menu appears with reasons for not completing the task. Choosing the 'other' option will reveal a custom input box for specifying a different reason.

In addition to selecting responses, users can also add comments to each task. Once all tasks are completed, users can submit the form.

My main challenge lies in capturing the form responses due to the dynamic nature of the component displaying each task. While I have enclosed the component in a <form> tag to trigger form submission upon clicking, I am unsure how to extract the data from the component itself.

If you would like to explore the code and provide insights, please check out this CodeSandbox link.

App.vue

<template>
  <button @click="toggleComplete">Complete All</button>
  <form @submit.prevent="submitModal">
    <checklist-item
      v-for="taskList in myChecklist.tasks.data"
      :key="taskList.id"
      :taskDetails="taskList"
      :marked="markAll"
    >
    </checklist-item>
    <div class="">
      <button class="" type="submit">Submit</button>
    </div>
  </form>
</template>

... (The rest of the App.vue code as per original text) ...

ChecklistItem.vue

<template>
  ... (The ChecklistItem.vue template code as per original text) ...
</template>

... (The rest of the ChecklistItem.vue script + style sections as per original text) ...

Answer №1

If you want to update the values inside your myChecklist.tasks.data array, you can emit events from within the checklist-item component for each event.

methods: {
    updateData(property, value) {
      this.$emit("update-form-data", {
        id: this.taskDetails.id,
        property,
        value,
      });
    },
    customEventToggle(event) {
      this.updateData("reason", event.target.value);
      if (event.target.value === "other") {
        this.customReasonToggle = true;
      } else {
        this.customReasonToggle = false;
      }
    },
    disableSelect() {
      this.updateData("status", "YES");
      this.notCompletedToggle = false;
      this.customReasonToggle = false;
    },
    enableSelect() {
      this.updateData("status", "NO");
      this.notCompletedToggle = true;
    },
    setComment(event) {
      this.updateData("comment", event.target.value);
    },
}

Then, in the parent component, listen for the event and update your data array accordingly:

<checklist-item
      v-for="taskList in myChecklist.tasks.data"
      :key="taskList.id"
      :taskDetails="taskList"
      :marked="markAll"
      @update-form-data="
        this.myChecklist.tasks.data.find((task) => task.id === $event.id)[
          $event.property
        ] = $event.value
      "

You can access the updated sandbox here, which logs data to console upon clicking submit.

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 there a way to efficiently utilize a JavaScript function multiple times within a single page?

Here is the code snippet I am working with: Javascript: <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { var transition = &apos ...

Typescript enhances Solid JS by using the "as" prop and the "component" prop

Hey there, I've been experimenting with solid-js lately and I'm facing a challenge integrating it with typescript. My objective is to make my styling more modular by incorporating it within my components. type RelevantTags = Exclude<keyof ...

In the ASP.NET framework, users can download an Excel file using Response.End, accompanied by a JavaScript loader on the client side. While I have successfully implemented the loader, I

Is there a different approach to displaying or hiding a loader when using Response.write and Response.end, as the client script does not function properly after response.end()? ...

The JavaScript date picker is malfunctioning in the HTML editor, but it functions properly in Fiddle

I have a working format available in a JS fiddle. Here is the code I have used on my demo site: I created a new folder named "js" and placed datepicker.js inside it, then linked it in my HTML like this: <script type="text/javascript" src="js/datepicke ...

What sets apart defining a function in ReactJS using only parentheses versus curly braces within parentheses?

As a newcomer to React, I encountered an interesting application that had functions defined in two different ways. One style was async function (a, b) => {//body}, which I found easy to understand. However, another set of functions followed the struct ...

Accessing `this.$refs`, `this.$router`, and `this.$route` using the Vue 2 Composition API

Is there a way to access this.$refs, this.$router, and this.$route in the setup function of Vue 2 Composition API? I've seen suggestions using context.$root, but it appears that $root is no longer accessible with context. Any guidance on this issue wo ...

Error: You can't call .text as a function in jQuery

While trying to retrieve the text from my td, I encountered the following error: $tds[2].text is not a function. The output of console.log('td',$tds[2]) shows: https://i.stack.imgur.com/OUngP.png $(document).ready(function() { $trs = $(&ap ...

What is the best way to calculate the product of two variables in JavaScript when my form is contained within a for each loop?

This form is part of a foreach loop and I'm trying to calculate the product of two inputs, "qty" and "uprice", for each row. However, currently only the result from the first row is being displayed. Form <tbody> <?php foreach($_POST[ ...

Sencha ExtJs[4.2]: Is it possible to selectively disable a single action button in the handler function for a column item?

Here is a code snippet example: Ext.create("Ext.tree.Panel", { renderTo: $(".gsBasciInfo")[0], store: "basic_grid_store", useArrows: true, rootVisible: false, columns: { items: [{ text: 'id', d ...

Checkbox click triggers a delayed update to the array

In my attempt to develop an array of user permissions that can be managed through a series of checkboxes, I encountered an issue. When I select a checkbox and use console.log() to display the array, it shows all the permissions I have checked. However, upo ...

Creating a custom toJSON function for a property declared using Object.defineProperty

Let's consider a scenario where we have an object with a specific property that is meant to reference another object, as shown below: Object.defineProperty(parent, 'child', { enumerable: true, get: function() { return this._actualCh ...

Load HTML content dynamically from a template

Having some difficulty creating a popup directive where I want to retrieve a template from the server, insert it into a popup div, and display it on the screen. Additionally, I would like these popups to have their own controllers. Unfortunately, it is not ...

Control the access to shared resources when dealing with asynchronous functions in JavaScript

Currently, I am developing a node.js server script that will utilize a shared text list for multiple clients to access asynchronously. Clients have the ability to read, add, or update items within this shared list. static getItems(){ if (list == undef ...

Combining Files in Angular 2 for Optimal Production Deployment

What is the best method for packaging and combining an Angular 2 application for production? My goal is to have an index.html file and a single app.min.js file. Although the Angular 2 documentation mentions using webpack, I found it to be overly complex f ...

What is the best way to validate a value with the help of the $.post functions in JavaScript?

An example of a script: <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script> <script type="text/javascript"> $(document).ready( function() { function validateInput( value ) { ...

Express-hbs: Dynamic Helper Function with Additional Features

I am currently utilizing express-hbs and Async Helpers in my project. However, I am facing an issue with passing options to the helper as async helpers do not seem to support this feature (or maybe I am unaware of how to do it correctly). In the code snipp ...

What is the best way to add an array to my JSON object in Javascript?

I'm currently in the process of formatting an array into a JSON object for API submission. Struggling to find the right method to transform my array into the desired structure. This is what my array looks like: data: [ ["Lisa", "Heinz", "1993-04 ...

Why isn't mapStateToDispatch sending the correct value? The unexpected behavior is causing issues

I've been experimenting with sending actions from my component to update the state. Normally, I include all my action creators in the connect function like this - connect(mapStateToProps,{actions})(component). However, after watching a tutorial on te ...

Utilizing jQuery to explore a JSON object from Google Books

I successfully retrieved a list of books using the Google Books API. I stored the JSON object in localStorage and then converted it back into an object: var books = JSON.parse(localStorage.books); Now, I am looking to retrieve information for a specific ...

What is causing .addClass() and .removeClass() to not function as anticipated?

Despite my extensive online research on Google and StackOverflow, I couldn't find a similar situation in about 2 dozen questions. This led me to post a new question to seek clarification. My goal is not to get this working; rather, I am focused on un ...