Vue+Element UI表格列的显示和隐藏

如下代码:

<template>
  <div>
    <el-table
      :data="tabledate"
      height="400"
    >
      //使用v-if else 控制两列的显示和隐藏,每个列必须加唯一标识符:key="Math.random()",否则会不起作用,无法控制显示与隐藏
      <el-table-column type="selection" width="50" v-if="showColumn==true" :key="Math.random()"></el-table-column>
      <el-table-column label="序号" prop="id" v-else :key="Math.random()"></el-table-column>
      <el-table-column label="英文名" prop="value"></el-table-column>
      <el-table-column label="中文名" prop="label"></el-table-column>
    </el-table>
    <el-button plain style="width:100px" @click="showColumn=true">显示</el-button>
    <el-button plain style="width:100px" @click="showColumn=false">隐藏</el-button>
  </div>
</template>

<script>
export default {
  name: "TableColumnShowHideDemo",
  data() {
    return {
      table_interval: null,
      //控制列的显示和隐藏
      showColumn: false,
      tabledate: [
        { id: "1", value: "right", label: "正确" },
        { id: "2", value: "wrong", label: "错误" },
        { id: "3", value: "rightorwrong", label: "正确or错误" },
        { id: "4", value: "right", label: "正确" },
        { id: "5", value: "wrong", label: "错误" },
        { id: "6", value: "rightorwrong", label: "正确or错误" },
        { id: "7", value: "right", label: "正确" },
        { id: "8", value: "wrong", label: "错误" },
        { id: "9", value: "rightorwrong", label: "正确or错误" },
        { id: "10", value: "right", label: "正确" },
        { id: "11", value: "wrong", label: "错误" },
        { id: "12", value: "rightorwrong", label: "正确or错误" },
        { id: "13", value: "right", label: "正确" },
        { id: "14", value: "wrong", label: "错误" },
        { id: "15", value: "rightorwrong", label: "正确or错误" }
      ]
    };
  },
  methods: {}
};
</script>

    原文作者:The Future is mine
    原文地址: https://blog.csdn.net/yhj198927/article/details/123996123
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