You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vuetify3中v-data-table根据值为行添加style_1类问题

问题分析与解决办法

1. 修复rowProps的返回逻辑

你的rowProps方法仅在满足条件时返回对象,不满足条件时无返回值,这会导致Vue无法正确处理所有行的属性配置。必须保证所有情况都返回一个对象:

修改后的方法(简洁版):

rowProps(item) {
  return {
    class: item.lowest === 1 ? "style_1" : ""
  };
}

或者更清晰的对象写法:

rowProps(item) {
  const rowClasses = {};
  if (item.lowest === 1) {
    rowClasses.style_1 = true;
  }
  return { class: rowClasses };
}

2. 确保样式优先级足够

Vuetify的v-data-table行自带默认CSS类(如v-data-table__row),如果你的style_1样式被默认样式覆盖,需要提高优先级:

示例样式写法:

.style_1.v-data-table__row {
  background-color: #ffebee; /* 可根据需求修改样式 */
  color: #c62828;
}

3. 检查数据类型匹配

确认item.lowest的数据类型是数字1还是字符串"1"。如果是字符串,需调整判断条件为全等匹配:

if (item.lowest === "1") {
  // 执行样式添加逻辑
}

内容的提问来源于stack exchange,提问作者RGriffiths

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:02:08