简体   繁体   English

如果在v-for中列出了除单击的按钮之外的所有按钮,如何隐藏它们?

[英]How to hide all buttons except clicked one if they are listed in v-for?

I have projects route where I used v-cards to show project name, link to details and button that should start project. 我在项目路线中使用v卡显示项目名称,指向详细信息的链接以及应该启动项目的按钮。 When I click button it is disabled and another is shown-to stop it. 当我单击按钮时,它被禁用,并显示另一个-停止它。 Problem is that to show all projects cards I used v-for that goes through all projects. 问题在于要显示我使用v-for的所有项目卡片,该卡片遍及所有项目。 When I click on one start button, all are disabled (and that's ok), but I want only one stop button to appear, not all. 当我单击一个开始按钮时,所有按钮都被禁用(没关系),但是我只希望出现一个停止按钮,而不是全部。 I can get project id but I can't wrap my head around how to disable all others projects stop buttons 我可以获取项目ID,但是无法解决如何禁用所有其他项目的停止按钮的问题

  <v-flex v-for="project in projects"
    :key="project.id" xs4>
    <v-card>
      <v-card-title>
        <span class="headline">{{ project.name }}</span>
        <v-spacer></v-spacer>
        <v-btn
          v-if="!isButtonActive"
          left
          small
          fab
          outline
          color="green"
          v-model="isButtonActive"
          @click="startStop(isButtonActive, project.id)">
          <v-icon>play_arrow</v-icon>
        </v-btn>
        <v-btn
          v-if="isButtonActive"
          small
          v-model="isButtonActive"
          @click="startStop(isButtonActive, project.id)">
          <stopwatch />
        </v-btn>
      </v-card-title>
      <v-card-actions>
        <v-btn
          small
          flat
          @click="goTo({
              name: 'project',
              params: {
                projectId: project.id
              }
            })">
          Project details
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-flex>

in script: 在脚本中:

startStop (isButtonActive, projectId) {
  console.log(projectId)
  this.isButtonActive = !isButtonActive
  // hideButtons(projectId)
}

I think you'll want to store all the data about your buttons in an array, here's a quick crack at what you may be looking to do. 我想您希望将有关按钮的所有数据存储在一个数组中,这可以让您快速找到所需的内容。

I've edited the snippet to track the index and also track what project, and buttonType is active. 我已经编辑了代码段以跟踪索引,还跟踪什么项目,并且buttonType处于活动状态。 The general concept is you need to track what project is 'activated', and you'll need a mechanism to change what project is active. 一般概念是您需要跟踪“激活”了哪些项目,并且需要一种机制来更改正在激活的项目。

 data () { return { activeProject: 0, activeButton: 'start', projects: [ { name: 'one', active: true }, { name: 'two, active: false } ], buttons: [ { color: 'green', active: true, icon: 'play_arrow', type: 'start' }, { color: 'blue', active: false, type: 'stop' } ] } }, methods: { startStop (projectId) { this.buttons.forEach((button) => { if (button.type !== activeButton) { button.active = true } else { button.active = false } }) }, activateProject (index) { this.activeProject = index } } 
 <v-flex v-for="(project, index) in projects" :key="project.id" xs4> <v-card> <v-card-title> <span class="headline">{{ project.name }}</span> <v-spacer></v-spacer> <v-btn v-for="button in buttons" v-if="button.active && activeProject === index" :color="button.color" v-model="button.active" @click="startStop()"> <v-icon v-if="button.type === 'start'">play_arrow</v-icon> <stopwatch v-else /> </v-btn> </v-card-title> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM