el-table表格在表头和数据行添加图标、处理数据

通过header插槽自定义表头:

<template slot="header" slot-scope="scope">
 ...
</template>

可以通过scope.row.columnName 获取单元项数据进行数据处理:

<template  slot-scope="scope">
 <span :class="1<2 ? 'red-font' : 'blue-font' ">
    {{formatDate(scope.row.temperature)}} ℃
 </span>
</template>

如图:

                <el-table :data="currentCabinData"
                          style="width: 100%" stripe border height="100">
                  <el-table-column prop="name"
                                   label="设备仓" fixed>
                  </el-table-column>

                  <el-table-column prop="temperature" >
                    <!-- 表头项 -->
                    <template slot="header"
                              slot-scope="scope">
                      <span class="mgrinr1">温度</span>
                      <d2-icon name="thermometer-2"
                               style="font-size: 18px;" />
                    </template>

                    <!-- 行数据项 -->    
                    <template slot-scope="scope">
                      {{ scope.row.temperature }} <!-- scope.row.prop -->   
                      <span v-show="scope.row.temperature"
                            class="unit-color">℃</span>
                    </template>
                  </el-table-column>

                  <el-table-column prop="humidity">
                    <template slot="header"
                              slot-scope="scope">
                      <span class="mgrinr1">湿度</span>
                      <d2-icon name="tint"
                               style="font-size: 18px;" />
                    </template>
                    <template slot-scope="scope">
                      {{ scope.row.humidity }}
                      <span v-show="scope.row.humidity"
                            class="unit-color">%</span>
                    </template>
                  </el-table-column>

                </el-table>

上述el-table中关键字可以实现border纵向边框、stripe斑马线、固定表头或表列、多级表头、最小表高度、行单选、多选、数据过滤、展开行数据、树形数据懒加载、自定义表头、行尾统计、合并行或列等效果

 

 

使用header-cell-style属性修改表头样式,可为函数或对象:

使用cell-style属性更改表格中某个单元格的样式,可为函数或对象:

<!-- html -->
<el-table :header-cell-style="rowClass" :cell-style="cellStyle">
</el-table>
//在method里面写上方法
cellStyle({row, column, rowIndex, columnIndex}){
    if(rowIndex === 1 && columnIndex === 2){ //指定坐标
        return 'background:pink'
    }else{
        return ''
    }
}

 

 

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要动态生成el-table的二维表格,并且使用图标作为表头和表列,可以使用el-table-column组件并结合自定义的作用域插槽来实现。 首先,需要定义一个二维数组的数据结构和对应的图标数组: ```javascript data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ], iconData: [ 'el-icon-user', 'el-icon-goods', 'el-icon-message' ] } } ``` 然后,在模板使用el-table组件,并使用v-for指令动态生成表头表格数据,并在作用域插槽使用图标作为表头和表列的内容: ```html <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex"> <template slot="header" slot-scope="scope"> <i :class="iconData[scope.$index]"></i> </template> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> ``` 在这个例子,我们使用v-for指令遍历二维数组的每一,并使用el-table-column动态生成每一表头。然后,在作用域插槽使用iconData数组对应位置的图标作为表头的内容。 接着,在每一的作用域插槽,再使用v-for指令遍历该的每一个单元格,并使用el-table-column动态生成每一列的表头数据。 最终效果就是一个动态生成的二维表格表头和表列都使用图标作为内容。 完整的代码示例: ```html <template> <el-table :data="tableData"> <el-table-column v-for="(row, rowIndex) in tableData" :key="rowIndex"> <template slot="header" slot-scope="scope"> <i :class="iconData[scope.$index]"></i> </template> <template slot-scope="{ row }"> <el-table-column v-for="(cell, cellIndex) in row" :key="cellIndex"> {{ cell }} </el-table-column> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'] ], iconData: [ 'el-icon-user', 'el-icon-goods', 'el-icon-message' ] } } } </script> ``` 这样就可以实现一个动态生成的二维表格表头和表列都使用图标作为内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值