Filter data using v-select in Vue

Asked

Viewed 322 times

-1

I have an API that returns me all registered users on the system, I need to implement a form of search where I can filter these users by sex. I have a field (v-select) where we can choose male or female, but the method of filtering is not working properly.

Ex:in case I do not choose anything should see all registered employees, if choose female, only female people should appear, if male vice versa.

only that at the moment the function filters the data only once , if I remove the filter all users get lost and no data appears.

PS: I am using Vue and vuetify and have no previous experience with js or front frameworks.

The code I have is:

    <template>

    <v-col class="d-flex" cols="12" sm="6">
       <v-select v-model="genero" @change="getfilteredGenero" :items="generoChoice"  label="Sexo" :clearable="true" solo></v-select>
     </v-col>

    </template>


     <script>
        export default {
      data() {
        return {
          users:[],
          genero:'',
          generoChoice:['Feminino', 'Masculino'],
      },

      mounted() {
        axios.get('users/')
        .then(response => {
          this.users = response.data;
        })
        .catch(error => console.log(error))
      },
      methods: {
        getfilteredGenero(){
          let usersAux = this.users;
          let genero = this.genero;

          this.users = usersAux.filter(function(obj){
            if(genero == 'Feminino'){
              return obj.sexoChoice == 'FEM'
            }else if(genero == 'Masculino'){
              return obj.sexoChoice == 'MAS'
            }
          });
        },
      },
    }

</script>

In the call the API returns me the following keys:

{
        "id": *,
        "username": "****",
        "name": "****",
        "social_name": **,
        "email": "*******",
        "profile": *,
        "password": "*******",
        "birthdate": **,
        "sexoChoice": "FEM"
    },
  • The problem varies a lot from the return of the API, is it possible to show a basic example at least of the keys that come in the object? But what I suggest is to solve this in the back-end, making a new endpoint or the ideal would be a filter, where you would pass something in queryString and it returns only the users of the desired sex. If it is not possible to post an example API result that I try to help

  • Hi, I added in question the return in API. I thought about creating another endpoint but on the same page I have a search bar where you can also find the user by name. I have the endpoint that returns all registered users and in Vue I make the filter to return only the user with the name searched in the search bar. Being that in addition I need to implement these Selects to be able to filter users according to some attributes such as sex, for example, and this I can’t do.

1 answer

1


Sorry for the delay in responding. In case the problem I imagine is happening, is that you are filtering the array with only one variable users for sex.

However, the second time you lost all the initial contents of this array that came back in response. That is, you assume that you received 4 answer users, 2 male and 2 female. If you select in your select the female gender, you will filter and lose the male 2 of the answer automatically. How will you not make another answer on @change from your select, you have no way to recover and show the others.

Here is a simple example below showing how to use two variables. I also changed the method. In your case, I believe it is enough to remove the static objects and uncomment the API call.

export default {
  name: 'App',
  data: () => ({
    users:[
      {
          id: 1,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'FEM'
      },
      {
          id: 2,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'FEM'
      },
      {
          id: 3,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'MAS'
      },
      {
          id: 4,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'MAS'
      },
    ],
    usersInitial: [
      {
          id: 1,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'FEM'
      },
      {
          id: 2,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'FEM'
      },
      {
          id: 3,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'MAS'
      },
      {
          id: 4,
          username: 'teste',
          name: 'teste',
          social_name: 'teste',
          email: '[email protected]',
          profile: 'Teste',
          password: '***',
          birthdate: '09/12/1955',
          sexoChoice: 'MAS'
      }
    ],
    genero:'',
    generoChoice:['Feminino', 'Masculino'],
  }),
  mounted() {
    // axios.get('users/')
    //   .then(response => {
    //     this.users = response.data;
    //     this.usersInitial = response.data;
    //   })
    //   .catch(error => console.log(error))
  },
  methods: {
    getfilteredGenero() {
      const generoAbbr = this.genero.substring(0, 3).toUpperCase()
      const users = [ ...this.usersInitial ]
      this.users = users.filter(user => user.sexoChoice.toUpperCase() === generoAbbr)
    },
  },
};
<template>
  <v-app>
    <v-row>
      <v-col class="d-flex" cols="12" sm="6">
        <v-select v-model="genero" @change="getfilteredGenero" :items="generoChoice"  label="Sexo" :clearable="true" solo></v-select>
        <ul>
          <li v-for="(user, idx) in users" :key="idx">
            {{ user.name }} - {{ user.sexoChoice }}
          </li>
        </ul>
      </v-col>
    </v-row>
  </v-app>
</template>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.