美文网首页
vue 循环列表点击多选 再次点击取消

vue 循环列表点击多选 再次点击取消

作者: jesse28 | 来源:发表于2021-08-22 09:08 被阅读0次
<template>
  <div class="roster-content">
    <div class="roster-grid-item" v-for="(item, index) in list" :key="index">
      <div
        :class="{ bgcolor: spanIndex.indexOf(index) > -1 }"
        @click="selectName(index)"
        :data-index="index"
      >
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      list: [
        { name: '余光中', id: 1 },
        { name: '郑愁予', id: 2 },
        { name: '戴望舒', id: 3 },
        { name: '乐毅', id: 4 },
        { name: '吴起', id: 5 },
        { name: '孙膑', id: 6 },
    
      ],
      spanIndex: []
    };
  },
  mounted () {},
  methods: {
    selectName (index) {
      let arrIndex = this.spanIndex.indexOf(index);
      console.log(arrIndex)
      if (arrIndex > -1) {
        this.spanIndex.splice(arrIndex, 1);
        console.log(this.spanIndex)
      } else {
        this.spanIndex.push(index);
      }
      console.log(this.spanIndex);
    }
  }
};
</script>

<style lang="scss" scoped>
.roster-content {
  display: flex;
  flex-wrap: wrap;
  padding-left: 35px;
  overflow: hidden;
  overflow-y: scroll;
}
.roster-grid-item {
  width: 25%;
  padding-right: 35px;
  margin-top: 20px;
  text-align: center;
  font-size: 20px;
  div {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    height: 100%;
    padding: 16px 8px;
    background-color: #409eff;
    color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 4px 0 rgba(148, 150, 171, 0.77);
  }
}
.roster-content::-webkit-scrollbar {
  display: none;
}
.bgcolor {
  background: #f56c6c !important;
}
</style>

效果如图:


image.png

相关文章

网友评论

      本文标题:vue 循环列表点击多选 再次点击取消

      本文链接:https://www.haomeiwen.com/subject/vqsciltx.html