<el-table

:header-cell-style="selectedCellStyle"

@header-click="handleClick"

></el-table>

data(){

    return {

          cellIndex:[],

         tableData:[]   //表格数据

       }

}

methods:{

       handleClick(column){

               let property = column

              //选中表头

              if(this.cellIndex.includes(property)){

                          this.cellIndex = this.cellIndex.filter((t) => t !== property);

              }else{

                          this.cellIndex.push(property);

              }

             // 获取当前列的数据

             this.selectedData = [];

             this.cellIndex.forEach((prop, i) => {

                        this.selectedData[i] = this.tableData.map((item) => item[prop]);

             });

         },

    selectedCellStyle({column}){

              let property = column

              if(this.cellIndex.includes(property)){

                       return {"background-color": "#409EFF"}

              }

        }

}

el-table里的header-cell-style改变表头单元格的style方法,如果cellIndex这个数组里面没有包括你点击的表头,那么就改变颜色,如果有反之转变回来。

header-click点击表头的回调方法,如果选中的表头里面已经有重复的了,那么把重复的进行过滤掉,留下不是重复的,如果这个数组里面还没有这个表头,那么cellIndex这个数组就会把他加进去,最后你可以自己在设个数组selectedData,把cellIndex数组进行遍历,拿到你选中这列数据的所有数据

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