vue3中<el-table-column>状态的显示

方法 1:使用作用域插槽 + <el-tag> 标签

javascript 复制代码
<template>
  <el-table :data="tableData">
    <!-- 其他列 -->
    <el-table-column prop="status" label="状态">
      <template #default="{ row }">
        <el-tag :type="row.status === 1 ? 'success' : 'danger'">
          {{ row.status === 1 ? '启用' : '禁用' }}
        </el-tag>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
const tableData = [
  { id: 1, name: '数据1', status: 1 },
  { id: 2, name: '数据2', status: 0 },
  // ...
]
</script>

方法 2:使用 formatter 格式化显示

javascript 复制代码
<template>
  <el-table :data="tableData">
    <el-table-column
      prop="status"
      label="状态"
      :formatter="statusFormatter"
    ></el-table-column>
  </el-table>
</template>

<script setup>
const statusFormatter = (row) => {
  return row.status === 1 ? '启用' : '禁用'
}
</script>

自定义颜色样式

如果需要更个性化的样式,可以自定义 CSS 类名:

javascript 复制代码
<template>
  <el-table-column prop="status" label="状态">
    <template #default="{ row }">
      <span :class="['status-label', row.status === 1 ? 'active' : 'inactive']">
        {{ row.status === 1 ? '启用' : '禁用' }}
      </span>
    </template>
  </el-table-column>
</template>

<style>
.status-label.active {
  color: #67C23A;
  font-weight: bold;
}
.status-label.inactive {
  color: #F56C6C;
  font-style: italic;
}
相关推荐
吃饺子不吃馅21 小时前
⚡️ Zustand 撤销重做利器:Zundo 实现原理深度解析
前端·javascript·github
Wang's Blog21 小时前
前端FAQ: 描述⼀下你最近使⽤过的前端框架,并解释为何选择它们?
前端·vue.js·faq
callmeSoon1 天前
Solid 初探:启发 Vue Vapor 的极致框架
vue.js·前端框架·响应式设计
远航_1 天前
10 个被严重低估的 JS 特性,直接少写 500 行代码
前端·javascript
小高0071 天前
当前端面临百万级 API 请求:从"修 CSS 的"到架构师的进化之路
前端·javascript·面试
LateFrames1 天前
使用 Winform / WPF / WinUI3 / Electron 实现异型透明窗口
javascript·electron·wpf·winform·winui3
Asort1 天前
React类组件精要:定义机制与生命周期方法进阶教程
前端·javascript·react.js
陳陈陳1 天前
从“变量提升”到“调用栈爆炸”:V8 引擎是如何偷偷执行你的 JavaScript 的?
javascript
小二·1 天前
从零到上线:Spring Boot 3 + Spring Cloud Alibaba + Vue 3 构建高可用 RBAC 微服务系统(超详细实战)
vue.js·spring boot·微服务
San301 天前
深入理解JavaScript执行机制:从变量提升到内存管理
javascript·编程语言·代码规范