Vue迭代列表并检查条件

时间:2018-02-16 13:21:18

标签: vue.js

我有一个Vue文件,它将数据作为json。我想在我的模板中迭代问题并选择要添加的TR。在Django模板中,我可以这样做:

{% for foo in foos %}
        {% if foo.status == 'status1' %}
            <tr class="bg-warning">
        {% elif foo.status == 'status2' %}
            <tr class="bg-success">
        {% elif foo.status == 'status3' %}
            <tr class="bg-danger">
        {% else %}
            <tr class="bg-info">
        {% endif %}

我正试图在Vue中这样做

 <tr v-for="foo in foos"
          v-bind:class="{
          'bg-warning': isReview(foo.status), 
          'bg-info': isRegistering(foo.status), 
          'bg-danger': isCancelled(foo.status), 
          'bg-success': isFinished(foo.status)}">

接下来是我的方法:

computed: {
  isReview: function (status) {
    if (status === 'status1') {
      return true
    } else {
      return false
    }
  },
  isRegistering: function (status) {
    if (status === 'status2') {
      return true
    } else {
      return false
    }
  },
  isCancelled: function (status) {
    if (status === 'status3') {
      return true
    } else {
      return false
    }
  },
  isFinished: function (status) {
    if (status === 'status4') {
      return true
    } else {
      return false
    }
  }
}

所以我的问题是如何为依赖于Object.status的每次迭代创建1个特定的表行?

3 个答案:

答案 0 :(得分:1)

也许

<template>
    ...
    <tr v-for="foo in foos" :key="foo.status" :class="getStatusClass(foo)">
    ...
</template>

和组件上的方法

methods: {
    getStatusClass(foo) {
        let className = '';

        switch(foo.status) {
            case 'warning': className = 'bg-warning'; break;
            case 'info': className = 'bg-info'; break;
            case 'danger': className = 'bg-danger'; break;
            case 'success': className = 'bg-success'; break;
        }

        return [className];
    }
}

答案 1 :(得分:1)

Vue.js迭代列表并检查条件 - 供您参考:

的index.html

<!DOCTYPE html>
<html>

<head>
    <title>Vue iterating over list with and checking condition</title>
    <link rel="stylesheet" href="css/style.css"></link>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <table>
            <tr v-for="foo in foos" v-bind:class="{
                'bg-warning': isReview(foo.status), 
                'bg-info': isRegistering(foo.status), 
                'bg-danger': isCancelled(foo.status), 
                'bg-success': isFinished(foo.status)}"> 
                <td>{{ foo.issue }}</td>            
            </tr>
        </table>
    </div>
    <script type="text/javascript" src="js/main.js"></script>
</body>

</html>

main.js

new Vue({
    el: '#app',
    data: {
        foos: [{
                'status': 'status4',
                'issue': 'An issue with status4 success'
            },
            {
                'status': 'status1',
                'issue': 'An issue with status2 warning'
            }
        ]
    },
    methods: {
        isReview: function (status) {
            if (status === 'status1') {
                return true
            } else {
                return false
            }
        },
        isRegistering: function (status) {
            if (status === 'status2') {
                return true
            } else {
                return false
            }
        },
        isCancelled: function (status) {
            if (status === 'status3') {
                return true
            } else {
                return false
            }
        },
        isFinished: function (status) {
            if (status === 'status4') {
                return true
            } else {
                return false
            }
        }
    }
});

的style.css

.bg-success {
    background-color: rgb(187, 223, 187);
}
.bg-warning {
    background-color: yellow;
}

Chrome中显示的输出

enter image description here

答案 2 :(得分:0)

根据the docs,我认为语法可能是使用{name: boolean}形式的对象将类包装在数组中。尝试:

<tr v-for="foo in foos"
      v-bind:class="[
      {'bg-warning': isReview(foo.status)}, 
      {'bg-info': isRegistering(foo.status)}, 
      {'bg-danger': isCancelled(foo.status)}, 
      {'bg-success': isFinished(foo.status)}
      ]">