简体   繁体   English

在子组件中过滤道具后如何更新父数据?

[英]How to update parent data after filter props in child component?

I want to change my parent component data after filtering month in the child component. 我想在子组件中过滤月份之后更改我的父组件数据。 The problem is where I want to download excel data using vue-json-excel the filter month in the parent component don't want to update. 问题是我想使用vue-json-excel下载excel数据,而父组件中的过滤器月份不想更新。

I have tried to using onChange and emitting event with data in parent, after that update the data in parent. 我尝试使用onChange并在父级中的数据上发出事件,之后更新父级中的数据。 I have tried changing data using computed, but it won't work. 我曾尝试使用计算更改数据,但无法正常工作。

## 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>

I expect this.selectedMonth in async fetchData() changed to chosen option, so before I download the excel, I can filter based on selectedMonth. 我希望异步fetchData()中的this.selectedMonth更改为selected选项,因此在下载Excel之前,我可以基于selectedMonth进行过滤。 It always returns the default selectedMonth value. 它总是返回默认的selectedMonth值。

You are emitting "changed" event in the child, while listening to "change" event on the parent. 您正在孩子中发出"changed"事件,同时在父级上听到"change"事件。

Use this: 用这个:

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

instead of this: 代替这个:

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

Listen for changed event not 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"
        />

OR emit change event 或发出变更事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何更新通过道具发送给父级的子组件中的数据 - How should I update data in child component which I sent form parent via props 如何在不使用道具的情况下将数据从父组件传递到子组件 - How to pass data from parent to child component without using props Svelte:如何将数据或道具从子组件传递给父组件? - Svelte: How to pass data or props from a child component to the parent? 使用prop将子组件数据传递给父组件 - Passing child component data to parent using props 如何处理从父组件到子组件的 props 以填充表单中的 props 数据进行更新 - How to handle the props from Parent Component to child component to populate props data in the form for updation 如何从子组件更新 props.data? - How can I update the props.data from a child component? 如何将道具从父组件添加到子组件的子组件 - How to add props from parent component to child component of child component 子组件更新服务器后如何更新父组件的state? - How to update state of parent component after the server is updated in child component? 将道具从父级发送到子级,并在子级组件(ReactJs)中对其进行更新 - Send props from parent to child and update them in child component (ReactJs) 在父组件获取API数据后,同步子组件以接受道具 - Syncing Child component to accept props after Parent component gets API Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM