vue.js如何根据属性值将类添加到表行

时间:2016-09-21 07:22:27

标签: vue.js

我正在尝试根据propertys(overallStatus)值向表行添加bootstrap类(成功,减弱...)。

我如何在下面的代码中实现这个功能?

提前致谢!

<div id="people" class="col-md-12">      
    <v-client-table :data="tableData" :columns="columns" :options="options"></v-client-table>
</div>   

Vue.use(VueTables.client, {
    perPage: 50, 
    skin: 'table table-condensed'
});

new Vue({
    el: "#people",
    ready: function () { },
    methods: {},
    data: {
        columns: ['deviceType', 'reasonForStatus', 'ip', 'monitorIsOn', 'freeSpace', 'cpuUsage', 'availableRam', 'statusTime'],
        options: {
            dateColumns: ['statusTime'],
            headings: {
                deviceType: 'Device Type',
                ip: 'Device Ip',
                reasonForStatus: 'ReasonForStatus',
                freeSpace: 'Free Space',
                cpuUsage: 'CPU Usage',
                availableRam: 'Available Ram',
                statusTime: 'Log Time'
            },
            templates: {
                deviceType: function (row) {
                    return row == 0 ? "Stander" : "Monitor";
                },
                availableRam: function (row) {
                    return row.availableRam + " mb.";
                },
                freeSpace: function (row) {
                    return row.freeSpace + " %";
                },
                cpuUsage: function (row) {
                    return row.cpuUsage + " %";
                },
            },

        },
        selectedLetter: '',
        tableData: tableItems,
    }
});

1 个答案:

答案 0 :(得分:4)

您需要使用v-bind:class指令(或更短版本:class)。看看docs here

示例:

data: function () {
  return: {
     error: true,
     errorType: 'alert-error'
  }
}

<template>
  <div class="alert" :class="{ errorType: error }"</div>
</template>

<!-- or static text assignment -->

<template>
  <div class="alert" :class="{ 'alert-error': error }"</div>
</template>

这两者都会导致

<div class="alert alert-error"></div>

要同时绑定多个类,您可以这样做:

<template>
  <div :class="{ 'class1 class2 class3': error }"</div>
</template>

<template>
  <div :class="['class1', 'class2', error ? 'class3' : 'class4']"></div>
</template>