javascript - Polymer 1.0,如何实现无限嵌套的dom-repeat

标签 javascript json polymer

我有以下 JSON 对象,

    categoryList = [{
    "title": "Computers",
    "categories": 
    [
      {
        "title": "Laptops",
        "categories": 
        [
          {
            "title": "Ultrabooks",
            "categories": 
            [
              {
                "title": "Lenovo",
                "categories": 
                [
                  {
                    "title": "i5 intel"
                  }
                ]
              }
            ]
          },
          {
            "title": "Dell"
          },
          {
            "title": "Macbooks",
            "categories": 
            [
              {
                "title": "Air"
              }, 
              {
                "title": "Pro"
              }
            ]
          }
        ]
      }, 
      {
        "title": "Desktops"
      }, 
      {
        "title": "Tablets",
        "categories": 
        [
          {
            "title": "Apple"
          }, 
          {
            "title": "Android"
          }
        ]
      }, 
      {
        "title": "Printers"
      }
    ]
  }]

正如您所看到的,每个类别都可以有一个子类别,因此它实际上可以永远持续下去。我试图显示所有这些,但我不知道如何做到这一点。

这就是我到目前为止所拥有的一切(显然这只能获得每个类别中的第一个 child ):

<template is="dom-repeat" items="{{categoryList}}" as="category">  
  <template is="dom-if" if="{{_hasData(category.categories)}}">
      <div>{{category.title}}</div>
      <template is="dom-repeat" items="{{category.categories}}" as="item">
         <div>{{item.title}}</div>
      </template>
  </template>
</template>

最佳答案

您可以在此处使用类似递归代码的内容。 Here's一个同样的笨蛋。

<script src="https://polygit.org/components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="https://polygit.org/components/polymer/polymer.html">
<dom-module id="infinite-repeat">
  <template>
    <template is="dom-repeat" items={{categories}} as="category">
      <div>{{category.title}}</div>
      <template is="dom-repeat" items="{{category.categories}}" as="item">
        <div>{{item.title}}</div>
        <template is="dom-if" if="{{_hasData(item.categories)}}">
          <infinite-repeat categories={{item.categories}}></infinite-repeat>
        </template>
      </template>
    </template>
  </template>
</dom-module>
<script>
  Polymer({
    is: 'infinite-repeat',
    properties: {
      categories: {
        type: Array,
        value: function() {
          return [{
            "title": "Computers",
            "categories": [{
              "title": "Laptops",
              "categories": [{
                "title": "Ultrabooks",
                "categories": [{
                  "title": "Lenovo",
                  "categories": [{
                    "title": "i5 intel"
                  }]
                }]
              }, {
                "title": "Dell"
              }, {
                "title": "Macbooks",
                "categories": [{
                  "title": "Air"
                }, {
                  "title": "Pro"
                }]
              }]
            }, {
              "title": "Desktops"
            }, {
              "title": "Tablets",
              "categories": [{
                "title": "Apple"
              }, {
                "title": "Android"
              }]
            }, {
              "title": "Printers"
            }]
          }];
        }
      }
    },
    _hasData: function(item) {
      return item && item.length > 0;
    }
  })
</script>

<infinite-repeat></infinite-repeat>

关于javascript - Polymer 1.0,如何实现无限嵌套的dom-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306516/

相关文章:

javascript - 值不是序列 Safari 异常

javascript - 使用 TypeScript 从表单输入中获取值

javascript - 将结果从 Ajax 请求传输到数组的更简洁的方法

arrays - 需要将 JSON 数据提取字段配置到 flutter 中的小部件元素

sql - 如何从 Postgres 中的 JSON 数据类型列中提取日期

polymer - 在 Polymer 2 中只触发就绪生命周期回调

polymer - polymer 元素使用 getter/setter 声明可绑定(bind)属性的最简单方法?

Javascript 代码在 Ruby on Rails 中不起作用

javascript - 哈希安全漏洞 - 如何修复?

javascript - react 路由器 6.4.3 typescript - useLocation 任何类型错误