Element UI el-table-column 自定义列渲染并添加类
您可以通过使用el-table-column的custom属性来自定义列的渲染,然后在自定义列函数中为列添加类。下面是一个示例:
<template>
<el-table :data="tableData">
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="年龄" prop="age" custom>
<template slot-scope="scope">
<span :class="{'highlight': scope.row.age > 30}">{{ scope.row.age }}</span>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 25 },
{ name: '李四', age: 35 },
{ name: '王五', age: 40 }
]
};
}
};
</script>
<style>
.highlight {
color: red;
}
</style>
在上面的示例中,我们为el-table-column的prop属性指定了数据的属性名,然后在el-table-column的custom属性中定义了一个自定义列函数。在自定义列函数中,我们使用slot-scope来获取当前行的数据,并根据条件给span元素添加了一个highlight类。在样式中,我们定义了highlight类的样式,使得该列的文本颜色变为红色。
这样,当年龄大于30岁时,该列的文本就会显示为红色。
原文地址: https://www.cveoy.top/t/topic/qfXa 著作权归作者所有。请勿转载和采集!