Here is a way to transfer the form data from the modal component to the submit handler

I am currently developing a VUE application that includes displaying a team table with information fetched from the backend using axios and django rest_framework. Everything is functioning properly.

However, I encountered an issue when clicking on "new category" which opens a modal with a form. Upon submission of the form, the "submitHandler" function is supposed to be triggered to send the form data to the backend using axios for posting. Unfortunately, I am unable to retrieve the form data within this function, which is necessary for the POST request.

I attempted using v-model on the modal form, but it resulted in a reference error.

Any assistance or guidance on resolving this issue would be greatly appreciated.

The modal component:

<script setup>
import { defineProps, defineEmits, ref } from "vue";
import { onClickOutside } from '@vueuse/core'

const offset = 6

const props = defineProps({
    isOpen: Boolean,
    isTest: String,
    Categorie: String,
    modalAction: String,
});

const emit = defineEmits(["modal-close"]);

const target = ref(null)
onClickOutside(target, () => emit('modal-close'))

</script>

<!-- The template code was not altered -->

And the corresponding view:

<script setup>
import { ref } from "vue";
import ModalComponent from "../components/ModalComponent.vue";
import axios from 'axios'
// Additional logic remains unchanged
</script>

<!-- The template code was not altered -->

</template>

<script>
// Additional script logic remains unchanged
</script>

Answer №1

To transfer the data, utilize emit within the table component

In the modal component, add another emit. Modify the following line accordingly

const emit = defineEmits(["modal-close"]);

change it to this

const emit = defineEmits(["modal-close", "submit"]);
.

Then create a function to trigger this event along with the data. As of now, your modal does not have a data model, you need to establish this first and connect it with v-model to the controls.

const submit = () => {
  emit('submit', data);
}

I noticed that you have replaced the footer slot, you can either revert back and call the submit function from the existing button, or provide the submit function as a scoped slot (v-bind on slot). This will allow you to also utilize it in your table component.

<slot name="footer" v-bind="{ submit }">
  <button @click.prevent="submit">Submit</button>
</slot>

In your table component, adjust the submitHandler function to accept one parameter, which will be the data.

const submitHandler = (data) => {
    // perform operations with axios
    // close the modal
}

If you prefer to have control over the submit button, ensure that the footer of the modal in the table component is structured like this:

<template #footer="{ submit }"><button @click="submit">submit</button></template>

Answer №2

with the help of @wbiller (BIG THANKS!)

After some critical thinking...the data seemed 'empty', but the solution was to create a responsive data object:

const data = reactive({ category: '' })

and in the modal, the correct data binding:

v-model="data.category">

