Updating parent data after filtering props in a child component: A comprehensive guide

After filtering the month in the child component, I am trying to update the data in my parent component. However, when I attempt to download excel data using vue-json-excel, the filtered month in the parent component refuses to update.

I have experimented with using onChange and emitting events with data in the parent component, then updating the data. I have also attempted to change the data using computed properties, but to no avail.

## Parent (App.vue) ##
<template>
   </div> 
    <TodosList
      v-bind:todos="todos"
      :editedTodo="editedTodo"
      :selectedMonth="selectedMonth"
      :selectedYear="selectedYear"
      :months="months"
      :years="years"
      :json_data="json_data"
      :STORAGE_KEY="STORAGE_KEY"
      @change="changeSelectedMonth"
    />
     <download-excel
      class="button"
      :fields="json_fields"
      :fetch="fetchData"
      worksheet="My Worksheet"
      name="capaian_kinerja_pegawai.xls"
    >Download Excel</download-excel>
  </div>
</template>

<script>
  methods: {
    changeSelectedMonth(val) {
      this.selectedMonth = val;
    },
    async fetchData() {
      console.log(this.selectedMonth);
      let selectedMonth = this.selectedMonth;
      let data = this.todos;
      let ret = data.filter(function(data) {
        return data.month === selectedMonth;
      });

      return ret;
    }
  },
</script>

## Child (TodosList.vue) ##
<template>
  <div>
    <select v-model="selectedMonth" style="width:30%;" @change="onChange(selectedMonth)">
      <option v-for="month in months" :key="month" :selected="selectedMonth === month">{{ month }}</option>
    </select>
  </div>
</template>

<script>
methods: {
    onChange(newChangedMonth) {
      this.$emit("changed", newChangedMonth);
    }
  }
};
</script>

When executing async fetchData(), I anticipate that the value of this.selectedMonth will correspond to the chosen option so that I can effectively filter based on the selected month before downloading the excel file. Unfortunately, it always reverts back to the default selectedMonth value.

Answer №1

The event "changed" is triggered in the child component, but the parent component is listening for "change" events.

To fix this issue, replace the following code:

this.$emit("changed", newChangedMonth);

With this revised code:

this.$emit("change", newChangedMonth);

Answer №2

Keep an ear out for the changed event, not just a change

 <TodosList
          v-bind:todos="todos"
          :editedTodo="editedTodo"
          :selectedMonth="selectedMonth"
          :selectedYear="selectedYear"
          :months="months"
          :years="years"
          :json_data="json_data"
          :STORAGE_KEY="STORAGE_KEY"
          @changed="changeSelectedMonth"
        />

Alternatively, emit a change event

this.$emit("change", newChangedMonth);

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

Tips for properly updating node.js within an ionic framework application

Currently, I am working on a project that utilizes vue / vuetify and I am trying to merge it into an Ionic project. The original project is functioning well with vue version 3.4, whereas the Ionic project is using Vue version 3.3, which is not suitable for ...

Upon loading, the Carousel is visible instead of being hidden, which is contrary to its intended behavior

Any help would be greatly appreciated as I am struggling with creating a web page featuring tabs for "London, New York, Shanghai". The initial page displayed is the "welcome" page with the other tabs hidden on load. I successfully implemented a carousel f ...

When I specify the type for the function parameter, an error occurs when I attempt to execute the function

