vue formatter返回html,vue.js - Vue element-ui <el-table-column> formatter – how to display html? - Sta...

Ok, after a few hours of brainstorming I found pretty nice solution. I'm putting it here for others - I hope this helps somebody.

Value displayed in custom column can be:

simple string (prop)

formatted string (safe, without any html or components, via original formatter)

customized value (html, component, also safe!)

In order to achieve this, I had to create custom formatter components, which I put in folder i.e. /TableFormatters/

For this purpose, there is simple functional component DatetimeFormatter that display date-time with icon.

TableFormatters/DatetimeFormatter.vue

{{ props.row[props.column] }}

export default {

name: 'DatetimeFormatter',

}

Here come's columns configuration:

import DatetimeFormatter from './TableFormatters/DatetimeFormatter'

// ...

data() {

return {

data: [/*...*/],

columns: [

name: {

label: 'Name',

},

state: {

label: 'State',

formatter: row => {

return 'State: '+row.state__label

}

},

created_at: {

label: 'Created at',

formatter: DatetimeFormatter

}

]

}

}

Now it's time to define :

v-for="(column, index) in columns"

:key="index"

:label="columns[column] ? columns[column].label : column"

:prop="column"

:formatter="typeof columns[column].formatter === 'function' ? columns[column].formatter : null">

:is="columns[column].formatter"

:row="row"

:column="column">

{{ row[column] }}

This works like a charm. What's going on here with formatter?

First we check if the formatter is set as a function. If so, the native formatter takes the control, because will not be created. Otherwise formatter component will be created (via :is attribute). However, it there is no formatter, the plain value for a row will be shown.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值