View Element Plus table formatter supports return types of string and VNode
object;
If you use the h function directly globally, no reference is needed.
The following common basic usage: In Element Plus, you can use custom formatter
functions to return VNode
objects to achieve more flexible custom rendering.
First, define a custom formatter
function in the Table component and return an VNode
object.
<template>
<el-table :data="tableData">
<el-table-column prop="value" label="Value" :formatter="formatValue"></el-table-column>
</el-table>
</template>
<script>
import { h } from 'vue';
export default {
data() {
return {
tableData: [
{ value: 10 },
{ value: 20 },
{ value: 15 },
// 其他数据
],
};
},
methods: {
formatValue(row, column, cellValue) {
if (cellValue > 15) {
return h('span', { style: 'color: red;' }, cellValue);
} else {
return h('span', { style: 'color: green;' }, cellValue);
}
},
},
};
</script>
In the above code, we create an object of an element using h
a function (module from Vue 3 ) . Depending on the value of the cell, we dynamically set different styles.@vue/runtime-core
span
VNode
In this way, the Table component will render cells that are dynamically styled based on the cell value.
Please note that h
the function is used to create VNode
the object and it accepts three parameters: tag name, attribute object and child node. You can create different VNode
objects as needed to implement custom rendering.
The effect here: