引导导航丸在vue js 2

时间:2017-08-26 08:05:05

标签: html twitter-bootstrap vue.js vuejs2 nav-pills

jsfiddle是,https://jsfiddle.net/r6o9h6zm/2/

我在vue js 2中使用了bootstrap导航丸,根据所选标签显示数据(即,如果点击标准的非ac房间,需要显示该特定房间的记录)但是我在这里在实例中获得所有三个房间,我已经使用以下方法来实现它,但它没有给出任何结果。

HTML:

<div id="app">
<div class="room-tab">
  <ul class="nav nav-pills nav-justified tab-line">
    <li v-for="(item, index) in items" v-bind:class="{'active' : index === 0}">
      <a :href="item.id" data-toggle="pill"> {{ item.title }} </a>
    </li>
  </ul>
  <div class="room-wrapper tab-content">
    <div  v-for="(item, index) in items" v-bind:class="{'active' : index === 0}" :id="item.id">
      <div class="row">
        <div class="col-md-8">
        <div class="col-md-4">
          <h3>{{item.title}}</h3>
          <p>{{item.content}}</p>
        </div>
      </div>
    </div><br>
  </div>
</div>

脚本:

new Vue({
  el: '#app',
    data: {
  items: [
            {
                id: "0",
                title: "Standard Non AC Room",
                content: "Non AC Room",
            },
            {
                id: "1",
                title: "Standard AC Room",
                content: "AC Room",
            },
            {
                id: "2",
                title: "Deluxe Room",
                content: "Super Speciality Room",
            },
        ],
  }
})

我如何获得仅包含所选房间类型的记录的结果,而其他房间需要隐藏?

1 个答案:

答案 0 :(得分:1)

添加data属性currentSelected: 0以跟踪选择的房间

new Vue({
  el: '#app',
    data: {
        currentSelected: 0,
          items: [
            {
                id: "0",
                title: "Standard Non AC Room",
                content: "Non AC Room",
            },
            {
                id: "1",
                title: "Standard AC Room",
                content: "AC Room",
            },
            {
                id: "2",
                title: "Deluxe Room",
                content: "Super Speciality Room",
            },
        ],
  },
  methods:{
      selectRoom(index){
          this.currentSelected = index
      }
  }
}) 

在每个导航片上添加点击监听器以更改所选房间

<div id="app">
<div class="room-tab">
  <ul class="nav nav-pills nav-justified tab-line">
    <li 
        v-for="(item, index) in items" 
        v-bind:class="{'active' : index === currentSelected}"
        @click="selectRoom(index)">
      <a> {{ item.title }} </a>
    </li>
  </ul>
  <div class="room-wrapper tab-content">
    <div  
        v-for="(item, index) in items" 
        v-bind:class="{'active' : index === 0}"
        v-if="index === currentSelected"
        :key="item.id">
      <div class="row">
        <div class="col-md-8">
        <div class="col-md-4">
          <h3>{{item.title}}</h3>
          <p>{{item.content}}</p>
        </div>
      </div>
    </div><br>
  </div>
</div>

这里有updated fiddle