element-ui表格列el-table-column如何根据数据不同显示不同的值,获取prop值

发布时间 2023-09-21 11:36:12作者: Code_zero

方法一、格式化数据

在使用element-ui的表格时,有时候后台给你的字段和你要显示在表格列里的内容不一致。
例如后台给的字段是state,它的值为true或false,要求显示在表格里是‘正确’或‘错误’
这时可以给el-table-column添加一个属性:formatter,代码如下:

<el-table ref="accountTable"
  :data="accountsListData"
  border>
  <el-table-column label="状态" prop="state" :formatter="stateFormat"></el-table-column>
</el-table>
methods:{
  stateFormat(row, column) {
    if (row.state === true) {
      return '正确'
    } else  {
      return '错误'
    } 
  },
}

或者:

formatStatus(row, column) {
  return row.status == 'Y' ? '已执行' : '未执行'
},

方法二:直接在template scope 使用v-if判断

<el-table-column prop="status" label="显示状态">
  <template scope="scope">
    <span v-if="scope.row.status=== 1">在线</span>
    <span v-else-if="scope.row.status=== 0">离线</span>
  </template>
</el-table-column>

二、获取element-ui表格中的渲染的prop值

<el-table-column label="操作">
  <template slot-scope="scope">
    <el-table-column label="修改">
      <el-link :underline="false" icon="el-icon-edit" @click="clickChange(scope.row.id)">修改</el-link>
    </el-table-column>
    <el-table-column label="删除">
      <el-link :underline="false" icon="el-icon-delete">删除</el-link>
    </el-table-column>
  </template>
</el-table-column>

label是显示的文字

prop是对应后台获取数据的属性名

原文地址