javascript - 如何设置 v-data-table 上特定行的样式? [验证]

标签 javascript vue.js vuetify.js

我想要做的就是为包含等于 lowestEntry 的条目的给定行更改背景颜色。

<v-col cols="8">
        <v-data-table
          :loading="loadEntryTable"
          loading-text="A procurar dados..."
          :headers="this.entryheaders"
          :items="this.stockentries"
          :items-per-page="10"
        >
        //Have tried using the v-slot.item to achieve it but no success
</v-data-table>
</v-col>

我想将 tr 背景颜色更改为绿色。在 ìtem.id_entry == lowestEntry["id_entry"] 时突出显示它。

最佳答案

如果您使用的是较新版本的 vuetify,您可以访问 item-class 作为 v-data-table 的属性。这将提供 item 作为回调函数的第一个参数。

<v-data-table
....
:item-class="itemRowBackground"
></v-data-table>

然后定义返回类名的函数:

methods: {
  itemRowBackground: function (item) {
     return item.protein > 4.2 ? 'style-1' : 'style-2'
  }
}

然后只需为style-1style-2 定义类:

.style-1 {
  background-color: rgb(215,215,44)
}
.style-2 {
  background-color: rgb(114,114,67)
}

这里有一个适用于您的示例的代码笔 codepen example

编辑 如果 :item-class 不适用于您当前版本的 Vuetify,或者您需要对行进行更多控制,而不仅仅是绑定(bind)一个类,您您必须使用 item 插槽并手动绑定(bind)类/样式/等。

定位 item 插槽并手动将类绑定(bind)到行:

<v-data-table>
    <template #item="{ item }">
      <tr :class="item.id_entry === lowestEntry['id_entry'] ? 'custom-bg' : ''">
        //manually define all of your <td> elements now.
      </tr> 
    </template>
<v-data-table>

或者,您可以传递 :class="customRowClass(item, lowestEntry)" 并定义 customRowClass 方法:

methods: {
  customRowClass (item, lowestEntry) {
    return item.id_entry === lowestEntry['id_entry'] ? 'custom-bg' : ''
  }
}

关于javascript - 如何设置 v-data-table 上特定行的样式? [验证],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57961043/

相关文章:

函数中的 JavaScript 变量

typescript - 使用 TypeScript 的 Vue Prop 类型

javascript - 如何让vuejs点击事件同步

javascript - 访问 Vue JS 实例监视对象中的 $refs 数组

flexbox - vuetify 中心项目到 v-flex

javascript - 如何设置 DataTables 默认的 sEmptyTable 值?

javascript - 为什么 Obj2 无法通过 Object.Create() 从 Obj1 中按原型(prototype)找到函数

javascript - Vuex 状态属性设置为响应 GET 请求。函数给出错误,因为所述属性显然为 NULL

vue.js - Vuetify 输入自动完成错误

javascript - extjs 4 动态存储