【问题标题】:Apply styles to specific v-for element将样式应用于特定的 v-for 元素
【发布时间】:2019-02-26 06:05:00
【问题描述】:

我正在制作一个简单的待办事项列表应用程序,并且想知道如何在特定的动态v-for 元素上应用样式。

<f7-list-item v-for="(listItem, index) in activeList.listItems" :key="index" :class="(checked) ? 'checked':'not-checked'">
 {{ index+1 }}. {{ listItem }}
  <span @click="checkTaskDone(index)">
    <i class="f7-icons" id="check-task-btn">check_round</i>
  </span>
</f7-list-item>
export default {
 data() {
  return {
   checked: false
  }
 },
 methods: {
  checkTaskDone(item) {
   if (this.checked == false) {
    this.checked = true;
   } else if (this.checked == true) {
    this.checked = false;
   }
  }
 }
}
.checked {
 text-decoration: line-through;
 color: #444;
}

使用此代码,它将类添加到每个v-for 列表元素中,而不管单击了哪个元素,正如预期的那样。我想知道处理这个问题的最佳方法是什么。我已经尝试从 index 制作一个道具,并尝试以它为目标来应用样式,但我无法让它发挥作用。

提前致谢!

【问题讨论】:

    标签: javascript css list vue.js html-framework-7


    【解决方案1】:

    通常,您希望在各个待办事项上有一个“已完成”或“已检查”标志,例如:

    const todoList = [
      {
        name: 'Grab some food',
        done: false
      },
      {
        name: 'Start coding',
        done: false
      }
    ];
    

    在 Vue.js 中,您可以使用 v-bind:class 而不是三元运算符来切换类:

    export default {
      data() {
        return {
          //checked: false,
    
          activeList: {
            listItems: [
              {
                name: 'Grab some food',
                done: false
              },
              {
                name: 'Start coding',
                done: false
              }
            ]  
          }
        }
      },
      methods: {
        checkTaskDone(item) {
          //if (this.checked == false) {
          //  this.checked = true;
          //} 
          //else if (this.checked == true) {
          //  this.checked = false;
          //}
    
          // Check/uncheck
          item.done = !item.done;
        }
      }
    }
    
    <f7-list-item 
      v-for="(listItem, index) in activeList.listItems" 
      :key="index"
      :class="{ 'checked': listItem.done }">
    
     {{ index + 1 }}. {{ listItem }}
    
      <span @click="checkTaskDone(listItem)">
        <i class="f7-icons" :id="`check-task-btn${index}`">check_round</i>
      </span>
    </f7-list-item>
    

    顺便说一句,我在各个 i.f7-icons 元素上附加了一个索引,因为 ID 应该是唯一的,否则请改用 class

    【讨论】:

    • 非常简单 - 非常感谢!像魅力一样工作:)
    【解决方案2】:

    首先需要根据activeList.listItems长度创建动态校验数组!然后你可以检查索引,你可以通过this.$set(array,index,value)更新数组数据...

    new Vue({
     el: "#app",
     data: {
       checked: [],
       activeList : {listItems:[1,2,3,5]}
     },
     created: function() {
       for(var i = 0; i < this.activeList.listItems.length; i++) {
         this.checked.push(false);
       }
     },
     methods: {
      checkTaskDone(item) {
       if (this.checked[item] == false) {
        this.$set(this.checked,item, true);
       } else if (this.checked[item] == true) {
        this.$set(this.checked,item, false);
       }
      }
     }
     });
    .checked {
     text-decoration: line-through;
     color: red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
    <div v-for="(listItem, index) in activeList.listItems" :key="index" :class="{checked: checked[index]}">
     {{ index+1 }}. {{ listItem }}
      <span @click="checkTaskDone(index)">
        <i class="f7-icons" id="check-task-btn">check_round</i>
      </span>
    </div>
    
    </div>

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 2012-06-27
      • 2019-05-30
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多