【问题标题】:Vue2 list with filter带有过滤器的Vue2列表
【发布时间】:2017-12-23 22:21:23
【问题描述】:

我正在尝试在 Vue2 中使用输入过滤器在表中创建一个 foreach 循环。这可行,但我需要创建一个过滤器。

输入代码是

<div class="form-group">
     <input v-model="search" class="form-control" placeholder="Search shortcut...">
</div>

循环是

<tr v-for="edition in editions" >
     <td></td>
     <td class="alert alert-success">{{ edicion.intellij }}</td>
     <td class="alert alert-info">{{ edicion.eclipse }}</td>
     <td>{{ edicion.descripcion }}</td>
 </tr>

问题更新

这是 js (Vue) 代码。在这段代码中,“版本”只有一个元素,但在实际情况下有多个元素。

new Vue({
    el: '#app',
    data: {
        search: '',
  editions: [{
            intellij: "Ctrl + Espacio",
            eclipse: "Ctrl + Espacio",
            description: "Autocompletado de código (clases, métodos, variables)"
    }],
},

});

现在,¿如何使输入文本可以过滤“版本”?

【问题讨论】:

  • all in array 是什么意思?
  • 问题已更新。
  • 那么你的预期输出是什么

标签: vuejs2


【解决方案1】:

我不是 100% 确定我知道您在问什么,但听起来您想将文本输入用作过滤数组的搜索字段。

https://codepen.io/nickforddesign/pen/YYpZYe?editors=1010

随着this.search的值变化,计算出来的值会过滤数组,如果字段为空则直接返回数组。

<div class="app">
  <input type="text" v-model="search">
  <table>
    <tbody>
      <tr v-for="(edition, index) in matches" :key="index">
        <td></td>
        <td class="alert alert-success">{{ edition.intellij }}</td>
        <td class="alert alert-info">{{ edition.eclipse }}</td>
        <td>{{ edition.description }}</td>
      </tr>
    </tbody>
  </table>

还有js:

new Vue({
  el: '.app',
  data() {
    return {
      search: '',
      editions: [{
        intellij: "Ctrl + Espacio",
        eclipse: "Ctrl + Espacio",
        description: "Autocompletado de código (clases, métodos, variables)"
      }, {
        intellij: "Ctrl + C",
        eclipse: "Ctrl + C",
        description: "Copiar"
      }, {
        intellij: "Ctrl + V",
        eclipse: "Ctrl + V",
        description: "Pegar"
      }]
    }
  },
  computed: {
    matches() {
      return this.search
        ? this.editions.filter(edition => {
          let match = false
          for (let key in edition) {
            if (edition[key].toLowerCase().includes(this.search.toLowerCase())) {
              return true
            }
          }
        })
        : this.editions
    }
  }
})

【讨论】:

  • 这是一个正确的部分答案。但是假设我在数组中有多个元素。我可以创建一个在所有数组中搜索的函数吗?不仅在一个元素中。
  • 我确实在editions 数组中包含了多个元素,您说的是这个吗?
  • 我创建了多个“匹配”来过滤所有不同的数组。
猜你喜欢
  • 2021-06-15
  • 2013-11-27
  • 2011-12-14
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2017-09-07
  • 2016-07-12
相关资源
最近更新 更多