2

If I have code like the code below in my vue.js, upon clicking a button, how can I only display the array item I clicked ( for e.g, Donnie) and hide all of the rest? (Joanne, Peter e.t.c), then when you click the only displayed element again, make all of the other array elements display again?

const app = new Vue({
  el: '#app',
  data: {
    keyword: '',
    friends: [
      {
      name: "Donnie",
      age: "20"
    },
    {
        name: "Joanne",
        age:"19",
     },
      {
      name: "David",
      age: "26"
    },
      {
      name: "Peter",
      age: "23"
    },
      {
      name: "John",
      age: "29"
    },
      {
      name: "Jason",
      age: "19"
    },
     ],
  },
  computed: {
    filteredList() {
      return this.friends.filter((friend) => {
        return friend.name.toLowerCase().includes(this.keyword) + friend.age.includes(this.keyword) + friend.name.includes(this.keyword);
      });

      }
    },

  methods:{
      exclude(friend) {
        console.log(!friend.name);
    },
  }

  })

HTML

   <div v-for="friend in filteredList" class="card" @click="exclude(friend)">
      {{friend.name}} - {{friend.age}}
    </div>
5
  • this is simple question and does not even have to do anything with vuejs, it is a js question Commented May 30, 2018 at 23:24
  • Sorry, still learning, could you help me with the code? Commented May 30, 2018 at 23:25
  • 1
    Your filter expression is using an odd choice of operator. You probably want ||, not + though I guess adding the numeric equivalents of true or false have a similar effect Commented May 30, 2018 at 23:39
  • @Phil Sounds like he wants && Commented May 30, 2018 at 23:50
  • 1
    @TheIncorrigible1 I still think ||. When treated as numbers, Boolean addition works the same as OR in that any true (or 1) makes the expression truthy Commented May 30, 2018 at 23:53

2 Answers 2

1

You should be able to add an identity check to your filter expression if an item has been clicked.

Start by adding a property to store the clicked friend. I'll call mine selected

data {
  selected: null,
  keyword: '',
  //etc
}

Then in your exclude method

exclude (friend) {
  this.selected = this.selected ? null : friend
}

now your computed property can filter the list based on the selected friend first, then fall back to the keyword match

filteredList () {
  return this.selected ? [this.selected] : this.friends.filter(friend => {
    let search = this.keyword.trim().toLowerCase()
    return friend.name.toLowerCase().includes(search) || friend.age.includes(search)
  })
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution, but you should probably also lower-case the keyword.
@puelo was just using the same expression as in OP's code but good point. Updated
1

I think that's what you're looking for:

const app = new Vue({
  el: '#app',
  data: {
    keyword: '',
    friends: [
      {
        name: "Donnie",
        age: "20"
      },
      {
        name: "Joanne",
        age:"19",
      },
      {
        name: "David",
        age: "26"
      },
      {
        name: "Peter",
        age: "23"
      },
      {
        name: "John",
        age: "29"
      },
      {
        name: "Jason",
        age: "19"
      },
    ],
    selected: null
  },
  computed: {
    filteredList() {
      if (!this.selected) {
        return this.friends.filter((friend) => {
          return friend.name.toLowerCase().includes(this.keyword) + friend.age.includes(this.keyword) + friend.name.includes(this.keyword);
        });
      } else {
        return [this.selected];
      }
    },
  },

  methods:{
    exclude(friend) {
      if(!this.selected) {
        this.selected = friend;
      } else {
        this.selected = null;
      }
    },
  }

});
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
    <div v-for="friend in filteredList" class="card" @click="exclude(friend)">
      {{friend.name}} - {{friend.age}}
    </div>
</div>

The trick here is that the selected data property store the friend and also doubles as a checker if there's a friend, so if not, show all, if is, show only that one.

2 Comments

You're missing the keyword search
You're right, thank you for the heads up, edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.