3

I want to show conditional buttons inside a v-for loop using v-if (only when category_id=1): this is the vuejs part:

<div class="w3-margin-bottom" v-for="category in categorys" v-bind:key="category.category_id">
  <div class="w3-quarter w3-button">
      <i class="w3-small" v-if="category.category_id='1'">
          <button class="w3-button w3-green">Sauce Blanche</button>
          <button class="w3-button w3-green">Sauce Rouge</button>
      </i>
      <div class="w3-container w3-red w3-padding-16">
        <div class="w3-clear">{{ category.category_des }}</div>
      </div>
  </div>
</div>

and this is my javascript part:

import axios from "axios";
export default {
  data() {
    return {
      categorys: [],
      category_id: null,
      category_des: "",
    };
  },
  methods: {
    getCategory: function () {
      axios.get("http://127.0.0.1:8000/api/Category/").then((response) => {
        this.categorys = response.data;
      });
    },
2
  • try like this v-if="category.category_id === '1' Commented May 25, 2021 at 21:21
  • You've just got a typo in your v-if, = needs to be == or ===. Always double check your equal signs, this is a really easy mistake to make! Commented May 25, 2021 at 21:27

2 Answers 2

3
<template v-if="category.category_id === '1'">
  --buttons-- 
</template>

This assumes category_id is a string. Leave off the small quotes if it is a number.

Sign up to request clarification or add additional context in comments.

Comments

3

v-if="category.category_id = '1'" is always true, cause you used assign operator (=), instead of equality operator (==) or reference equality operator (===).

Change this to this instead: v-if="category.category_id === '1'"

Comments

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.