When I assign a generic type to the function parameter and try to call the function, an error message pops up stating "This expression is not callable. Type unknown has no call signature." function a() { return 'abc' } function fun<T>(x: T ...

What is the reason that the ES6 module import expression fails to import JSON files located in the assets folder when using certain path arguments?

A puzzling ES6 import scenario came up where the following import statement did not function as expected within methods of a Vue.js SFC: const fullContentFile = '@/assets/rules/rules.json' import(fullContentFile).then(data => console.log(&apos ...

In the MUI v5 framework, when using nested modals, both the parent and child modal instances will close simultaneously

Here is the reproduction of my issue on codesandbox: https://codesandbox.io/s/trusting-babbage-ovj2we?file=/src/App.js A nested modal has been created where the parent modal opens a button leading to the child modal. The useState of the parent modal has b ...

The object _plugins_vuetify__WEBPACK_IMPORTED_MODULE_8__.default does not contain a constructor function

https://i.sstatic.net/Cqx5B.pngAfter attempting to add Vuetify to my project, I encountered an issue despite trying different solutions from various sources. Below is the code snippet from my vuetify.js file: import Vue from 'vue' import Vuetify ...

The button attached to the jQuery .toggle() function requires two clicks to activate

My objective is twofold: To dynamically load the Disqus script embed.js only when the "Show Comments" button is clicked. To toggle the visibility of the disqus_thread div using the same button while also changing the button's text. The issue I am f ...

Issue with Vuex and Jest - Accessing undefined property <getterName>

Currently, I am attempting to utilize Jest for testing a Vue component that relies on a getter in Vuex. The getter is structured to return a function that ultimately provides an array: questions: state => pageNumber => state.pages[pageNumber].questi ...

Is it possible to pass a useState function to a component during its initialization?

Currently, I am utilizing the useState hook to effectively handle the rendering of components on the screen. My goal is to initialize it with a component while passing in the useState function to set the screen within the component. Below is my App.js fil ...

Upgrade to Bootstrap 5 - Implement Video Modal Feature

I need to update a modal with a video inside from Bootstrap 4 to Bootstrap 5 without jQuery. The issue lies in the JavaScript code below: <script> // codepen.io/JacobLett/pen/xqpEYE $(document).ready(function() { var $videoSrc; $(&a ...

Update content and image when clicking or hovering using Javascript

I have successfully implemented an image change on mouseover using javascript. However, I am facing a challenge in adding a description below the image upon mouseover or click. I do not have much experience with jQuery and would appreciate any help in so ...

Having trouble accessing the href attribute after clicking on an <a> tag with JavaScript

My issue lies in retrieving the href attribute of <a> tags when there is another element nested inside them: Here's the code snippet: const $elems = document.querySelectorAll('.content-container a') var elems = Array.from($elems) e ...

Unexpected behavior observed in the Python minifier Slimit

Attempting to decrease the size of some JavaScript code using the 'slimit' package in Python. import slimit slimit.minify('[1,2,3,4,5,6,7,8]') The above snippet executes without issue and outputs '[1,2,3,4,5,6,7,8]' import ...

What is the best way to style the header of a table when scrolling in CSS?

Currently, I am facing an issue with applying the top CSS property to the thead element of a table while scrolling. I have attempted various methods but have been unsuccessful in achieving the desired outcome. Initially, I used the scroll event, however, ...

The Problem with AJAX Error Handling Customization

Upon loading my webpage using Code Igniter and PHP, the JSON encoded query is returned successfully with data. I have managed to handle the scenario where no records are found by encoding a response in JSON format. However, I am unsure of how to transmit t ...

Jquery syntax for working with objects in Javascript

I am currently working on implementing a jQuery right mouse menu on my page, but I'm facing challenges in structuring it correctly for easy population. On my page, there is a list of items (an HTML table) that users can review. Depending on the user& ...

Is there a way to reach the state in store/index.js without using export?

In my Vue store actions, I am utilizing Axios and looking to dynamically set an Axios header based on a specific state value (such as changing the request header for accepted languages). However, I encountered an issue when attempting to access the store o ...

The $scope variable is not retaining the value for the ng-repeat directive

I'm struggling to implement an ng-repeat on data fetched from an API in JSON format. The issue I'm facing is that even though I can see the data in $scope.wines when I console.log() before the function ends, the data seems to disappear by the end ...

Which specific element should the userEvent.type(...) function target in order to work effectively with MUI's DesktopDatePicker TextField component?

Is there a way for me to input text into the TextField input within the MUI datepicker using react-testing-library's user-event? I've noticed that there is a mask applied to the input. I attempted to use userEvent.type(inputEl, '1') an ...

Expanding the size of the number input in Twitter Bootstrap to accommodate changing content dimensions

I have a numeric input field that I want to customize in terms of width. I need the field to start with a minimum width so that -1, the default value, fits nicely inside. As the value changes, whether decreasing to -100 or increasing to -1,000 and beyond ...