在v-for循环中仅显示一次项目

时间:2018-05-14 22:01:47

标签: vue.js vuejs2

我有以下对象,我想使用一个值作为节标题,我想在循环中使用但只显示一次。

children: {
  child_1: {
    id: 'child_1',
    name: 'Ruth Ann Morgan',
    gender: 'female',
    birth_year: '1999',
    parentage: 'our_child',
    dependency: 'not_dependent'
  },
  child_2: {
    id: 'child_2',
    name: 'James Nico Morgan',
    gender: 'male',
    birth_year: '2012',
    parentage: 'our_child',
    dependency: 'is_dependent'
  },
  child_3: {
    id: 'child_3',
    name: 'Dave Ash Moran',
    gender: 'male',
    birth_year: '1996',
    parentage: 'partners_child',
    dependency: 'is_dependent'
  }
},

我想使用parentage作为将内容分组在一起并在内容上方显示标题的方法。我已经厌倦了v-once,但这在循环中不起作用。

<div v-for="child in children" :key="child">
    <div v-if="child.parentage == 'our_child'">
        <p class="text-muted text-xs font-weight-medium mb-0 kern-1" v-once>Our Children</p>
        <a href="#" class="d-flex flex-row justify-content-between w-100 text-sm">
          <div>
            <strong v-text="child.name"></strong>, was born in <span v-text="child.birth_year"></span>, and <span v-text="child.gender"></span> is <span v-text="child.dependency"></span>
           </div>
           <div class="d-none d-lg-block text-sm text-uppercase brand-primary-color font-weight-bold" :id="child.id" @click.prevent="getChildId(child.id) + showMyChildForm()">Edit</div>
                </a>
            </div>

    <div class="mt-2" v-if="child.parentage == 'partners_child'">
         <p class="text-muted text-xs font-weight-medium mb-0 kern-1" v-once>Partner's Children</p>
         <a href="#" class="d-flex flex-row justify-content-between w-100 text-sm">
       <div>
          <strong v-text="child.name"></strong>, was born in <span v-text="child.birth_year"></span>, and <span v-text="child.gender"></span> is <span v-text="child.dependency"></span>
        </div>
        <div class="d-none d-lg-block text-sm text-uppercase brand-primary-color font-weight-bold" :id="child.id" @click.prevent="getChildId(child.id) + showMyChildForm()">Edit</div>
        </a>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

看起来您基本上希望根据父母身份过滤子内容。一种简单的方法是过滤计算属性中的children数据,如下所示:

computed: {
  showOurChildren() {
    return this.children.filter(child => child.parentage === 'our_child')
  },
  showPartnersChildren() {
    return this.children.filter(child => child.parentage === 'partners_child')
  }
}

new Vue({
  el: "#app",
  data: {
    children: [{
        id: 'child_1',
        name: 'Ruth Ann Morgan',
        gender: 'female',
        birth_year: '1999',
        parentage: 'our_child',
        dependency: 'not_dependent'
      },
      {
        id: 'child_2',
        name: 'James Nico Morgan',
        gender: 'male',
        birth_year: '2012',
        parentage: 'our_child',
        dependency: 'is_dependent'
      },
      {
        id: 'child_3',
        name: 'Dave Ash Moran',
        gender: 'male',
        birth_year: '1996',
        parentage: 'partners_child',
        dependency: 'is_dependent'
      }
    ]
  },
  computed: {
    showOurChildren() {
      return this.children.filter(child => child.parentage === 'our_child')
    },
    showPartnersChildren() {
      return this.children.filter(child => child.parentage === 'partners_child')
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h3 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
<div id="app">
  <h3>Our children</h3>
  <div v-for="child in showOurChildren">
    {{ child.name }}
  </div> <br>
  <h3>Partner's children</h3>
  <div v-for="child in showPartnersChildren">
    {{ child.name }}
  </div>

</div>

这个策略怎么样? https://jsfiddle.net/3c1jakyc/