简体   繁体   English

如何在 Vuejs 的 v-for 循环中使用 v-else 语句?

[英]How to use v-else statment in the v-for loop in Vuejs?

 computed: { status() { const box = this.box; const paint = this.paint; const matchingdata = this.matchingdata.filter( (m) => m.boxid == box.boxid && m.paintid == paint.paintid )[0]; }
 <div v-for="paint in paints":key="paint.id" class="line"> <div v-if=" matchingdata.some( (m) => m.boxid == box.boxid && m.paintid == paint.paintid ) "> <StatusComponent:box="box":paint="paint":matchingdata="matchingdata" /> <,--only status like ok,not, medium to be printed on line accordingly --> </div> <div v-else>sa</div> //ITS NOT WORKING </div>

From the looping, I am getting the data.从循环中,我正在获取数据。 And i placed the v-if condition, to show content if there is data according to the condition.我放置了v-if条件,如果有数据根据条件显示内容。

And i also, want to set the condition like, If there is no data found i want to show like "no data found".而且我还想设置条件,如果没有找到数据,我想显示“没有找到数据”。 For that i have taken v-else condition.为此,我采取了 v-else 条件。 But else condition not working.但其他条件不起作用。

If set like above (v-else statement) then it is printing for every data.如果像上面那样设置(v-else 语句),那么它会打印每个数据。 How to set condition to only show data, When nothing is matching?如何将条件设置为仅显示数据,什么都不匹配?

You will need another computed property that finds whether anything is matching or not in the entire dataset.您将需要另一个计算属性来查找整个数据集中是否有任何匹配项。 Then use that computed property like so:然后像这样使用该计算属性:

<template v-if="anythingMatching">
 ...your current code...
</template>
<template v-else>
  <div>sa</div>
</template>

Of course, you may use <div> instead of <template>当然,你可以使用<div>代替<template>

You have a few states which you should handle to show the corresponding messages, you need to detect that with some logic您应该处理一些状态以显示相应的消息,您需要使用一些逻辑来检测它们

  • No items but loading them没有项目,但正在加载它们
  • Items are empty项目为空
  • Have items with no filtering有没有过滤的项目
  • Filtered items, no result过滤的项目,没有结果

Then use a computed prop to filter out your result, the filtered copy is what you loop over plus it separates the filtered items from the original so you know that the original had items.然后使用计算的道具过滤掉你的结果,过滤后的副本就是你循环的内容,加上它将过滤后的项目与原始项目分开,这样你就知道原始项目有项目。

For example, (don't have StatusComponent so it just outputs json)例如,(没有 StatusComponent 所以它只输出 json)

<div id="app">
  search <input v-model="filter.search"/>
  box 1 <input type="radio" v-model="filter.box" :value="1">
  box 2 <input type="radio" v-model="filter.box" :value="2">

  <div v-if="!loading">
    <template v-if="filtered_paints.length">
      <div v-for="(paint, index) in filtered_paints" :key="paint.id" class="line">
        {{ index }}: 
        {{ filter.search ? 'searched: ' + filter.search : '' }} 
        {{ filter.box ? 'in box: ' + filter.box : '' }}
        {{ paint }}
      </div>
    </template>
    <template v-else>
      <div v-if="!paints.length">
        There was an issue loading paints.
      </div>
      <div v-else>
        No matching paints, for: 
        {{ filter.search ? filter.search : '' }} 
        {{ filter.box ? 'in box: ' + filter.box : '' }}
      </div>
    </template>
  </div>
  <div v-else>
    Loading paints...
  </div>
</div>
{
  data() {
    return {
      loading: true,
      filter: {
        search: '',
        box: 0
      },
      paints: [],
    };
  },
  computed: {
    filtered_paints() {
      return this.paints.filter((i) => {
        if (!this.filter.box && this.filter.search === '') {
          return true
        }

        let found = false
        if (this.filter.box && this.filter.search) {
          if (i.boxid === Number(this.filter.box) && i.name.startsWith(this.filter.search)) {
            found = true
          }
          return found
        }

        if (this.filter.box && i.boxid === Number(this.filter.box)) {
          found = true
        }

        if (this.filter.search && i.name.startsWith(this.filter.search)) {
          found = true
        }
        return found
      })
    }
  },
  mounted() {
    console.log('Mock loading some paints');
    setTimeout(() => {
      this.paints = [
        { id: 1, paintid: 1, boxid: 1, name: 'white' },
        { id: 2, paintid: 2, boxid: 2, name: 'black' }
      ]
      this.loading = false
    }, 2000)
  }
}

See example online: https://playcode.io/849492/在线查看示例: https://playcode.io/849492/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM