1. 程式人生 > 實用技巧 >elementui 表格 批量操作

elementui 表格 批量操作

html

      <el-button size="medium" @click="handleHide">批量隱藏</el-button>
      <el-button size="medium" @click="handleShow">批量顯示</el-button>
      <el-button size="medium"  @click="handleDelete">批量刪除</el-button>
      <el-table ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%;margin-top: 20px;"
                @selection-change="handleSelectionChange">
            <el-table-column
                    label="狀態"
                    width="140"
                    show-overflow-tooltip>
                <template slot-scope="scope">
                   <span><i class="el-icon-view show_icon" :class="[scope.row.is_show=='0' ? 'show_icon' : 'hide_icon']"></i>{{scope.row.is_show | getIsShow(scope.row.is_show)}}</span>
                </template>
            </el-table-column>
        </el-table>

data資料

tableData:[{is_show:0}],
selectedData:[],//被選中的資料

判斷顯示隱藏過濾器

      filters:{
          getIsShow(key){
              let is_show = '';
              switch (key) {
                  case 0:
                      is_show = '顯示';
                      break;
                  case 1:
                      is_show = '隱藏';
                      break;
              }
              return is_show
          }
        },

函式

            handleSelectionChange(val) {
                this.selectedData = val;
            },
            handleDelete(){//批量刪除
                var val = this.selectedData;
                if(val) {
                    val.forEach((item,index) => {
                        this.tableData.forEach((val,idx) => {
                            if(item === val){
                                this.tableData.splice(idx,1);
                            }
                        })
                    })
                }
            },
            handleHide(){//批量隱藏
                var val = this.selectedData;
                if(val) {
                    val.forEach((item,index) => {
                        this.tableData.forEach((val,idx) => {
                            if(item === val && val.is_show === 0){
                                val.is_show = 1;
                            }
                        })
                    })
                }
                this.$refs.multipleTable.clearSelection();
            },
            handleShow(){//批量顯示
                var val = this.selectedData;
                if(val) {
                    val.forEach((item,index) => {
                        this.tableData.forEach((val,idx) => {
                            if(item === val && val.is_show === 1){
                                val.is_show = 0;
                            }
                        })
                    })
                }
                this.$refs.multipleTable.clearSelection();
            },