Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle search with $emit value? #41

Closed
sajjadalis opened this issue May 25, 2020 · 5 comments
Closed

How to handle search with $emit value? #41

sajjadalis opened this issue May 25, 2020 · 5 comments
Labels
question Further information is requested

Comments

@sajjadalis
Copy link

I have search input field in header component. I'm getting emitted value but it's doesn't filter content. How do i pass $fetch with $emit from header component or what is the right way?

Index.vue

import Header from '@/components/layout/header';
export default {
  components: {
    Header
  },
  data() {
    return {
      posts: [],
      q: ''
    }
  },
  async fetch() {
    this.posts = await this.$content('posts')
    .search(this.q)
    .fetch()
  }
}

Header.vue

<template>
<div>
<input type="search" placeholder="Type your search" v-model="q" @input="$emit('q', $event)">
</div>
</template>
<script>
export default {
  props: [ 'q' ]
}
</script>
@sajjadalis sajjadalis added the question Further information is requested label May 25, 2020
@atinux
Copy link
Member

atinux commented May 25, 2020

@sajjadalis your Header us emitting q event, then you can listen in you index.vue and call $fetch, see more on https://vuejs.org/v2/guide/components.html#Emitting-a-Value-With-an-Event

<Header ... @q="$fetch" />

@atinux atinux closed this as completed May 25, 2020
@sajjadalis
Copy link
Author

sajjadalis commented May 25, 2020

@atinux Thanks for reply.

I tried this but its not working.

With only @q="$fetch", input field does not accept text, when i try to type it goes blank.
<Header :q="q" @q="$fetch" />

If i use @q="q = $event.target.value, $fetch" then input and v-model works fine but doesn't filter content.

<Header :q="q" @q="q = $event.target.value, $fetch" />

@atinux
Copy link
Member

atinux commented May 25, 2020

Then you need to add a method onSearch to update q and call fetch() again

@sajjadalis
Copy link
Author

sajjadalis commented May 25, 2020

@atinux Thanks for the help.

Is this the correct way to call onSearch method? It's working and filtering content, but i'm not sure if its require to call the whole await this.$content again.

methods: {
    async onSearch($event) {
      this.q = $event.target.value;

      this.posts = await this.$content('posts')
      .search(this.q)
      .fetch()
  }
}

@atinux
Copy link
Member

atinux commented May 26, 2020

I think that you can call this.$fetch() instead of this.$content('posts')..

pi0 pushed a commit that referenced this issue Feb 8, 2022
Co-authored-by: Renovate Bot <bot@renovateapp.com>
pi0 pushed a commit that referenced this issue Feb 8, 2022
Co-authored-by: Kevin Marrec <kevin@marrec.io>
Co-authored-by: Yaël Guilloux <yael.guilloux@gmail.com>
pi0 pushed a commit that referenced this issue May 5, 2022
Co-authored-by: Farnabaz <farnabaz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants