Iview中CheckboxGroup绑定后台数据


 

<div >
    <CheckboxGroup style="margin-left:78px;" @on-change="SelectQuestionType" v-model="selectTypes">
       <Checkbox label="0" key="0" :v-text="0"> <span>全部</span></Checkbox>
       <Checkbox v-for="item in questionTypes" :label="item.Id" :key="item.Id" ref="checkboxGroup">
                 <span>{{item.TypeName}}</span>
       </Checkbox>
    </CheckboxGroup>
</div>
 
<script>
export default {
       data () {
          return {
            isAllCheck:false,
            selectTypes:[],
           }
    },
    mounted(){
          this.GetQuestionTypes();
    },
    methods:{
         GetQuestionTypes()
        {
          //此处省略请求代码,result为后台数据
           this.questionTypes=result;
        },
        SelectQuestionType(data)
      {
       
            var checkboxGroup=this.$refs.checkboxGroup;
           var typeIds=data;
           if(this.ArrayContain(data,"0"))
          {
            if(typeIds.length==checkboxGroup.length&& this.isAllCheck==true)
            {
                 for(var i=0;i<typeIds.length;i++)
                  {
                    if(typeIds[i]=="0")
                     {
                        typeIds.splice(i,1);;break;
                     }
                  }
                  this.isAllCheck=false;
              }else{
                  typeIds=[];
                  checkboxGroup.forEach(function(item,index){
                  typeIds.push(item.label);
                   });
                  typeIds.push("0");
                  this.isAllCheck=true;
                 } 
             }
             else if(typeIds.length==checkboxGroup.length)
            {
                if(this.isAllCheck)
                {
                   typeIds=[];
                   this.isAllCheck=false;
                }else{
                  typeIds.push("0");
                   this.isAllCheck=true;
               }
            }
              this.selectTypes=typeIds;
        },
        ArrayContain(array,id)
       {
             var result=false;
           for(var i=0;i<array.length;i++)
           {
               if(array[i]==id)
             {
               result=true;break;
             }
          }
          return result;
        }
   }
}
</script>
 
 
如有更好的方法可以给个思路,谢谢!
 
 
 
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM