<el-table-column :formatter formatter的使用方法
时间: 2023-11-29 13:44:13 浏览: 258
<-table-column>是Element UI中的一个表格列组件,它可以通过设置formatter属性来格式化表格中的数据。具体使用方法如下:
1. 在<el-table-column>标签中设置formatter属性,值为一个函数名或一个匿名函数。
2. 在Vue实例中定义该函数,函数的参数为当前单元格的值和当前行的数据对象,函数的返回值为格式化后的值。
例如:
```html
<template>
<el-table :data="tableData">
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄" :formatter="formatAge"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18 },
{ name: '李四', age: 20 },
{ name: '王五', age: 22 }
]
}
},
methods: {
formatAge(age, row) {
return age + '岁'
}
}
}
</script>
```
在上面的例子中,我们定义了一个名为formatAge的函数,它接受两个参数:age和row,分别表示当前单元格的值和当前行的数据对象。在函数中,我们将age和字符串'岁'拼接起来作为返回值,从而实现了对年龄数据的格式化。
阅读全文
相关推荐
















<template> <el-table :data="tableData"> <el-table-column prop="name" label="名称" :formatter="formatName"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { id: 1, name: '项目A', link: 'https://example.com/project-a' }, { id: 2, name: '项目B', link: 'https://example.com/project-b' }, // ... ] }; }, methods: { formatName(row) { return ${row.name}; } } }; </script>如果tableDate数据来自于数据库,tableDate本身没有数据,该怎么优化这段代码