Now I can see the updated data in the submithandler: {category: 'Onder-14'}

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 comparing two state arrays in React, it is important to note that they will not be considered equal and return a boolean (True

As I attempt to compare two arrays stored in my state, my goal is to set a boolean variable to "true" if they match. However, my current if statement fails to detect equality between the arrays. I'm performing this comparison within a setInterval() f ...

Tips for swapping out a specific string in an HTML webpage with a different string using JavaScript

For my HTML website, I am trying to replace one string with another using JavaScript. Specifically, within the nodeList "AuthorList," there is a string called "Test String" that needs to be changed to "new test." I have attempted various modifications of ...

Trick to enable editing in Bootstrap Select Combobox

Is there a way for users to add their own options to bootstrap-select? Greetings! I have spent some time searching for a straightforward solution that is compatible with Bootstrap 4 styling. Despite exploring various suggestions, as well as unresolved thr ...

CodePen experiencing issues with JS/jQuery coding functionality

Experimenting on CodePen, I am practicing using JS and jQuery with HTML. HTML: <button>asdasads</button> JS: $('button').on('click', function() { alert('woot'); }); Visit this pen, unfortunately it's ...

Navigating through Objects in Angular 9

I am facing a challenge in Angular 9/Typescript while trying to iterate through the object response from my JSON data. Despite searching for solutions, I haven't found any that work for me. In my JSON, there is a section called "details" which contain ...

Tips for executing a loop while waiting for a jQuery ajax request to complete

I currently have this code setup: for (var i = 0; i < $total_files; i++) { $.ajax({ type: 'POST', url: 'uploading.php', context: $(this), dataType: 'json', cache: false, contentType: false, pr ...

Experience the simplistic magic of the Vue.js/Vuefire/Firebase app world, though it struggles with reading values

Transitioning to Vue.js from SQL programming has been a bit of a challenge, but I'm getting there. Currently, I am using vuefire according to the specifications and even manually inserting the correct key from my Firebase database with one record and ...

Error in custom class using vanilla JavaScript: "Error: Unable to assign innerHTML to an undefined property"

Apologies for the ambiguous title, but I am struggling to pinpoint the exact issue at hand... I'm essentially guessing my way through this problem. This marks my initial venture into creating a reusable Javascript class. Below is a simplified version ...

Laravel mix is compiling the Vuetify styles in the sass file and generating an empty CSS file

I am having trouble compiling vuetify 2.0.0-beta.9 SASS using laravel mix. When I compile the styles.sass file, it generates an empty css file. How can I resolve this issue? Following the documentation (), I started by running: $ npm install sass sass-lo ...

React-file-viewer shrinks any document to a compact size

I have been searching extensively for information on how to adjust file sizing in react-file-viewer without any success. My objective is to utilize the react-file-viewer to allow users to click on a filename hyperlink and open the file (be it an image, do ...

What is the proper way to send an AJAX request with the data type set to

I am currently working on creating my own POST request. Below is the function I have written: function sendPost(o) { var h = new XMLHttpRequest(); h.onreadystatechange = requestComplete; function requestComplete() { if (h.readyState = ...

implementing a dropzone feature in Vue using Laravel Mix for image uploads

Currently, I am working on a Single Page Application (SPA) using vue.js and Dropzone for uploading images. Despite having the correct path, the images are not appearing, and I'm unsure of the reason why! view image description here as seen in the scre ...

Can you guide me on how to adjust the correct view on my controller?

After successfully inserting data using jQuery AJAX within a modal, I am encountering a problem where the view appears differently than expected. Below is a snippet of my AjaxCrudController: public function store(Request $request) { // $create = ...

JavaScript - the act of exiting functions

Is it necessary to explicitly return from a JavaScript function? Unlike in other languages where not returning can result in a stack overflow error, JavaScript seems to handle this differently. Furthermore, due to its asynchronous nature, determining when ...

I'm looking for a way to showcase tasks in Fullcalendar that don't have a set end date

Is there a way to display a task in the full calendar when the task has a start date but no defined end date, such as a promotion that lasts until stock runs out? I would like the timeline for this type of task to extend indefinitely. However, since such ...

Unable to retrieve information from the firestore database

When trying to fetch documents from the firestore, I encountered an issue where it returns an empty array. However, when I run console.log(docs); outside of the declared function, it actually shows the array data. This problem arises because my useEffect f ...

Preventing Shift: How to Only Replace the Chosen Vue Draggable Item Without Affecting Other Grid Items

Let's try an example to demonstrate: https://codesandbox.io/s/j4vn761455?file=/src/App.vue:112-116 Step 1: Drag item 4 to where 5 is. Notice how 4 and 5 switch positions. Step 2: Now, drag item 1 to position 6. Watch as 1 moves to where 6 was, pushi ...

Suggestions for updating ng-repeat variable with autocomplete functionality?

Many thanks to the Stack Overflow community for helping me resolve my previous angularjs and autocomplete issue. Here is the link to the question: Angularjs with jquery auto complete not working However, I am facing a similar problem now within the ng-rep ...

What is the best method for adding a header to a dynamically created table using JavaScript?

After receiving JSON data in my JavaScript, I have successfully converted it into an HTML table. Everything seems to be working fine, but I'm wondering how I can add a header to my table? Below is the code snippet: <script> $(document ...

Leveraging MongoDB imported documents within DerbyJS

I am facing a challenge with my MongoDB collection which was not saved through my Derby app. My goal is to query that data and pull it into my Derby app. After figuring out the solution, I have written the following code snippet: get '/:year', ...