当前位置:   article > 正文

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

vue隐藏表格列

一、效果:

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


二、代码

1.HTML部分

  1. <template>
  2.   <div id="app">
  3.     <el-table :data="tableData" border style="width: 100%" ref="table">
  4.       <el-table-column
  5.         fixed
  6.         prop="date"
  7.         label="日期"
  8.         width="150"
  9.         v-if="showColumn.date"
  10.       >
  11.       </el-table-column>
  12.       <el-table-column
  13.         prop="name"
  14.         label="姓名"
  15.         width="120"
  16.         v-if="showColumn.name"
  17.       >
  18.       </el-table-column>
  19.       <el-table-column
  20.         prop="province"
  21.         label="省份"
  22.         width="120"
  23.         v-if="showColumn.provinces"
  24.       >
  25.       </el-table-column>
  26.       <el-table-column
  27.         prop="city"
  28.         label="市区"
  29.         width="120"
  30.         v-if="showColumn.city"
  31.       >
  32.       </el-table-column>
  33.       <el-table-column
  34.         prop="address"
  35.         label="地址"
  36.         width="300"
  37.         v-if="showColumn.adreess"
  38.       >
  39.       </el-table-column>
  40.       <el-table-column
  41.         prop="zip"
  42.         label="邮编"
  43.         width="120"
  44.         v-if="showColumn.zipCode"
  45.       >
  46.       </el-table-column>
  47.       <el-table-column fixed="right" width="100" align="center">
  48.         <template slot="header">
  49.           <i
  50.             class="el-icon-setting"
  51.             style="font-size: 22px; cursor: pointer"
  52.             @click="showColumnOption"
  53.           ></i>
  54.         </template>
  55.         <template slot-scope="scope">
  56.           <el-button @click="handleClick(scope.row)" type="text" size="small"
  57.             >查看</el-button
  58.           >
  59.           <el-button type="text" size="small">编辑</el-button>
  60.         </template>
  61.       </el-table-column>
  62.     </el-table>
  63.     <!-- 配置列面板 -->
  64.     <transition name="fade">
  65.       <div class="columnOption" v-show="isShowColumn">
  66.         <div class="content">
  67.           <div class="head">选择显示字段</div>
  68.           <div class="body">
  69.             <el-checkbox v-model="checkList.date" disabled>日期</el-checkbox>
  70.             <el-checkbox v-model="checkList.name">姓名</el-checkbox>
  71.             <el-checkbox v-model="checkList.provinces">省份</el-checkbox>
  72.             <el-checkbox v-model="checkList.city">市区</el-checkbox>
  73.             <el-checkbox v-model="checkList.adreess">地址</el-checkbox>
  74.             <el-checkbox v-model="checkList.zipCode">邮编</el-checkbox>
  75.           </div>
  76.           <div class="footer">
  77.             <el-button size="small" type="primary" plain @click="saveColumn"
  78.               >保存列配置</el-button
  79.             >
  80.           </div>
  81.         </div>
  82.       </div>
  83.     </transition>
  84.   </div>
  85. </template>

  通过 v-if="showColumn.date" 来判断表格对应列的状态

 2.javascript部分

  1. <script>
  2. export default {
  3.   data() {
  4.     return {
  5.       isShowColumn: false,
  6.       tableData: [
  7.         {
  8.           date: "2016-05-02",
  9.           name: "王小虎",
  10.           province: "上海",
  11.           city: "普陀区",
  12.           address: "上海市普陀区金沙江路 1518 弄",
  13.           zip: 200333,
  14.         },
  15.         {
  16.           date: "2016-05-04",
  17.           name: "王小虎",
  18.           province: "上海",
  19.           city: "普陀区",
  20.           address: "上海市普陀区金沙江路 1517 弄",
  21.           zip: 200333,
  22.         },
  23.         {
  24.           date: "2016-05-01",
  25.           name: "王小虎",
  26.           province: "上海",
  27.           city: "普陀区",
  28.           address: "上海市普陀区金沙江路 1519 弄",
  29.           zip: 200333,
  30.         },
  31.         {
  32.           date: "2016-05-03",
  33.           name: "王小虎",
  34.           province: "上海",
  35.           city: "普陀区",
  36.           address: "上海市普陀区金沙江路 1516 弄",
  37.           zip: 200333,
  38.         },
  39.       ],
  40.       // 列的配置化对象,存储配置信息
  41.       checkList: {},
  42.       showColumn: {
  43.         date: true,
  44.         name: true,
  45.         provinces: true,
  46.         city: true,
  47.         adreess: true,
  48.         zipCode: true,
  49.       },
  50.     };
  51.   },
  52.   watch: {
  53.     // 监听复选框配置列所有的变化
  54.     checkList: {
  55.       handler: function (newnew, oldold) {
  56.         // console.log(newnew); 
  57.         this.showColumn = newnew;
  58.         // 这里需要让表格重新绘制一下,否则会产生固定列错位的情况
  59.         this.$nextTick(() => {
  60.           this.$refs.table.doLayout();
  61.         });
  62.       },
  63.       deep: true,
  64.       immediate: true
  65.     },
  66.   },
  67.   mounted() {
  68.     // 发请求得到checkListInitData的列的名字
  69.     if(localStorage.getItem("columnSet")){
  70.       this.checkList = JSON.parse(localStorage.getItem("columnSet"))
  71.     }else{
  72.       this.checkList = {
  73.         date: true,
  74.         name: true,
  75.         provinces: true,
  76.         city: true,
  77.         adreess: true,
  78.         zipCode: true,
  79.       };
  80.     }
  81.   },
  82.   methods: {
  83.     handleClick(row) {
  84.       console.log(row);
  85.     },
  86.     showColumnOption() {
  87.       this.isShowColumn = true;
  88.     },
  89.     saveColumn() {
  90.       localStorage.setItem("columnSet",JSON.stringify(this.checkList))
  91.       this.isShowColumn = false;
  92.     },
  93.   },
  94. };
  95. </script>

