【问题标题】:Vue JS nested loop search not returning resultsVue JS嵌套循环搜索不返回结果
【发布时间】:2019-01-20 21:41:41
【问题描述】:

我正在构建一个键命令资源,并在此过程中让 VueJS 试一试。我是一个新手,但正在掌握事物(慢慢地......)。

我希望能够在全局搜索表单中搜索我在命令部分中定义为 动作 的关键命令(请参阅下面的数据示例)。我想搜索所有操作以仅显示与搜索条件匹配的操作。

我的 HTML 如下:

<div id="commands">

  <input v-model="searchQuery" />

  <div class="commands-section" v-for="item in sectionsSearched" 
:key="item.id">

    <h3>{{ item.section }}</h3>

    <div class="commands-row" v-for="command in item.command" :key="command.action"> 
          {{ command.action }}
    </div>

  </div>
</div>

我的主 Vue 实例如下所示:

import Vue from 'vue/dist/vue.esm'
import { commands } from  './data.js'

document.addEventListener('DOMContentLoaded', () => {

  const element = document.getElementById("commands")

  if (element != null) {

  const app = new Vue({
    el: element,
    data: {
      searchQuery: '',
      commands: commands
    },
    computed: {
      sectionsSearched() {
        var self = this;
        return this.commands.filter((c) => {
          return c.command.filter((item) => {
            console.log(item.action)
            return item.action.indexOf(self.searchQuery) > -1;
           });
          });
        },
      }
    });
  }
});

最后是data.js中的数据结构

const commands = [
  {
     section: "first section",
     command: [
       { action: '1' },
       { action: '2' },
       { action: '3' },
     ],
  },
  {
     section: "second section",
     command: [
       { action: 'A' },
       { action: 'B' },
       { action: 'C' },
     ]
  },  
]

export { commands };

我可以使用console.log(item.action) sn-p 输出命令,您在称为sectionsSearched 的计算方法中看到。

我在浏览器中看不到任何错误,并且数据正确呈现。

不能但是通过实时搜索进行过滤。我几乎肯定它是我的数据结构+计算方法的组合。任何人都可以了解我在这里做错了什么吗?

理想情况下,我希望保持数据不变,因为分割很重要。

我是一名 Rails 人,对这些东西不熟悉,因此欢迎提供任何和所有反馈。

谢谢!

编辑

我已经尝试了以下建议的解决方案,但在我通过的任何查询中都会收到undefined。在大多数情况下,该功能似乎适用于以下情况:

 sectionsSearched() {
      return this.commands.filter((c) => {
        return c.command.filter((item) => {
          return item.action.indexOf(this.searchQuery) > -1;
         }).length > 0;
      });
    },

但遗憾的是,实际上什么都没有回来。我正在努力挠头。

【问题讨论】:

    标签: javascript search vue.js vuejs2


    【解决方案1】:

    您的sectionsSearched 存在问题,因为它返回的只是命令数组。

    看到这个

    sectionsSearched() {
      return this.commands.reduce((r, e) => {
        const command = e.command.filter(item => item.action.indexOf(this.searchQuery) > -1);
        const section = e.section;
        r.push({
          section,
          command
        });
      }, []);
    }
    

    【讨论】:

    【解决方案2】:

    const commands = [
      {
         section: "first section",
         command: [
           { action: '1' },
           { action: '2' },
           { action: '3' },
         ],
      },
      {
         section: "second section",
         command: [
           { action: 'A' },
           { action: 'B' },
           { action: 'C' },
         ]
      },  
    ]
    
    const element = document.getElementById("commands")
    
      if (element != null) {
    
      const app = new Vue({
        el: element,
        data: {
          searchQuery: '',
          commands: commands
        },
        computed: {
          sectionsSearched() {
            var self = this;
            return this.commands.filter((c) => {
            
             // the code below return an array, not a boolean
             // make this.commands.filter() not work
             
             // return c.command.filter((item) => {
             //   return item.action.indexOf(self.searchQuery) > -1;
             //  });
             
             // to find whether there has command action equal to searchQuery
              return c.command.find(item => item.action === self.searchQuery);
             });
            },
          }
        });
      }
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <div id="commands">
    
      <input v-model="searchQuery" />
    
      <div class="commands-section" v-for="item in sectionsSearched" 
    :key="item.id">
    
        <h3>{{ item.section }}</h3>
    
        <div class="commands-row" v-for="command in item.command" :key="command.action"> 
              {{ command.action }}
        </div>
    
      </div>
    </div>

    这是你希望的工作吗?

    【讨论】:

    • 这似乎应该可行,但是当我执行console.log(c.command.find(item =&gt; item.action === self.searchQuery)) 的console.log 并尝试搜索时,我输入的所有内容都会返回为undefined。有什么想法吗?
    • 这也只返回第一部分的输出。我需要它用于所有部分和命令
    • 输入A时,console.log(c.command.find(item =&gt; item.action === self.searchQuery))会输出{action:"A"}。输入2会输出{action: "2"}只有当你的输入不匹配任何动作时才会输出undefined。
    • 并且代码工作正常。如果您的目的是当输入完全等于'A'或'B'或'C'时,将显示整个Section B。并且当输入完全等于1或2 或 3 将显示整个 A 部分。或者如果你想做模糊搜索,这段代码将不起作用。也许你应该将比较逻辑更改为c.command.find(item =&gt; self.searchQuery.includes(item.action))
    【解决方案3】:
    sectionsSearched() {
        return this.commands.filter((c) => {
          return c.command.filter((item) => {
            return item.action.indexOf(this.searchQuery) > -1;
           }).length > 0;
        });
        },
      }
    

    因为过滤器总是会返回一个数组(无论是否为空),其值总是为真。

    【讨论】:

    • 谢谢。这让我走得最远。目前,当我执行搜索并且查询与命令匹配时,我没有得到任何结果。有什么想法吗?
    • 在什么情况下你会得到一个未定义的?
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多