Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

javascript - VueJS: Filter Function only works on reload

When my website loads the array for my select box, it always returns all objects inside the array. It should filter everyone who is registered already but it only works when I press reload after navigating to the view.

I am completly lost, why when I navigate to this page it loads all athletes. When I press F5, the athletes are filtered correctly.

router.js

{
  path: '/events/:id/register/athletes',
  name: 'RegisterAthletes',
  component: () => import(/* webpackChunkName: "registerAthletes" */ '../views/RegisterAthletes.vue'),
  beforeEnter: async (to, from, next) => {
    if (!await isAuthorized()) next({ name: 'Login' });
    if (!await isGymManager()) next({ name: 'Error403' });
    else next();
  },
},

view

<template>
  <select id="athlete" v-model="selectedAthlete" class="form-control">
    <option v-for="a in freeAthletes" :key="a.id" :value="a.id">
      {{ a.account.full_name }}
    </option>
  </select>
</template>

<script>
  methods: {
    freeAthletes() {
      return this.athletes.filter((athlete) => !this.registrations
        .map((reg) => reg.athleteId).includes(athlete.id));
    },
  },
  async mounted() {
    this.session = await this.$getSession();
    const eventId = this.$route.params.id;
    const gymId = this.session.gym_id;
    this.event = await this.$getEvent(eventId);
    this.gym = await this.$http.get(`gyms/${gymId}`).then((response) => response.data);
    this.athletes = await this.$getUnregisteredAthletes(eventId, gymId);
  }
</script>

function

Vue.prototype.$getUnregisteredAthletes = async function (eventId, gymId) {
  const allAthletes = await this.$http.get(`athletes?filter[gym_id]=${gymId}&expand=registrations`)
    .then((response) => response.data)
    .catch((error) => this.$log(error));
  return allAthletes.filter((athlete) => !athlete.registrations
    .map((reg) => String(reg.event_id)).includes(eventId));
};
question from:https://stackoverflow.com/questions/65641046/vuejs-filter-function-only-works-on-reload

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...