zoukankan      html  css  js  c++  java
  • 实现v-for遍历出来的 元素的 单选 和 多选

    <template>
      <div class="home">
        <div class="box">
          <ul>
            <li 
             :class="{active: (isActive &&  currentIndex == index) || (MultiSelect && selectList.indexOf(list[index]) !=-1) }" 
            class="label" 
            v-for="(data,index) in list" 
            :key="index"
            @click="select(index)">{{data}}</li>
          </ul>
        </div>
      </div>
    </template>
    
    <script>
    import { reactive,ref } from '@vue/reactivity'
    // @ is an alias to /src
    
    export default {
      name: 'Home',
      setup(){
        const list = reactive(['xh','xm','dh','ypp'])
        const isActive = ref(false) // 是否选中
        const MultiSelect = ref(false) //是否多选
        const currentIndex = ref(null) // 当前索引的对比值
        const selectList = reactive([]) //多选的判断
        const select = (index) => {
          if(MultiSelect.value){
            if(selectList.indexOf(list[index]) == -1){
              selectList.push(list[index])
              console.log(selectList)
            }else {
              console.log(selectList.indexOf(list[index]))
              selectList.splice(selectList.indexOf(list[index]),1)
            }
    
          }else {
            //单选
            // isActive.value = true
            if(currentIndex.value == index) isActive.value = ! isActive.value
            else{
              currentIndex.value = index
              isActive.value = true
            }
           
          }
        }
        return {
          list,
          isActive,
          MultiSelect,
          currentIndex,
          selectList,
          select
        }
      }
    }
    </script>
    <style lang="stylus">
      .label {
        display inline-block
        border solid #ccc 1px
        border-radius .5rem
        padding 2rem 3rem
        margin-left 2rem
        list-style none
      }
      .active {
        background rgba(200,10,230,.3)
        color yellow
      }
    </style>
  • 相关阅读:
    Use jQuery to hide a DIV when the user clicks outside of it
    Backbone
    重学HTML
    javaScript return false
    Regular Expression--Good parts
    ASP.NET批量下载文件的方法
    ASP.NET多文件批量打包下载
    word图文混排复制到UEditor图片不显示
    百度ueditor 拖文件或world 里面复制粘贴图片到编辑中 上传到第三方问题
    Ueditor Word图片转存交互
  • 原文地址:https://www.cnblogs.com/diligent-noob/p/15173100.html
Copyright © 2011-2022 走看看