vue表格遍历根据表头里的类型和每行的状态来判断


bash 复制代码
<el-table class="mt-10" :data="weldingdataList"  style="width: 100%"   height="330">
                                <el-table-column 
                                    v-for="(column) in weldingcolumnList"
                                    header-align="center"
                                    :sortable="false"
                                    :key="column.prop + column.propName"
                                    :prop="column.prop"
                                    :label="column.propName"
                                    align="center"
                                    :show-overflow-tooltip="true"
                                >
                                    <!-- 作用域插槽自定义单元格内容 -->
                                    <template slot-scope="scope">
                                        <!-- 根据displayType渲染不同内容 -->
                                        <span 
                                            v-if="['switch', 'remind'].includes(column.displayType)"
                                            :class="getCellStyle(column.displayType, scope.row[column.prop])"
                                            class="status-dot" >
                                        </span>
                                        <span 
                                            v-else
                                            :class="getCellStyle(column.displayType, scope.row[column.prop])">
                                            {{ formatCellValue(column.displayType, scope.row[column.prop]) }}
                                        </span>
                                    </template>
                                </el-table-column>
                            </el-table>
bash 复制代码
getdevicewelding(){
            devicewelding().then(response => {
                this.weldingcolumnList=response.data.columnList
                this.weldingdataList=response.data.dataList
            });
        },
         // 根据displayType和值获取单元格样式类名
        getCellStyle(displayType, value) {
            // 统一转换为字符串比较
            const val = String(value)
            
            switch(displayType) {
                // 模拟量 - 数值展示,无特殊颜色
                case 'analog':
                return 'cell-analog';
                
                // 开关状态 - 1=开启(绿色),0=关闭(灰色)
                case 'switch':
                return val === '1' ? 'cell-switch-on' : 'cell-switch-off';
                
                // 提醒状态 - 1=异常(红色),0=正常(绿色)
                case 'remind':
                return val === '1' ? 'cell-remind-warning' : 'cell-remind-normal';
                
                // 模式状态 - 1=自动(蓝色),0=手动(橙色)
                case 'mode':
                return val === '1' ? 'cell-mode-auto' : 'cell-mode-manual';
                
                // 默认样式
                default:
                return 'cell-default';
            }
        },
        formatCellValue(displayType, value) {
            const val = String(value);
            switch(displayType) {
                case 'mode':
                return val === '1' ? '自动' : '手动';
                default:
                return value; // analog类型显示数值,switch/remind已用图标替代
            }
        },
bash 复制代码
 /* 模拟量样式 */
  .cell-analog {
  color: #333;
}

/* 开关状态样式 - 圆形 */
.cell-switch-on {
  background-color: #67c23a; /* 绿色-开启 */
}
.cell-switch-off {
  background-color: #909399; /* 灰色-关闭 */
}

/* 提醒状态样式 - 圆形 */
.cell-remind-warning {
  background-color: #f56c6c; /* 红色-异常 */
}
.cell-remind-normal {
  background-color: #67c23a; /* 绿色-正常 */
}

/* 模式状态样式 */
.cell-mode-auto {
  color: #409eff; /* 蓝色-自动 */
  font-weight: bold;
}
.cell-mode-manual {
  color: #e6a23c; /* 橙色-手动 */
  font-weight: bold;
}

/* 圆形样式统一设置 */
.status-dot {
  display: inline-block;
  width: 12px;   /* 圆形大小 */
  height: 12px;  /* 宽高一致才是正圆 */
  border-radius: 50%; /* 圆角50%实现圆形 */
}

/* 默认样式 */
.cell-default {
  color: #333;
}
相关推荐
从文处安1 天前
「九九八十一难」组合式函数到底有什么用?
前端·vue.js
前端Hardy1 天前
面试官:JS数组的常用方法有哪些?这篇总结让你面试稳了!
javascript·面试
yuki_uix1 天前
Props、Context、EventBus、状态管理:组件通信方案选择指南
前端·javascript·react.js
全栈老石1 天前
手写无限画布4 —— 从视觉图元到元数据对象
前端·javascript·canvas
用户11489669441051 天前
VUE3响应式原理——从零解析
vue.js
用户83040713057011 天前
SPA 首屏加载速度慢怎么解决?
vue.js·webpack
一枚前端小姐姐1 天前
低代码平台表单设计系统技术分析(实战三)
前端·vue.js·低代码
SuperEugene1 天前
从 Vue2 到 Vue3:语法差异与迁移时最容易懵的点
前端·vue.js·面试
Leon1 天前
新手引导 intro.js 的使用
前端·javascript·vue.js
Forever7_1 天前
仅用一个技巧,让 JavaScript 性能提速 500%!
前端·vue.js