Vue动态控制表格列的显示隐藏

效果
Vue
如上图所示,点击table右上方的表格按钮,弹出菜单栏,进行勾选,从而达到表格对应列显示和隐藏

1.HTML部分(ant design)

<div class="right-btns">
  <!-- 按钮 -->
  <a-button class="table" @click="handleTable"><a-icon type="table" /></a-button>
  <!-- 弹出框 -->
  <div class="tableSelect" v-if="tableSelectVisible">
    <a-checkbox-group :value="checkValue" :options="colOptions"/>
  </div>
</div>

<!-- 表格内容 -->
<a-table :dataSource="expertList">
  <a-table-column v-if="colIsTrue[0]" title="专家ID" key="idAsString" />
  <a-table-column v-if="colIsTrue[1]" title="专家姓名" key="name" />
  <a-table-column v-if="colIsTrue[2]" title="行业" key="industry" />
  <a-table-column v-if="colIsTrue[3]" title="年龄" key="age" />
  <a-table-column v-if="colIsTrue[4]" title="学历" key="education" />
  <a-table-column v-if="colIsTrue[5]" title="工作单位" key="company" />
  <a-table-column v-if="colIsTrue[6]" title="入库时间" key="createAt" />
  <a-table-column title="操作" key="action">
    <template slot-scope="record">
      <a-button size="small" icon="edit" type="primary" @click="openUpd(record.id)">编辑</a-button>
      <a-button size="small" icon="delete" type="primary" @click="openDel(record)">删除</a-button>
    </template>
  </a-table-column>
</a-table>

a. 通过v-if="colIsTrue[6]"来判断表格对应列的状态

2.javascript部分

data(){
  return {
	colOptions:[
      {label:'专家ID',value:0,onChange:this.colChange},
      {label:'专家姓名',value:1,onChange:this.colChange},
      {label:'行业',value:2,onChange:this.colChange},
      {label:'年龄',value:3,onChange:this.colChange},
      {label:'学历',value:4,onChange:this.colChange},
      {label:'工作单位',value:5,onChange:this.colChange},
      {label:'入库时间',value:6,onChange:this.colChange},
    ],
    colIsTrue:[],
    checkValue:[],
  }
},
created(){//当所有数据都加载完成之后,将checkbox设置为全部选中状态
  this.colOptions && this.colOptions.forEach((item,index)=>{
    this.colIsTrue.push(true)
    this.checkValue.push(index)
  })
},
mounted(){//菜单栏弹出后,点击其它地方,关闭菜单栏
  let that=this;
  $(document).bind("click", function(e) {
    var target = $(e.target);
    if (target.closest(".tableSelect").length == 0 && target[0].className.indexOf('table')==-1) {
      that.tableSelectVisible=false;
    }
  })
  this.colOptions && this.colOptions.forEach((item,index)=>{
    this.colIsTrue.push(true)
    this.checkValue.push(index)
  })
},
methods:{
  colChange(e){//当checkbox有修改时
   const index=e.target.value
   this.colIsTrue[index] ? 
   (this.$set(this.colIsTrue,index,false)) : 
   (this.$set(this.colIsTrue,index,true))

   if(e.target.checked) this.checkValue.push(index)
   else{
     this.checkValue.forEach((item,arrIndex)=>{
       if(item==index){
         this.checkValue.splice(arrIndex,1)
       }
     })
   }
 },
 handleTable(){//打开菜单栏
   this.tableSelectVisible=true;
 },
}

3.css样式部分

.right-btns{
  position:relative;
  .tableSelect{
    position: absolute;
    background:#fff;
    border:1px solid #ecedef;
    display:inline-block;
    top: 33px;
    right: 0;
    z-index: 100;
    padding: 10px;
    width: 156px;
  }
}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值