【问题标题】:Search nested array of JS objects for v-treeview在嵌套的 JS 对象数组中搜索 v-treeview
【发布时间】:2021-08-07 23:42:58
【问题描述】:

我正在尝试为我的 Vuetify v-treeview 组件实现一个搜索功能,它使用一个过滤器函数,用于嵌套的 JS 对象数组,看起来像。

[
   {
      Name: 'Name Level 1',
      Title: 'Title Level 1',
      children: [
         {
            Name: 'Name Level 2',
            Title: 'Title Level 2',
            children: [ ... ]
         }
      ]
   },
...
]

现在我有一个文本字段,可以对搜索进行 v-models,还有一个过滤功能,看起来像 Bara Koc 评论中的那个。

filter () {
      return (item, search, textKey) => {
        let result = []
        const _filter = (item, search, textKey) => {
          for (const leaf of item) {
            if (leaf[textKey].indexOf(search) !== -1) {
              result = [...result, { Name: leaf.Name, Title: leaf.Title }]
            }
            if (leaf.children) {
              return _filter(leaf.children, search, textKey)
            } else {
              return null
            }
          }
        }
        _filter(item, search, textKey)
        return result
      }
    }

v-treeview 看起来像

<v-treeview
      :items="treeItems"
      :search="search"
      :filter="filter"
      :open.sync="open"
      item-key="Name"
      dense
      style="max-height: 700px;"
      class="overflow-y-auto overflow-x-auto"\>

search is v-modelto av-text-field` 作为要搜索的字符串,但出现以下错误。

vue.runtime.esm.js?5eb8:1888 TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.

【问题讨论】:

    标签: javascript arrays vue.js search vuetify.js


    【解决方案1】:

    您可以通过递归函数遍历树。您可以将数据结构视为 M 树。我也使用闭包进行数据封装。

    const data = [
       {
          Name: 'Name Level 1',
          Title: 'Apple',
          children: [
             {
                Name: 'Name Level 2',
                Title: 'Title Level 2',
                children: [{
                  Name: 'Name Level 3',
                  Title: 'Application',
                  children: null
                }]
             }
          ]
       },
       {
          Name: 'Name Level 1',
          Title: 'Apple',
          children: null
       }
    ]
    
    const filter = (item, search, textKey) => {
      let result = []
      const _filter = (item, search, textKey) => {
        for (const leaf of item) {
          if (leaf[textKey].indexOf(search) !== -1) {
            result = [...result, {Name: leaf.Name, Title: leaf.Title}]
          }
          leaf.children ? _filter(leaf.children, search, textKey) : null
        }
      }
      _filter(item, search, textKey)
      return result
    }
    
    console.log(filter(data, 'App', 'Title'))

    【讨论】:

    • 谢谢@Bera Koc,只是一个简单的问题,我在`leaf.children 上得到Expected an assignment or function call and instead saw an expression? _filter(leaf.children, search, 'Title') : null` 行。知道为什么吗?我猜这是因为我尝试将您的过滤器常量作为 Vue.js 方法并将其传递给v-treeview
    • 我不太喜欢 Vue。 Vue 中必须存在一些数据差异。由于函数是纯函数,我认为发送它不会导致任何错误。从查看错误消息来看,我认为您发送它的方式是错误的。我希望我能帮助你更多。
    猜你喜欢
    • 2020-06-11
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    相关资源
    最近更新 更多