在el-table标签中设置 :cell-style 属性
如:cell-style = cleeStyle
接着在方法中定义
cleeStyle({row, column, rowIndex, columnIndex}){
let arr = [];
for(let item in row) {
if(item !== 'type'){
arr.push(Number(row[item]))
}
};
if(columnIndex -1 === arr.indexOf(Math.max.apply(null,arr))) {
//columnIndex -1 是因为第一列不是数字
//arr.indexOf(Math.max.apply(null,arr)) 可以拿到最大值的下标
return 'color : red';
}
if(columnIndex -1 === arr.indexOf(Math.min.apply(null,arr))) {
//arr.indexOf(Math.min.apply(null,arr)) 可以拿到最小值的下标
return 'color : green';
}
};
上述代码可以实现每一行中的最大值和最小值设置不同的颜色。但是有个问题就是,如果一行中有重复的最大值或者最小值,只会标记第一个最大值或者最小值。
实现一行中所有的最大值和最小值全部标记,使用以下代码:
cleeStyle({row, column, rowIndex, columnIndex}){
let arr = [];
for(let item in row) {
if(item !== 'type'){
arr.push(Number(row[item]))
}
}
let maxIndex = 0,minIndex = 0, minIndexArr = [], maxIndexArr = [];
maxIndex = arr.indexOf(Math.max.apply(null,arr));
minIndex = arr.indexOf(Math.min.apply(null,arr));
arr.forEach((item, index) => {
if (item === arr[minIndex]) {
minIndexArr.push(index);
}
if (item === arr[maxIndex]) {
maxIndexArr.push(index);
}
});
if(maxIndexArr.indexOf(columnIndex -1) >= 0) {
return 'color : red';
}
if(minIndexArr.indexOf(columnIndex -1) >= 0) {
return 'color : green';
}
};