前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何设置Element表格显示或者隐藏列

如何设置Element表格显示或者隐藏列

作者头像
tianyawhl
发布2020-10-14 09:44:24
5.9K0
发布2020-10-14 09:44:24
举报
文章被收录于专栏:前端之攻略前端之攻略

Element 表格点击复选框显示或隐藏列,效果如下:

主要步骤:

一、渲染复选框

代码语言:javascript
复制
    <el-checkbox-group v-model="checkboxVal">
      <el-checkbox
        v-for="item in formTheadOptions"
        :label="item.prop"
        :key="item.prop"
      >{{item.label}}</el-checkbox>      
    </el-checkbox-group>

二、渲染表格

代码语言:javascript
复制
    <el-table :key="key" :data="tableData" border style="width:500px;" height="230" id="table">
      <el-table-column prop="name" label="名称" fixed sortable>
        <template slot-scope="scope">
          <div v-if="scope.row.name>2">
            <span style="color:red;">{{scope.row.name}}</span>
          </div>
          <div v-else>
            <span style="color:green;">{{scope.row.name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in formHead"
        :key="index"
        :label="item.label"
        :prop="item.prop"        
        :render-header="renderheader"
      ></el-table-column>
    </el-table>

三、监听复选框选择内容的变化,过滤表格的thead信息

代码语言:javascript
复制
  watch: {
    checkboxVal(valArr) {
      console.log(valArr);
      this.formHead = this.formTheadOptions.filter(
        item => valArr.indexOf(item.prop) >= 0
      );
      this.key = this.key + 1; 
    }
  },

注意:要设置key为不同的值,不复用之前的表格

数据结构

代码语言:javascript
复制
const defaultFormThead = [
  { label: "第一列 firstColumn", prop: "col1" },
  { label: "第二列", prop: "col2" }
];
const defaultCheckedValue = ["col1", "col2"];

data() {
    return {
      checkboxVal: defaultCheckedValue,
      tableData: [
        {
          name: 1,
          col1: "11",
          col2: "12",
          col3: "13",
          col4: "11",
          col5: "12",
          col6: "13"
        },
        {
          name: 2,
          col1: "11",
          col2: "12",
          col3: "13",
          col4: "11",
          col5: "12",
          col6: "13"
        },
        {
          name: 3,
          col1: "11",
          col2: "12",
          col3: "13",
          col4: "11",
          col5: "12",
          col6: "13"
        },
        {
          name: 4,
          col1: "11",
          col2: "12",
          col3: "13",
          col4: "11",
          col5: "12",
          col6: "13"
        },
        {
          name: 6,
          col1: "16",
          col2: "126",
          col3: "136",
          col4: "116",
          col5: "126",
          col6: "136"
        },
        {
          name: 5,
          col1: "15",
          col2: "125",
          col3: "135",
          col4: "115",
          col5: "1225",
          col6: "1335"
        }
      ],
      key: 1,      
      formTheadOptions: [
        { label: "第一列 firstColumn", prop: "col1" },
        { label: "第二列", prop: "col2" },
        { label: "第三列", prop: "col3" },
        { label: "第四列", prop: "col4" },
        { label: "第五列", prop: "col5" },
        { label: "第六列", prop: "col6" }
      ],
      formHead: defaultFormThead
    };
  },

如果实现头部根据空格换行

代码语言:javascript
复制
    renderheader(h, { column, $index }) {
      //console.log(column)
      if(column.property == "col1"){
        column.sortable = true
        column.minWidth = 150;
      }
      //column.minWidth = 150;
      let arr = column.label.split(" ")
      if(arr.length == 1){
        return arr[0]
      }
      else{
        return h(
        "div",
        { 'class': 'header-center' },
        [
          h("div",arr[0]),
          h("div",arr[1])
        ]
      );
      }
      //console.log(column.label.split(" "))      
    },
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档