Vue模板条件rener

时间:2016-01-14 10:53:03

标签: javascript vue.js

<template id="players-template">
<div v-for="player in players" v-bind:class="{ 'row': $index == 0}">
    <div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <a href="#">{{ player.username }}</a>
                    <span class="small pull-right">{{ player.createdAt }}</span>
                </h3>
            </div>

            <div class="panel-body">
                <img alt="" class="img-circle center-block">
            </div>
            <div class="panel-footer">
                <div class="btn-group btn-group-justified" role="group" aria-label="...">
                    <a href="#" class="btn btn-primary btn-success send-message" data-toggle="tooltip" data-placement="bottom" title="Wyślij wiadomość" id="{{ player.username }}"><span class="glyphicon glyphicon-envelope"></span>&nbsp;</a>
                    <a href="#" class="btn btn-primary btn-info" data-toggle="tooltip" data-placement="bottom" title="Pokaż profil"><span class="glyphicon glyphicon-user"></span>&nbsp;</a>
                    <a href="#" class="btn btn-primary btn-primary" data-toggle="tooltip" data-placement="bottom" title="Zobacz szczegółowe informacje o poście"><span class="glyphicon glyphicon-option-horizontal"></span>&nbsp;</a>
                </div>
            </div>
        </div>
    </div>
    </div>

我想以每行第三个元素的行结束div,然后追加新行。我如何使用if条件?

2 个答案:

答案 0 :(得分:1)

<div id="app" class="container">
    <div v-for="player in players">
        <div class="row" v-if="($index + 1) % 3 == 0 ">
          {{$index + 1}}
        </div>
    </div>
</div>

$ index是v-for

中的一个特殊变量

http://vuejs.org/guide/list.html#v-for

答案 1 :(得分:1)

我想我想出来了

<template id="players-template">
<div class="row">
    <template v-for="player in players">
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <a href="#">{{ player.username }}</a>
                        <span class="small pull-right">{{ player.createdAt }}</span>
                    </h3>
                </div>

                <div class="panel-body">
                    <img alt="" class="img-circle center-block">
                </div>
                <div class="panel-footer">
                    <div class="btn-group btn-group-justified" role="group" aria-label="...">
                        <a href="#" class="btn btn-primary btn-success send-message" data-toggle="tooltip" data-placement="bottom" title="Wyślij wiadomość" id="{{ player.username }}"><span class="glyphicon glyphicon-envelope"></span>&nbsp;</a>
                        <a href="#" class="btn btn-primary btn-info" data-toggle="tooltip" data-placement="bottom" title="Pokaż profil"><span class="glyphicon glyphicon-user"></span>&nbsp;</a>
                        <a href="#" class="btn btn-primary btn-primary" data-toggle="tooltip" data-placement="bottom" title="Zobacz szczegółowe informacje o poście"><span class="glyphicon glyphicon-option-horizontal"></span>&nbsp;</a>
                    </div>
                </div>
            </div>
        </div>

        <template v-if="($index + 1) % 3 == 0 ">
        </div><div class="row">
        </template>
    </template>
</div>
</template>

诀窍是在循环之前打开一行,然后关闭它。但是,在每三个玩家之后,我们关闭该行并开启一个新行。这将导致每三名球员占一排。

然而,在大多数情况下,这不是必需的 - bootstrap会知道将每组三个带到一个新行,因为你使用了col-md-4