3.css样式部分

  1. .columnOption {
  2.   position: fixed;
  3.   z-index: 20;
  4.   top: 0;
  5.   left: 0;
  6.   width: 100%;
  7.   height: 100%;
  8.   background-color: rgba(0, 0, 0, 0.3);
  9.   display: flex;
  10.   flex-direction: row-reverse;
  11.   .content {
  12.     width: 100px;
  13.     height: 100%;
  14.     background-color: rgb(203, 223, 198);
  15.     .head {
  16.       width: 100%;
  17.       height: 44px;
  18.       border-bottom: 1px solid #000;
  19.       display: flex;
  20.       justify-content: center;
  21.       align-items: center;
  22.       font-size: 12px;
  23.     }
  24.     .body {
  25.       width: 100%;
  26.       height: calc(100% - 88px);
  27.       box-sizing: border-box;
  28.       padding-top: 10px;
  29.       overflow-y: auto;
  30.       .items {
  31.         width: 100%;
  32.         height: 100%;
  33.         overflow-y: auto;
  34.         display: flex;
  35.         flex-direction: column;
  36.         .el-checkbox {
  37.           width: 100%;
  38.           height: 28px;
  39.           line-height: 28px;
  40.           margin-bottom: 14px;
  41.           display: inline-block;
  42.           font-family: PingFang SC;
  43.           font-style: normal;
  44.           font-weight: normal;
  45.           font-size: 14px;
  46.           color: #000;
  47.           overflow: hidden;
  48.           text-overflow: ellipsis;
  49.           white-space: nowrap;
  50.           box-sizing: border-box;
  51.           padding-left: 14px;
  52.         }
  53.         .el-checkbox:hover {
  54.           background-color: #f5f7fa;
  55.         }
  56.       }
  57.     }
  58.     .footer {
  59.       width: 100%;
  60.       height: 44px;
  61.       border-top: 1px solid #000;
  62.       display: flex;
  63.       justify-content: center;
  64.       align-items: center;
  65.     }
  66.   }
  67. }
  68. // 控制淡入淡出效果
  69. .fade-enter-active,
  70. .fade-leave-active {
  71.   transition: opacity 0.3s;
  72. }
  73. .fade-enter,
  74. .fade-leave-to {
  75.   opacity: 0;
  76. }


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/420218
推荐阅读
相关标签
  

闽ICP备14008679号