最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you toggle a button with Vue.js? - Stack Overflow

programmeradmin7浏览0评论

Note: Using Vue.js and Vuetify.js for functionality and styling.

With :class and @click properties, I was able to change the button's background color to desired color, but it applies the change to all of them, and not just the one that I clicked on.

Question: How do I have a button toggled without having all of them toggled at once?

In my vue file:

<v-layout>
  <v-flex md6>
    <v-text-field>Welcome.</v-text-field>
  </v-flex md6>
  <v-flex id="icon-filter">
    <span>Filter by:</span>
    <v-btn class="filter-button" :class="{toggled: isToggled}" @click="isToggled = !isToggled"><v-icon>local_offer</v-icon></v-btn>
    <v-btn class="filter-button" :class="{toggled: isToggled}" @click="isToggled = !isToggled"><v-icon>notifications</v-icon></v-btn>
  </v-flex>
</v-layout>

In the script section of same vue file:

<script>
  export default {
    data: function() {
      return {
        companies,
        msg: "indiv",
        dashboards: ['profile', 'charts'],
        isToggled: false
      }
    },
    methods: {
      
    }
  }
</script>

I've read through this question, but I get a Vue warning, mentioning that I have isToggled method as already defined data property. Toggle Class for an item Vue.js

I also read through vue.js docs on data binding, but still need help on this. .html

Vuetify framework has toggled buttons components, but client wants a distinct style, so cannot use this.

Note: Using Vue.js and Vuetify.js for functionality and styling.

With :class and @click properties, I was able to change the button's background color to desired color, but it applies the change to all of them, and not just the one that I clicked on.

Question: How do I have a button toggled without having all of them toggled at once?

In my vue file:

<v-layout>
  <v-flex md6>
    <v-text-field>Welcome.</v-text-field>
  </v-flex md6>
  <v-flex id="icon-filter">
    <span>Filter by:</span>
    <v-btn class="filter-button" :class="{toggled: isToggled}" @click="isToggled = !isToggled"><v-icon>local_offer</v-icon></v-btn>
    <v-btn class="filter-button" :class="{toggled: isToggled}" @click="isToggled = !isToggled"><v-icon>notifications</v-icon></v-btn>
  </v-flex>
</v-layout>

In the script section of same vue file:

<script>
  export default {
    data: function() {
      return {
        companies,
        msg: "indiv",
        dashboards: ['profile', 'charts'],
        isToggled: false
      }
    },
    methods: {
      
    }
  }
</script>

I've read through this question, but I get a Vue warning, mentioning that I have isToggled method as already defined data property. Toggle Class for an item Vue.js

I also read through vue.js docs on data binding, but still need help on this. https://v2.vuejs.org/v2/guide/class-and-style.html

Vuetify framework has toggled buttons components, but client wants a distinct style, so cannot use this. https://vuetifyjs.com/components/buttons

Share Improve this question edited Jul 14, 2022 at 1:03 tony19 138k23 gold badges277 silver badges346 bronze badges asked Oct 18, 2017 at 15:50 stormynpipstormynpip 4724 gold badges7 silver badges14 bronze badges 5
  • You'll need a separate isToggled for each button, like localOfferIsToggled. – ceejayoz Commented Oct 18, 2017 at 15:54
  • 2 Each button should be a separate component, so that they have internal references to their own state. What you're doing right now is updating a global state that is shared across all button instances. – Terry Commented Oct 18, 2017 at 15:59
  • @Terry Can you elaborate more on your comment? – stormynpip Commented Oct 18, 2017 at 16:21
  • @Pusheenicorn This question's accepted answer is an example of what Terry is describing. stackoverflow.com/questions/46791340/… – thanksd Commented Oct 18, 2017 at 16:52
  • Take a look to this article webomnizz.com/create-toggle-switch-button-with-vue-js which helps to create own custom button – jogesh_pi Commented Apr 9, 2020 at 7:40
Add a comment  | 

2 Answers 2

Reset to default 9

Make another vue file (lets call it button.vue)...

button.vue

// template
<v-btn class="filter-button" :class="{toggled: isToggled}" @click="isToggled = !isToggled">
  <slot></slot>
</v-btn>

// script
export default {
  data: function () {
    return {
      isToggled: false
    }
  }
}

your_parent.vue

// script
import CustomButton from './button.vue'

export default {
  components: { CustomButton },
  data...
}

// template
<v-layout>
  <v-flex md6>
    <v-text-field>Welcome.</v-text-field>
  </v-flex md6>
  <v-flex id="icon-filter">
    <span>Filter by:</span>
    <custom-button><v-icon>local_offer</v-icon></custom-button>
    <custom-button><v-icon>notifications</v-icon></custom-button>
  </v-flex>
</v-layout>

Note: CustomButton and button.vue can be renamed to whatever is convenient for you

This would allow each custom-button to have its own data that can now be toggled!

I would use @click with a function and pass the $event to it, then use javascripts classList.toggle function to apply a css class or use .toggle()do whatever else you want.;

@click="myToggleFunction($event)"

Then

methods: {
   myToggleFunction: function(event){
        let button = event.target;
        button.classList.toggle('toggled');
   }
}
发布评论

评论列表(0)

  1. 暂无评论