el-table的formatter属性的使用方法

文章介绍了formatter在ElementUI的el-table-column中的作用,用于内容格式化,如将数字转换为文本。通过示例展示了如何在HTML和methods中使用formatter函数进行类型转换,使代码更简洁易用。
摘要由CSDN通过智能技术生成

一、formatter是什么?

formatter是el-table-column的一个属性,用来格式化内容。(比如后台给你返0或1,你需要展示成“否”和“是”)

二、详细使用

1.知道formatter之前:

代码如下(示例):

 <el-table :data="tabledata">
      <el-table-column label="类型"  prop="type">
          <template slot-scope="scope">
                <span>
                  <span v-if="scope.row.type === '1'">菜单</span>
                  <span v-else-if="scope.row.type === '2'">按钮</span>
                  <span v-else>其他</span>
                </span>
            </template>
      </el-table-column>
   </el-table>

2.知道formatter之后,以上代码就可以改写为:

html中:

 <el-table :data="tabledata">
      <el-table-column label="类型" :formatter="typeFormatter" prop="type"></el-table-column>
 </el-table>

methods中:

//规范化类型   默认有四个参数(row, column, cellValue, index)详情可以查看elmentUI官网
      typeFormatter(row){
	        switch(row.type){
	          case '1':
	            return '菜单'
	          case '2':
	            return '按钮'
	          default:
	            return '其他'
	        }

      }

总结

把这个理解为function修饰就可以了方便好用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值